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...