November 2019
Beginner
394 pages
10h 31m
English
In this section, we will implement a naive strategy based on the number of times a price increases or decreases. This strategy is based on the historical price momentum. Let's have a look at the code:
def naive_momentum_trading(financial_data, nb_conseq_days): signals = pd.DataFrame(index=financial_data.index) signals['orders'] = 0 cons_day=0 prior_price=0 init=True for k in range(len(financial_data['Adj Close'])): price=financial_data['Adj Close'][k] if init: prior_price=price init=False elif price>prior_price: if cons_day<0: cons_day=0 cons_day+=1 elif price<prior_price: if cons_day>0: cons_day=0 cons_day-=1 if cons_day==nb_conseq_days: signals['orders'][k]=1 elif cons_day == -nb_conseq_days: signals['orders'][k]=-1 ...