Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
talib.ATR always returns NaN

Hi everyone!

I'm new to Quantopian and trying to use talib's ATR function to test some trading ideas. However, the function is always returning NaN. Does anyone have any idea of why this might be the case? Here's my implementation:

Request closes, lows and highs for last 4 days

closes = history(4, '1d', 'price')
lows = history(4, '1d', 'low')
highs = history(4, '1d', 'high')

Loop through context.stocks

for stock in context.stocks:
yesterday_ATR = talib.ATR(highs[stock][-4:-2], lows[stock][-4:-2], closes[stock][-4:-2])[-1]
today_ATR = talib.ATR(highs[stock][-3:-1], lows[stock][-3:-1], closes[stock][-3:-1])[-1]
log.info(str(today_ATR)+", "+str(yesterday_ATR))
#Always returns "nan, nan"

As you can see, I am interested in the three-day ATR for both yesterday and today. Any help would be much appreciated!

1 response

Solved. Had to remove the indexes on the arrays (and add timeperiod=3) because talib.atr calculates the atr for each array elem. Then I had to select the 2nd to last item for yesterday's ATR, and the last for today's:
yesterday_ATR = talib.ATR(highs[stock], lows[stock], closes[stock], timeperiod=3)[-2]
today_ATR = talib.ATR(highs[stock], lows[stock], closes[stock], timeperiod=3)[-1]