Why is TA-Lib in Python returning nan when computing the ATR?

3.7k views Asked by At

I was trying to get the ATR for a couple of symbols (BTCUSDT, ETHUSDT) with TA-Lib but the results would be all "nan", and I can't figure out why.

Here is an minimal example with exemplary data:

import talib
import numpy

highs = numpy.array([10697.12, 10706.16, 10744.75, 10747.88, 10745.42])
lows = numpy.array([10683.51, 10694.72, 10705.16, 10728.22, 10727.29])
closes = numpy.array([10696.47, 10705.16, 10728.23, 10742.46, 10730.27])

print(talib.ATR(highs, lows, closes, timeperiod = 5))

And the output:

[nan nan nan nan nan nan nan nan nan nan nan nan nan nan]
1

There are 1 answers

0
truf On BEST ANSWER

Your example is supposed to result in something like

(<TALibResult.OK: 0>, array([nan, nan, nan, nan, nan]))

and if you try timeperiod = 3 you'll get something like

(<TALibResult.OK: 0>, array([nan, nan, nan, 23.56333333, 21.75222222]))

Because ATR is a rolling function with timeperiod = N. According to the readme of TA-Lib python wrapper

Typically, these functions will have an initial "lookback" period (a required number of observations before an output is generated) set to NaN.

Technically count of NaNs (lookback period) depends on optional arguments you pass to rolling mean indicator (or default values used in it). But it may be not equal to it. For ex., in case of rolling means of rolling means indicator. If you don't want to just skip NaN's from the beginning of a result array but know exact number of data to skip then you may get this value. I suppose in python this could be achieved via Ta-Lib's Abstract API and lookback property of a function object.