I am trying to run the below code in a DataFrame. My intention is to include as part of the DataFrame (df) "Buy", "Sell", "Hold" or "Wait" based on the below conditions.
for i in range(len(df)):
if df['sma_fast'][i-1]>df['sma_slow'][i-1] and df['sma_fast'][i-2]<df['sma_slow'][i-2]:
df['Signal'] = 'Buy'
elif df['sma_fast'][i-1]>df['sma_slow'][i-1] and df['sma_fast'][i-2]>df['sma_slow'][i-2]:
df['Signal'] = 'Hold'
elif df['sma_fast'][i-1]<df['sma_slow'][i-1] and df['sma_fast'][i-2]>df['sma_slow'][i-2]:
df['Signal'] = 'Sell'
elif df['sma_fast'][i-1]<df['sma_slow'][i-1] and df['sma_fast'][i-2]<df['sma_slow'][i-2]:
df['Signal'] = 'Wait'
else:
df['Signal'] = 'nan'
But I am getting the following error:
KeyError: -1
Could you please help me solving this issue?
Thanks!
Aucun commentaire:
Enregistrer un commentaire