backtest-engine · multi-asset case study
Case study: SPY/TLT relative momentum walk-forward
SPY and TLT daily · 2016-11-21 to 2026-11-20 · 5028 bar events (2,514 per asset) · adjusted for distributions
What is measured
This uses real daily histories for two assets. On the first bar of each month the strategy computes each asset's return over the selected lookback and puts 100% of equity in the winner, 0% in the loser. The lookback is selected on each training block and applied to the following, non-overlapping test block. The first test callback emits both legs; the second callback at the same timestamp is a no-op.
The engine values the two assets independently, marks the portfolio at their latest closes and charges every fill separately. The reference is a static 50/50 SPY/TLT portfolio, bought once at the start of the out-of-sample period and never rebalanced, with no costs.
Data provenance
| Source | Yahoo Finance chart API |
|---|---|
| Retrieved | 2026-11-23T16:29:25Z |
| Symbols / bars | SPY 2514 · TLT 2514 |
| Range | 2016-11-21T21:00:00Z to 2026-11-20T21:00:00Z |
| CSV SHA-256 | aa12fdae89a3740aaa91bad77ec5e071e96384f49cbb418035c57e544739e076 |
| Engine data hash | b30e32289503cf9e0d9001f4aa1cb40cbed340fa4f06dba40fd5636b4b5130cb |
| Adjustment | OHLC scaled by Yahoo adjclose/close; volume unchanged |
Walk-forward design
Training window: 504 timestamps. Test window: 252 timestamps. Step: 252, so test windows are contiguous and never overlap. Lookback grid: 63, 126, 252. The winner is chosen by training Sharpe, then total return, then grid order. 7 folds fit in the available history. Across the test folds the strategy selected SPY 69 times and TLT 22 times.
Out-of-sample performance
Out-of-sample equity
The grey line is the static 50/50 SPY/TLT reference over the same test timestamps. It is not rebalanced and has no costs.
Strategy vs static 50/50
| Metric | Relative momentum | Static 50/50 |
|---|---|---|
| Total return | 34.2857% | 91.2796% |
| Annualized volatility | 17.8669% | 12.2823% |
| Sharpe ratio | 0.3258 | 0.8189 |
| Max drawdown | -46.6757% | -27.8624% |
The strategy underperforms the static 50/50 reference on total return. The result is published as measured, including the drawdown and cost drag.
Capital allocation and per-asset costs
Target weights are fractions of total portfolio equity. The engine sizes each order as
(w_i * E - position_i * price_i) / price_i at the decision close and fills it at that
asset's next open. The strategy uses w_SPY + w_TLT = 1, so gross exposure is 100% and
cash is the residual after fills. The engine does not enforce a budget; the strategy is responsible
for keeping the sum of absolute weights at or below 1.
Every fill is priced and charged on its own asset. A switch from SPY to TLT produces a sell fill
and a buy fill, so the fixed commission is paid twice. Commission is
fixed + bps * |qty_i| * open_i / 10_000; spread and slippage use that asset's open and
volume. The table below is the actual out-of-sample cost split.
| Asset | Fills | Notional | Commission | Spread | Slippage | Total |
|---|---|---|---|---|---|---|
| SPY | 62 | 22,633,022.92 | 2,263.30 | 2,263.30 | 9.00 | 4,535.61 |
| TLT | 31 | 17,556,560.78 | 1,755.66 | 1,755.66 | 135.78 | 3,647.09 |
| Total | 93 | 40,189,583.69 | 4,018.96 | 4,018.96 | 144.78 | 8,182.70 |
Folds
| Fold | Train | Test | Lookback | 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 | 126 | 0.3065 | -8.5554% | -0.5801 | -14.5080% | 19 | 2,475.35 |
| 1 | 2017-11-21 to 2019-11-21 | 2019-11-22 to 2020-11-20 | 63 | 0.5372 | 20.2835% | 0.8896 | -15.7027% | 15 | 1,045.27 |
| 2 | 2018-11-21 to 2020-11-20 | 2020-11-23 to 2021-11-22 | 63 | 0.8616 | 21.8327% | 1.6593 | -6.6475% | 13 | 1,197.80 |
| 3 | 2019-11-22 to 2021-11-22 | 2021-11-23 to 2022-11-22 | 63 | 1.1312 | -39.1397% | -1.9685 | -46.1132% | 19 | 2,115.57 |
| 4 | 2020-11-23 to 2022-11-22 | 2022-11-23 to 2023-11-24 | 252 | 0.4463 | 15.0671% | 1.0584 | -9.9743% | 7 | 200.82 |
| 5 | 2021-11-23 to 2023-11-24 | 2023-11-27 to 2024-11-25 | 252 | 0.1173 | 33.3437% | 2.4379 | -8.4056% | 6 | 200.33 |
| 6 | 2022-11-23 to 2024-11-25 | 2024-11-26 to 2025-11-28 | 126 | 1.6770 | 7.3108% | 0.4601 | -18.7552% | 14 | 947.57 |
Look-ahead checks on the two-asset data
as_of on 2016-11-21, requesting
2016-11-22. The first divergent decision is #0 on
2016-11-21: normal SPY +1.000000, TLT +0.000000; masked SPY +0.000000, TLT +0.000000. Divergent decisions:
121. Normal final equity 2,025,864.26; masked final equity
1,000,000.00.
The trap is cross-sectional: it does not use a single future price, it
ranks two assets with their next-bar returns. The detector compares the per-symbol target weights
and finds the first month where the ranking changes. In the masked replay every future request
returns None, so the strategy stays flat. Strict mode rejects the future reads before a
raw report can be produced.
Reproduce
python -m pip install . python scripts/build_multi_asset_case.py python scripts/verify_multi_asset_case.py
The committed report hash is in docs/multi_asset.sha256. The numbers
above come from this exact CSV and this exact code. No result is hand-entered.