19 — Backtest & Validate

Walk-Forward Validation#

Backtesting✓ Mathematical
◆ The PatternRolling window backtesting — the right way to validate strategies on time-ordered data

Walk-forward slides a training window through time, always testing on the next unseen period. It simulates real deployment: train on the past, predict the future. Anchored mode grows the training window. Rolling keeps it fixed.

Train: [t−W, t] → Test: [t+1, t+S]
W = training window  |  S = test step  |  then slide forward by S and repeat.
// Interactive — rolling vs anchored walk-forward
Window size8
Mode
# Python — walk-forward with TimeSeriesSplit
from sklearn.model_selection import TimeSeriesSplit

tscv = TimeSeriesSplit(n_splits=5)
results = []
for train_idx, test_idx in tscv.split(X):
    X_train, X_test = X[train_idx], X[test_idx]
    model.fit(X_train, y[train_idx])
    score = model.score(X_test, y[test_idx])
    results.append(score)

print(f"Walk-forward: {np.mean(results):.3f}")
Pattern bridge: Walk-forward validation is cross-validation adapted for time. The same "never test on training data" principle, but respecting temporal order — critical in both ML deployment and market trend analysis.
Real-world pipeline: backtesting a strategy
  1. Define windows: Train on 2 years, test on next 3 months, slide forward by 3 months
  2. Feature engineering inside each fold — never compute indicators on future data
  3. Track per-window metrics: Sharpe, drawdown, hit rate — look for consistency, not just the average
  4. Compare to benchmark: Does your model beat buy-and-hold in each window, or just on average?
  5. Stress test: Include windows with crashes (2008, 2020, 2022) — how does the model perform under stress?
Common pitfall — look-ahead bias: Using indicators like 52-week high/low that peek into the future of your test window. Also: survivorship bias — backtesting on today's S&P 500 ignores all the companies that went bankrupt. Use point-in-time datasets.
← Previous
Maximum Drawdown & Recovery
Open in the full reader, with the topic sidebar →