backtest-engine · real-data case study
Case study: SPY moving average walk-forward
SPY daily · 2016-11-21 to 2026-11-20 · 2514 bars · adjusted for distributions
What is measured
This is a real daily history of SPY, not synthetic data. The strategy is a long/flat moving-average rule. Every fold selects its window only on the training block, then runs the winning window on the following, non-overlapping test block. Costs are charged on every fill: 1 bps commission, 2 bps spread, and 100 bps slippage at 100% participation.
The source is Yahoo Finance's chart API. OHLC values are multiplied by Yahoo's adjusted close, so splits and distributions are reflected in the price series. Volume is unchanged. Yahoo data is used here for research; check the provider's terms before commercial use.
Data provenance
| Source | Yahoo Finance chart API |
|---|---|
| Retrieved | 2026-11-23T15:04:30Z |
| Symbol / rows | SPY / 2514 |
| Range | 2016-11-21T21:00:00Z to 2026-11-20T21:00:00Z |
| CSV SHA-256 | 2db20ca1bd97775f20b913b2b4fc9620c3bbf130b735a1e1d1b6e0b4f3da8fff |
| Engine data hash | 4f5c488080735c1d7fd06b4e206ef8dda958f5c9671df3f99791a558a7cd6ebb |
| Adjustment | OHLC scaled by Yahoo adjclose/close; volume unchanged |
Walk-forward design
Training window: 504 bars. Test window: 252 bars. Step: 252 bars, so test windows are contiguous and never overlap. Grid: 20, 50, 100, 200. The winner is chosen by training Sharpe, then total return, then grid order. 7 folds fit in the available history.
Out-of-sample performance
Out-of-sample equity
The grey line is buy-and-hold over the same test timestamps. It is shown as a reference and is not part of the walk-forward strategy. The strategy has no costs on the benchmark line.
Strategy vs buy and hold
| Metric | Walk-forward strategy | Buy and hold |
|---|---|---|
| Total return | 69.1985% | 187.5521% |
| Annualized volatility | 10.0943% | 20.0574% |
| Sharpe ratio | 0.7980 | 0.8560 |
| Max drawdown | -12.9362% | -33.7173% |
The walk-forward strategy underperforms buy-and-hold on total return. This is the honest result, including the cost drag.
Folds
| Fold | Train | Test | Window | Train Sharpe | Test return | Test Sharpe | Test max DD | Fills | Cost |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 2016-11-21 to 2018-11-20 | 2018-11-21 to 2019-11-21 | 100 | 0.8533 | -3.3619% | -0.3556 | -12.5754% | 43 | 3,226.19 |
| 1 | 2017-11-21 to 2019-11-21 | 2019-11-22 to 2020-11-20 | 20 | 0.8391 | 8.6288% | 0.5927 | -10.0937% | 68 | 3,895.72 |
| 2 | 2018-11-21 to 2020-11-20 | 2020-11-23 to 2021-11-22 | 20 | 1.2455 | 14.0713% | 1.5770 | -4.0515% | 64 | 4,458.02 |
| 3 | 2019-11-22 to 2021-11-22 | 2021-11-23 to 2022-11-22 | 100 | 1.6990 | -4.3264% | -0.5610 | -10.0353% | 22 | 1,747.61 |
| 4 | 2020-11-23 to 2022-11-22 | 2022-11-23 to 2023-11-24 | 50 | 0.1907 | 5.9861% | 0.6976 | -6.7771% | 39 | 3,397.23 |
| 5 | 2021-11-23 to 2023-11-24 | 2023-11-27 to 2024-11-25 | 100 | 0.2330 | 18.3017% | 1.7784 | -8.4056% | 15 | 621.62 |
| 6 | 2022-11-23 to 2024-11-25 | 2024-11-26 to 2025-11-28 | 100 | 1.8231 | 17.7863% | 1.9935 | -5.0714% | 6 | 200.59 |
Look-ahead checks on the real data
research_series) on 2016-11-21, requesting data through
2026-11-20. The first divergent decision is #0 on
2016-11-21: normal target weight +0.660893, masked target weight
+0.000000. Divergent decisions: 2514. Normal final equity
1,209,445.25, masked final equity 1,000,000.00.
The subtle strategy does not peek at tomorrow's price. It computes the mean and standard deviation of the whole sample and normalizes each close with them. In the masked replay it only sees bars up to the clock, so the first standard deviation is zero and it stays flat. The detector compares the two decision traces and reports the first difference. The default strict mode rejects the research view before any report can be produced.
Reproduce
python -m pip install . python scripts/build_case_study.py python scripts/verify_case_study.py
The committed report hash is in docs/case_study.sha256. The numbers
above come from this exact CSV and this exact code. No result is hand-entered.