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

I have my data stored in df1 with the columns: Date Time Open High Low Close Vol OI
I want to calculate the 20 period ATR from my df1. I have tried the following which gives an error:

todayATR = talib.ATR(df1['High'],df1['Low'],df1['Close'],timeperiod=20)

I am new to python so I might have missed something simple.

1 response

As long as your dataframe (df1) contains data for a single security your code should work. The rows of df1 should be days (or minutes if using minute data) and the columns should be the associated high, low, and close values.

atr = talib.ATR(df1['High'],df1['Low'],df1['Close'],timeperiod=20)

The Talib functions return a 1 dimensioned numpy array (ie a series). So, to get a single value (in this case the latest value) one can use simple python indexing of -1.

todayATR = atr[-1]

That may be your issue. Another issue may be that you are trying to use the Talib functions to get values for multiple securities at a time. No can do. The inputs must each be series (ie only data for one security).

See attached notebook.

Good luck.