Alex
Alex

Reputation: 347

Is there an error in Pythons talib.ATR method?

Here is the data:

high = np.array([10697.12, 10706.16, 10744.75, 10747.88, 10745.42])
low = np.array([10683.51, 10694.72, 10705.16, 10728.22, 10727.29])
close = np.array([10696.47, 10705.16, 10728.23, 10742.46, 10730.27])

Method 1: Input the data into Average True Range method directly

talib.ATR(high, low, close, timeperiod=3)
output: array([nan, nan, nan, 23.56333333, 21.75222222])

Method 2: Calculate True Range first, and then take average

talib.TRANGE(high, low, close)
output: array([nan, 11.44, 39.59, 19.66, 18.13])
taking 3 day average:
(11.44+39.59+19.66)/3=23.56
(39.59+19.66+18.13)/3=25.79

So the Average True Range array should be: array([nan, nan, nan, 23.56, 25.79])

The last value in the Average True Range array from the first method is different from the second method. (21.75 vs 25.79)

What is wrong here?

Upvotes: 4

Views: 6695

Answers (1)

Hernán Alarcón
Hernán Alarcón

Reputation: 4099

The calculation of talib.ATR() is correct.

From the Average true range page on Wikipedia:

The ATR at the moment of time t is calculated using the following formula: (This is one form of an exponential moving average)

ATR_{t}={{ATR_{{t-1}}\times (n-1)+TR_{t}} \over n}

Using your values:

>>> (23.56333333 * 2 + 18.13) / 3
21.752222219999997

Upvotes: 8

Related Questions