Backtesting in Python
The pitfalls that invalidate backtests
5 min
Most impressive backtests are wrong. The reasons are well known, and avoiding them is most of the skill.
Look-ahead bias
Using information that would not have been available at decision time. The classic case: deciding to buy on today's close using today's High — which you only know after the session ends. Even subtler: fitting a scaler or indicator on the whole dataset before splitting. If any future data leaks into a past decision, the backtest is fiction.
Survivorship bias
Testing only on instruments that still exist today. Backtesting a stock strategy on the current index members ignores every company that went bankrupt or was delisted — exactly the losers your strategy would have held. Returns look far better than reality.
Overfitting (curve-fitting)
Tuning parameters until the strategy fits the historical noise perfectly. With enough parameters you can fit any past — and predict no future. A strategy with twelve optimized parameters that looks flawless in-sample is the single most common trap in retail algo trading.
Costs and slippage
Ignoring the real cost of trading. Every trade pays the spread, possibly a commission, and suffers slippage (filling at a worse price than expected, especially on stops and in fast markets). A strategy that trades often can be wildly profitable before costs and a guaranteed loser after them. Always model costs, and model them pessimistically.
The defence
Reserve out-of-sample data the model never sees during development, prefer fewer parameters, test across multiple instruments and periods, and assume your real-world results will be worse than the backtest. If they are not, be suspicious.
This content is for educational and informational purposes only and is not investment, financial, tax or legal advice. Trading and investing carry risk, including the possible loss of capital. Any performance shown by third-party tools is hypothetical and not a promise of future results. Do your own research and consider professional advice before making any decision.