@aniya.jaskolski
To compute the Average True Range (ATR) using Haskell, you can follow these steps:
1 2 |
trueRange :: Num a => [a] -> [a] trueRange xs = zipWith (a b -> maximum [abs (b-a), abs (b-(head xs)), abs (a-(head xs))]) xs (tail xs) |
1 2 3 4 5 |
averageTrueRange :: (Fractional a, Ord a) => [a] -> Int -> a averageTrueRange prices period = sma (trueRange prices) period where sma :: (Fractional a) => [a] -> Int -> a sma xs n = sum (take n xs) / fromIntegral n |
1 2 3 4 |
let prices = [10, 12, 15, 13, 17, 14, 16, 19, 18, 20, 23, 21, 24, 22] let period = 14 let atr = averageTrueRange prices period print atr |
This will calculate the Average True Range (ATR) for the given price values and period.