Backtesting & optimization in MetaTrader
Optimization, walk-forward and curve-fitting
6 min
The Strategy Tester can optimize — run the EA across many combinations of input parameters and rank them by a chosen metric. This is powerful and is also where most strategies are quietly destroyed.
How optimization works
You give each input a start, step and stop (e.g. FastPeriod from 5 to 50 in steps of 5) and the tester runs every combination, reporting the best by profit, drawdown, profit factor or a custom criterion. MT5 offers a fast genetic algorithm so you do not have to brute-force enormous spaces.
The curve-fitting danger
This is the same overfitting trap from the Python chapter, and the tester makes it dangerously easy. If you keep adding parameters and search hard enough, you will find a combination that produced a gorgeous historical equity curve — by fitting the noise, not a real edge. That combination almost always falls apart on new data.
Warning signs: a result that is wildly better than its neighbours (a fragile spike), many parameters, and a strategy whose performance collapses if a parameter moves one step. Robust parameters sit on a broad plateau of good results, not a lonely peak.
Walk-forward analysis
The serious defence is walk-forward optimization:
- Optimize on an in-sample window (e.g. 2018–2020).
- Test the chosen parameters, untouched, on the following out-of-sample window (e.g. 2021).
- Roll both windows forward and repeat.
The out-of-sample results — on data never used to choose parameters — are the only ones that approximate live behaviour. MT5's tester has a built-in forward mode that splits the period for exactly this.
The honest expectation
Even a clean walk-forward result is hypothetical and will degrade live. Treat optimization as a way to find robust parameters, never the best-ever ones. A slightly worse but stable strategy beats a spectacular but fragile one every time.
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.