Strategy · Trend following

Momentum.

Buys a stock that has been rising and sits above its moving average; sells it short on the mirror condition.

The rule

  1. Long when the return over the lookback is above the threshold and the close is above its moving average.
  2. Short when the return over the lookback is below minus the threshold and the close is below its moving average.
  3. Flat otherwise.

The moving average is a trend filter: it stops the rule buying a bounce inside a downtrend. The threshold keeps it out of the market when the price is drifting rather than moving.

Every signal is acted on at the next day's close, never the close it was computed from. Skipping that one-day shift is the most common way a backtest invents returns (why).

Parameters

ParameterDefaultWhat it does
Lookback20 daysHow far back the return is measured.
Moving average50 daysThe trend filter's window.
Threshold2%How large that return must be before the rule takes a position.

What validation tries

Walk-forward validation picks the best of these 16 combinations on the first 70% of the period, then scores only that one on the remaining 30%. The fewer combinations, the less room to find one that fit by luck.

  • lookback 10 · MA 20
  • lookback 10 · MA 50
  • lookback 10 · MA 100
  • lookback 10 · MA 200
  • lookback 20 · MA 20
  • lookback 20 · MA 50
  • lookback 20 · MA 100
  • lookback 20 · MA 200
  • lookback 40 · MA 20
  • lookback 40 · MA 50
  • lookback 40 · MA 100
  • lookback 40 · MA 200
  • lookback 60 · MA 20
  • lookback 60 · MA 50
  • lookback 60 · MA 100
  • lookback 60 · MA 200

How it did on AAPL

From the write-up: the same three strategies on Apple, 10 bp costs, with the 70/30 split. The best in-sample combination on the first window was lookback 20 / MA 200. Moving the end date one year moves the split, and the verdict can flip.

Test period endsIn-sample SharpeOut-of-sample SharpeVerdict
2023 (2018–2024 run)0.949−0.303failed
2024 (2018–2025 run)0.7660.547held up

Hypothetical results on past data, re-run by a script in the repository. Not a recommendation to trade this or any strategy.

Where it misleads

Trend following earns in sustained moves and is whipsawed in choppy markets, so its result depends on which of the two a test period happens to contain. On AAPL it failed on 2022–2023 and held up once 2024's uptrend was in the test half. One good run says more about the period than about the rule.

More on reading a result like this: how to tell if a backtest is overfit.

Run it yourself

The same set-up, AAPL from 2018 to 2024, opens in the workspace with this strategy's defaults, straight after sign-up if you are new. Change the ticker or the dates and the Validate tab runs the same checks.

The same rule on other stocks: 39 US and Indian stocks. The other strategies: MACD · Bollinger. Head to head: Momentum vs MACD · Momentum vs Bollinger bands.