I'm developing a market sentiment trading strategy for my Investment class. Basically, the strategy currently consists of selling the market index in Zone 1 and 3 and buying (holding) the market index on Zones 2 and 4. The zones are divided based on three lines : Upper (avg + std deviation), Lower (avg - std deviation) and Avg of a previous calculated Market Sentiment indicator.
What I want to do is to enhance the program by buying the market index once you enter Zone 4 and exiting the market only once you hit Zone 1 and vice-versa. Zone 2 and 3, therefore, have their results based on the where the trend is coming from (if it entered through zone 1 or through zone 4).
How can I possibly do this?
for i in range(data.shape[0]) :
Zone_1 = data.loc[i,"Market Sentiment"] > data.loc[i,'Upper Bound']
Zone_2 = data.loc[i,"Market Sentiment"] > data.loc[i,'Average Sentiment'] and data.loc[i,"Market Sentiment"] <= data.loc[i,'Upper Bound']
Zone_3 = data.loc[i,"Market Sentiment"] > data.loc[i,'Lower Bound'] and data.loc[i,"Market Sentiment"] <= data.loc[i,'Average Sentiment']
Zone_4 = data.loc[i,"Market Sentiment"] <= data.loc[i,'Lower Bound']
if Zone_1:
tracker = "n"
list1.append(data.loc[i,'risk free rate'])
elif Zone_2 and data.loc[i-1,"Market Sentiment"] in Zone_1:
tracker = 'down'
list1.append(data.loc[i,'risk free rate'])
elif Zone_2 and data.loc[i-1,"Market Sentiment"] in Zone_2:
if data.loc[i-1,'tracker'] == 'down':
list1.append(data.loc[i,'HS300 Index Return'])
else:
list1.append(data.loc[i,'risk free rate'])
elif Zone_2 and data.loc[i-1,'Market Sentiment'] in Zone_3:
tracker = 'up'
list1.append(data.loc[i,'HS300 Index Return'])
elif Zone_3 and data.loc[i-1,'Market Sentiment'] in Zone_2:
tracker = 'down'
list1.append(data.loc[i,"risk free rate"])
elif Zone_3 and data.loc[i-1,'Market Sentiment'] in Zone_3:
if data.loc[i-1,'tracker'] == 'down':
list1.append(data.loc[i,'risk free rate'])
else:
list1.append(data.loc[i,'HS300 Index Return'])
list1.append(data.loc[i,'HS300 Index Return'])
elif Zone_3 and data.loc[i-1,'Market Sentiment'] in Zone_4:
tracker = 'up'
list1.append(data.loc[i,'HS300 Index Return'])
elif Zone_4:
list1.append(data.loc[i,"HS300 Index Return"])
data['Strategy Return'] = list1
data['Tracker'] = tracker
pd.options.display.float_format = '{:.4f}'.format
return data
