Learning momentum-based/trend-following strategies
Momentum-based/trend-following strategies are types of technical analysis strategies. They assume that the near-time future prices will follow an upward or downward trend.
Rolling window mean strategy
This strategy is to own a financial asset if its latest stock price is above the average price over the last X days.
In the following example, it works well for Apple stock and a period of 90 days:
%matplotlib inline from zipline import run_algorithm from zipline.api import order_target_percent, symbol, set_commission from zipline.finance.commission import PerTrade import pandas as pd import pyfolio as pf import warnings warnings.filterwarnings('ignore') def initialize(context): Â Â Â Â context.stock = symbol('AAPL') Â Â Â Â context.rolling_window = 90 Â Â Â Â set_commission(PerTrade(cost=5)) def handle_data(context, data): Â Â Â Â price_hist...