I am trying to add an Average True Range column to a dataframe that contains historical stock data.
The code I am using so far is:
def add_atr_to_dataframe (dataframe):
dataframe['ATR1'] = abs (dataframe['High'] - dataframe['Low'])
dataframe['ATR2'] = abs (dataframe['High'] - dataframe['Close'].shift())
dataframe['ATR3'] = abs (dataframe['Low'] - dataframe['Close'].shift())
dataframe['TrueRange'] = max (dataframe['ATR1'], dataframe['ATR2'], dataframe['ATR3'])
return dataframe
The last line, containing the max function, gives the error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I have googled for days in trying to learn how to resolve this error, or do the code in a better way, etc and have found nothing that helps me along.
Any help in the following would be much appreciated:
How to resolve the error
How to do the code in a better way - I do not mean that I have to code it this way and there may be better ways to do it.
Thanks ahead of time.