How to backtest an Expert Advisor in the MT5 Strategy Tester without fooling yourself
To backtest an Expert Advisor honestly in MT5, test with "Every tick based on real ticks", include realistic spread, commission and execution delays, and keep part of the history out of optimization as a forward test. Judge the result by maximum drawdown, recovery factor, profit factor and number of trades together, then run the EA on a demo account before going live.
How do you backtest an EA without fooling yourself?
To backtest an Expert Advisor (EA) honestly in the MetaTrader 5 (MT5) Strategy Tester, test on real tick data, include realistic trading costs, and keep part of the history hidden from optimization. Then judge the report by drawdown, recovery factor and trade count, not by net profit alone. Finally, run the EA on a demo account before trusting it with real money.
A backtest is a simulation of how an EA would have traded on past prices. Most misleading backtests come from poor price modeling, missing costs, or settings tuned until they fit the past perfectly.
Which modeling mode should you use?
The modeling mode decides how the MT5 Strategy Tester builds price movement inside each bar. It has the biggest effect on accuracy and speed. You pick it in the Modelling drop-down on the tester's Settings tab.
| Modeling mode | What it simulates | Speed | Best use |
|---|---|---|---|
| Every tick based on real ticks | The broker's recorded tick history, including real spreads | Slowest | Final validation, EAs with tight stops, pending orders or scalping logic |
| Every tick | Ticks generated from 1-minute bars by an algorithm | Slow | When real tick history is missing or short |
| 1 minute OHLC | Only the open, high, low and close of each 1-minute bar | Fast | Early optimization of EAs that act on bar close |
| Open prices only | Only the open price of each bar on the test timeframe | Fastest | Rough optimization of EAs that only trade at a new bar |
| Math calculations | No price data at all | Instant | Testing pure calculations, not trading |
"Every tick based on real ticks" is the only mode that uses the broker's actual price sequence. The other modes guess what happened inside a bar. When a bar touches both the stop loss and the take profit, the guess decides which one filled first, and that can turn a loss into a win.
"Open prices only" is safe only if the EA makes all its decisions at the first tick of a new bar. After testing, check the History Quality line in the report; low quality means gaps in the data.
How do you model spread, commission and slippage?
Trading costs are small per trade but large across hundreds of trades. A backtest that ignores them almost always looks better than live trading.
Spread
With "Every tick based on real ticks", the tester uses the spread recorded in the tick history. In the generated-tick modes, spread comes from the bar history. If you want to test a wider spread than the history shows, one option is a custom symbol with edited data. Test at spreads at least as wide as your live broker's typical spread.
Commission
Many ECN-style accounts charge a commission per lot on top of the spread. Check the Deals tab of the backtest report to confirm that commission appears on each deal. If it does not, add it to your analysis, or test on a custom symbol with commission configured.
Slippage and execution delay
Slippage is the difference between the price you asked for and the price you got. The Delays setting in the tester lets you choose "Zero latency, ideal execution", a fixed delay, or a random delay. Zero latency is the least realistic choice. Use a delay close to your real ping to the broker's server so stop orders fill at more honest prices.
How do you optimize without overfitting?
Optimization means running the EA many times with different input values to find the best set. The MT5 Strategy Tester offers a "Slow complete algorithm" that tries every combination and a "Fast genetic based algorithm" that searches large ranges more quickly.
Overfitting, also called curve fitting, means the settings match the noise in past data instead of a real, repeatable pattern. An overfit EA looks excellent in the test period and fails as soon as prices behave a little differently. The more inputs you optimize and the finer the steps, the higher the risk.
Some simple rules help reduce overfitting:
- Optimize as few inputs as possible, with coarse steps.
- Prefer a flat "plateau" of good results over one sharp peak. If nearby values fail, the peak is probably luck.
- Choose the optimization criterion with care. "Balance max" rewards profit alone; criteria such as "Recovery Factor max" or a custom score from
OnTester()also account for drawdown. - Distrust any result built on very few trades.
A custom criterion lets you score each run your own way. The OnTester() handler below returns net profit divided by maximum equity drawdown, and scores runs with too few trades as zero. Select "Custom max" as the optimization criterion to use it.
input int InpMinTrades = 100; // minimum trades for a run to count
double OnTester()
{
double profit = TesterStatistics(STAT_PROFIT);
double drawdown = TesterStatistics(STAT_EQUITY_DD);
double trades = TesterStatistics(STAT_TRADES);
if(trades < InpMinTrades || drawdown <= 0.0)
return(0.0);
return(profit / drawdown);
}
How do you test on out-of-sample data?
Out-of-sample data is price history the EA was not tuned on. It is the closest a backtest can get to the future. The MT5 Strategy Tester supports this directly with the Forward setting, which can split the date range into a back part and a forward part (1/2, 1/3, 1/4 or a custom date).
With Forward enabled, the tester optimizes on the back period and then runs the results on the forward period. The Forward Results tab shows how each parameter set did on data it never saw. A set that is strong in both periods is more trustworthy than the top result of the back period.
Walk-forward thinking
Walk-forward testing repeats that idea in steps: optimize on one window, test on the next, move both windows forward, and repeat. MT5 has no built-in rolling walk-forward mode, so you run the steps manually by changing dates. It shows whether the EA's edge survives re-tuning over time.
How do you read the Strategy Tester report?
The Strategy Tester report shows many numbers. Read these together, because each one hides something on its own.
- Total net profit: the final result after costs. It says nothing about risk.
- Profit factor: gross profit divided by gross loss. Above 1.0 means the EA made money. Very high values on few trades are a warning sign.
- Maximum drawdown: the largest drop from a peak. MT5 shows it for balance and for equity. Equity drawdown includes open losses and is the more honest number.
- Recovery factor: net profit divided by maximum drawdown. It shows how much profit the EA earned for the pain it caused.
- Total trades: the sample size. A handful of trades cannot prove anything, however good the other numbers look.
Also look at the balance and equity graph. A smooth curve that suddenly drops, or an equity line that dips far below the balance line, can reveal open losing trades held for a long time. Grid and martingale EAs often show this pattern.
What should you do before going live?
A good backtest is a filter, not a guarantee. Before trading real money, run the EA on a demo account, or a small live account, for long enough to see a realistic number of trades. Compare the fills, spreads and results with the backtest over the same period.
If live results differ a lot from the tester, find out why before scaling up. Sizing each trade from a fixed risk, as described in risk-based position sizing in MQL5, also keeps drawdowns comparable between the test and live trading.
Summary
An honest MT5 backtest uses "Every tick based on real ticks", realistic spread, commission and delays, a small number of optimized inputs, and a forward period the EA never saw. Judge the result by drawdown, recovery factor, profit factor and trade count together, then confirm it on a demo account. If you need an EA built or tested this way, see the MT4/MT5 Expert Advisor development service, or start with what an Expert Advisor is.
Need this built? See MT4 / MT5 Expert Advisors or get in touch.
By