The rules for the trend strategy as outlined in the Alpha Architect white paper are:
The strategy can be flat, 50% short, or 100% short, depending on which rules trigger. For simplicity, we'll modify the first rule to go short when the market is below 0, rather than below the T-bill return.
The file vmot_trend.py contains the strategy code. The highlights are shown below. First, we get a boolean DataFrame of times when the market is below 0:
one_year_returns = (closes - closes.shift(252))/closes.shift(252)
market_below_zero = one_year_returns < 0
Then we get a boolean DataFrame of times when the market is below its moving average:
mavgs = closes.rolling(window=252).mean()
market_below_mavg = closes < mavgs
We convert these two boolean DataFrames to integers, add them, and negate them to get a DataFrame of 0, -1, or -2, indicating how many hedge signals triggered.
hedge_signals = market_below_zero.astype(int) + market_below_mavg.astype(int)
hedge_signals = -hedge_signals
Then, in signals_to_target_weights
, we divide the signals by two to get a DataFrames of target weights consisting of 0 (not hedged), -0.5 (50% hedged), and -1 (100% hedged).
weights = signals / 2
We also include code to rebalance the hedge weekly, rather than daily.
Execute the following cell to "install" the strategy by moving the file to the /codeload/moonshot
directory:
# make directory if doesn't exist
!mkdir -p /codeload/moonshot
!mv vmot_trend.py /codeload/moonshot/
Before running the full VMOT backtest, we can run a backtest on just the trend strategy:
from quantrocket.moonshot import backtest
backtest("vmot-trend", filepath_or_buffer="vmot_trend_backtest.csv")
The tear sheet shows that the strategy loses money over time but made significant gains during the bear markets. The full VMOT backtest will help us decide whether this is a worthwhile tradeoff in the context of our portfolio.
from moonchart import Tearsheet
Tearsheet.from_moonshot_csv("vmot_trend_backtest.csv")