TradeHouse Fidelity Lab · Guides
Overfitting in backtrader
Backtrader is a popular open-source Python backtesting framework. Its optimisation feature makes it easy to test hundreds of parameter combinations in one run, and that is exactly where overfitting tends to start.
How optimisation creates overfitting
When you call cerebro.optstrategy() with ranges for several parameters, backtrader runs one backtest per combination. Ten values for each of three parameters is 1,000 backtests. If you then sort by Sharpe ratio or final value and keep the top row, you have selected the maximum of 1,000 noisy draws. Some of that result is skill, some is luck, and the run does not tell you how much is which.
Practical steps inside a backtrader workflow
- Hold out data before you optimise. Load a separate data feed for the most recent period and do not run optimisation on it.
- Record the number of combinations. It is the trial count you need for a deflated Sharpe ratio.
- Look for plateaus, not peaks. If the best parameters sit on an isolated spike and their neighbours lose money, the edge is probably noise.
- Set realistic costs. Use the broker commission and slippage settings rather than leaving costs at zero.
- Check order timing. Orders placed in
next()fill on the next bar by default. Cheat-on-close or cheat-on-open modes can introduce look-ahead bias if used without care. - Walk forward. Optimise on one window, freeze the parameters, run the frozen strategy on the next window, and repeat.
Exporting trades for an independent check
Backtrader reports closed trades through the strategy's notify_trade callback and through analyzers such as TradeAnalyzer. Writing each closed trade (time, instrument, side, size, entry and exit price, fees) to a CSV gives you a file you can upload to Fidelity Lab. The upload schema lists the columns it reads.
What Fidelity Lab adds
Fidelity Lab grades the exported trades without seeing your code: 11 contamination detectors, fees re-netted from a per-venue schedule, a frozen-threshold walk-forward, day-clustered effective sample size and Benjamini-Hochberg at q=0.05. The verdict is SURVIVES, DISSOLVES or UNTESTABLE.
Try it on your own trades. The free Fidelity Lab tier runs one report a month on up to 5,000 legs: all 11 contamination detectors, cost re-netting and the walk-forward verdict. Run a free report or see pricing.
Frequently asked questions
Is backtrader itself the cause of overfitting?
No. Any engine with fast parameter sweeps makes overfitting easy. The cause is selecting the best of many runs without correcting for it.
Should I stop using optstrategy?
No. Use it, but count the combinations, keep a hold-out period and judge the winner against that count.
Related guides
- Backtest overfitting: a practical guide
- Walk-forward validation vs cross-validation
- Backtest validation tool: what it should check
- Probability of backtest overfitting (PBO)
- Trading strategy audit service
- Trading strategy robustness tests
- Monte Carlo backtests
- Look-ahead bias in backtests
- Survivorship bias in backtests
- Data snooping in trading
- Out-of-sample testing in trading
- What is backtest overfitting?
- Deflated Sharpe ratio calculator
- Fidelity Lab
Educational content, not investment advice. Updated 2026-10-28.