Home > Net >  Python TA library, ATR getting errors in dataframe series
Python TA library, ATR getting errors in dataframe series

Time:10-02

my code

import ccxt
import ta
import pandas as pd

ftx = ccxt.ftx()
markets = ftx.load_markets()
df = pd.DataFrame(ftx.fetch_ohlcv(symbol, timeframe='1h'))
df.rename(columns = {0:'time', 1:'open', 2:'high', 3:'low', 4:'close', 5:'volume'}, inplace = True)    
df['atr'] = ta.volatility.AverageTrueRange(df.high,df.low,df.close, window=14, fillna=True)
#df.dropna(inplace=True)
df = df[['open','high','low','close','volume','atr']]
print(df)

the output i am getting atr column errors

any idea what's causing this? (i have tested other technicals such as RSI, and MACD they seems to be working just perfectly with same dataset - Link to this techincal - https://technical-analysis-library-in-python.readthedocs.io/en/latest/ta.html#ta.volatility.AverageTrueRange)

CodePudding user response:

The solution can be found in the documentation you linked. The keyword in this case is class.

From the documentation:

class ta.volatility.AverageTrueRange (...)

...

average_true_range() -> pandas.core.series.Series

So you are currently just creating a class holding parameters for creating your desired output. However, you are not calling the function (average_true_range()) that actually calulates and returns the Seriesyou can add to your DataFrame. Therefore, change your code as shown below to add the atr series to your DataFrame.


import ta
import pandas as pd

df = pd.DataFrame({
    'high': [10, 10, 10],
    'low': [8, 8, 8],
    'open': [9, 9, 9],
    'close': [9, 9, 9]
})

df['atr'] = ta.volatility.AverageTrueRange(
    df.high,
    df.low,
    df.close,
    window=2,
    fillna=False
).average_true_range() # <- call function


print(df)

  • Related