Created 5P moving average and 20P moving average for dataframe and am trying to create a new column based on the values of the 5P & 20P moving average.
If 5Pmoving average > 20Pmoving average then new column cell to = 'BUY' If 5Pmoving average < 20Pmoving average then new column cell to = 'SELL'
I've already created a column with says either BUY/SELL
Im trying to create a new column which confirmed if the row and row+1 is different then BUY/SELL depending on the order.
This is what my dataframe looks like
CBA = {5MA: [13.11,13.44,13.56,13.45,13.10,12.45],
20MA: [12.67,12.77,12.87,13.50,13.45,12.30],
test: [BUY,BUY,BUY,SELL,SELL,BUY]}
and i want the outcome
CBA = {5MA: [13.11,13.44,13.56,13.45,13.10,12.45],
20MA: [12.67,12.77,12.87,13.50,13.45,12.30],
test: [BUY,BUY,BUY,SELL,SELL,BUY],
BUY/SELL: [NaN, NaN,SELL,NaN,BUY]}
i've made this function
def buy_sell_strat(df):
for i, row in df:
if df.loc[i,'test'] == 'SELL' & df.loc[i+1,'test'] == 'BUY':
return 'BUY'
elif df.loc[i,'test'] == 'BUY' & df.loc[i+1,'test'] == 'SELL':
return 'SELL'
else:
return 'NaN'
CBA['Buy/Sell'] = CBA.apply(buy_sell_strat,axis = 1)
However im getting error
TypeError: cannot unpack non-iterable float object