Backtesting an MT5 Expert Advisor
Tick data, honest costs, walk-forward, and the metrics that matter.
A backtest is not evidence that a strategy works. It is evidence that a strategy would have worked, on one data set, under assumptions you chose. The gap between those two statements is where almost every disappointing EA lives.
This is a practical guide to closing that gap in MetaTrader 5. None of it will make a bad strategy good. All of it will stop a bad strategy from looking good.
Start with the data, because nothing else matters if it is wrong
MT5's Strategy Tester builds its test bars from whatever history your broker's server supplies. That history varies enormously between brokers — different depth, different gap handling, different treatment of the weekend, and on gold, materially different prices during thin hours.
Two things to check before you trust a single result:
- Modelling quality. "Every tick based on real ticks" is the only mode worth using for anything that trades intrabar. "Open prices only" is useful solely for a first sanity check that the code runs. "1 minute OHLC" sits in between and will flatter any strategy whose entry and stop could both be touched inside one bar — which includes every breakout system.
- How much real history exists. Many brokers hold only one to three years of real tick data. Beyond that, MT5 synthesises ticks from minute bars without telling you loudly. A ten-year test on three years of real ticks is seven years of fiction.
The intrabar problem deserves a sentence of its own. If a candle's high touched your entry and its low touched your stop, the tester has to decide which happened first. Without real ticks it guesses, and the guess is frequently generous. For a strategy with stops close to entries, this one issue can invent the entire edge.
Costs: the three settings that turn profit into loss
Spread. The tester defaults to "Current" — the spread at the moment you press start, applied to the whole test. If you set it up at 15:00 UTC with gold at 18 cents, you have just backtested a world in which the Asian session has an 18-cent spread. Use real-tick spread where your data supports it; otherwise set a fixed spread at or above the realistic average for the hours your strategy actually trades.
Commission. Not included by default on most setups. You must set it in the tester configuration or inside the EA. For a strategy taking one trade a day this is a rounding error; for one taking twenty it is often the difference between a profitable system and a broker-funding operation.
Swap. Only matters if you hold overnight, but if you do, it matters a great deal and the tester uses the current swap rates applied retroactively to the whole period.
Here is the test that saves the most time: double your assumed costs and run it again. If the strategy survives at twice the realistic spread and commission, it has a real margin. If it collapses, you were measuring your cost assumptions, not your edge.
Optimisation and the way it lies to you
The Strategy Tester will happily search thousands of parameter combinations and hand you the best one. That number is close to meaningless on its own, for a simple reason: with enough parameters and enough combinations, something will fit the noise in your sample. It always does.
Three defences, in increasing order of effort:
Look at the parameter surface, not the peak
Sort the optimisation results by parameter value rather than by profit. You want a broad plateau — a region where neighbouring values all produce similar, decent results. A lone spike surrounded by losses is a curve fit, and the live market will not land you on the spike.
If the best setting is 23 and settings 22 and 24 both lose money, the correct conclusion is that this parameter has no stable optimum, not that 23 is special.
Count your degrees of freedom
Every parameter you optimise consumes some of your sample's ability to tell you anything. A rough working rule is to want at least 30 trades per optimised parameter, and preferably far more. Optimising six parameters on 150 trades is not research; it is drawing a curve through scattered dots.
Walk forward
This is the one that actually works. Split your history into consecutive blocks. Optimise on block 1, test on block 2 without touching the settings. Then optimise on block 2, test on block 3. And so on. What you end up with is a series of genuinely out-of-sample results produced by the process you would actually follow in live trading, which is the thing you want to evaluate.
Walk-forward results are almost always much worse than in-sample optimisation results. That is not the method failing. That is the method telling you the truth for the first time.
Reading the report properly
The headline net profit tells you almost nothing. These tell you more:
| Metric | What it tells you | Common trap |
|---|---|---|
| Number of trades | Whether the sample means anything | Under ~200, conclusions are weak regardless of how good they look |
| Maximum drawdown | What you must be able to sit through | Relative (%) and absolute ($) differ; read the one that matches your sizing |
| Profit factor | Gross win ÷ gross loss | Above about 2.5 on a small sample usually means a data or cost error, not a great system |
| Largest single win | Concentration of the result | Remove it. If the strategy is now unprofitable, you have one lucky trade, not a strategy |
| Longest losing streak | What you must survive emotionally and financially | Whatever the backtest shows, assume live will be longer |
| Recovery factor | Net profit ÷ max drawdown | Below 2 over a multi-year test is usually not worth the stress |
Then look at the equity curve itself, not the summary. You are looking for whether the profit arrived steadily or in one regime. A strategy that made everything during one trending quarter and went sideways for two years is a bet on that regime returning — which may be a reasonable bet, but it is a different bet from the one the summary implies.
The things a backtest structurally cannot see
Even a perfect backtest omits a category of problems entirely, because the tester is a clean simulation:
- Slippage on stops. The tester fills your stop at your stop price. Reality fills it at the next available price, which during a release can be far away.
- Requotes and rejections. They do not exist in the tester.
- Connection loss and VPS restarts. A strategy that must manage an open position every minute behaves very differently when it is offline for ten.
- Broker-side changes. Spread widening around news, leverage cuts before major events, symbol specification changes, holiday sessions.
- Server time shifts. If your logic is time-based, the daylight-saving transitions move your session windows twice a year, and Europe and the US do not switch on the same dates. The tester applies whatever offset the historical data carries, which may not be what your live server will do.
- Survivorship in your own process. You have already discarded the versions that did not work. The surviving version looks better than your search process really was.
A sequence worth following
- Verify history depth and that real tick data actually covers your test window.
- Set realistic spread for your trading hours, plus commission. Then run everything again at double.
- Test on the full window with default parameters first, before optimising anything. If it is hopeless here, optimisation will only hide that.
- Optimise a small number of parameters. Inspect the surface for a plateau.
- Walk forward across the whole history. Keep these results, discard the in-sample ones.
- Test on a second broker's data. Large divergence means you fitted one feed's quirks.
- Forward test on demo for a month minimum, on the broker and VPS you will actually use.
- Go live at the smallest size the broker permits, and compare live fills against the same period's backtest. The difference is your real-world cost, and it is the only honest number in this entire process.
A strategy that looks mediocre in a conservative backtest and holds up in walk-forward is worth far more than one that looks spectacular in an optimised one. The first might survive contact with a live account. The second has already told you it will not.
Read next
- Drawdown: What the Equity Curve Doesn't Tell YouLosing streaks are longer than intuition, and they all look the same from inside.
- How to Pass a Prop Firm ChallengeThe evaluation is a constraint problem before it is a trading problem.
- Why the Tokyo Session Range BreaksWhat the Asian range actually is, and why London moves through it.