Home › Learn › Automation

Automation

Backtesting an MT5 Expert Advisor

Tick data, honest costs, walk-forward, and the metrics that matter.

By Zubare Khan · 15 November 2026 · 10 min read

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:

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:

MetricWhat it tells youCommon trap
Number of tradesWhether the sample means anythingUnder ~200, conclusions are weak regardless of how good they look
Maximum drawdownWhat you must be able to sit throughRelative (%) and absolute ($) differ; read the one that matches your sizing
Profit factorGross win ÷ gross lossAbove about 2.5 on a small sample usually means a data or cost error, not a great system
Largest single winConcentration of the resultRemove it. If the strategy is now unprofitable, you have one lucky trade, not a strategy
Longest losing streakWhat you must survive emotionally and financiallyWhatever the backtest shows, assume live will be longer
Recovery factorNet profit ÷ max drawdownBelow 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:

A sequence worth following

  1. Verify history depth and that real tick data actually covers your test window.
  2. Set realistic spread for your trading hours, plus commission. Then run everything again at double.
  3. Test on the full window with default parameters first, before optimising anything. If it is hopeless here, optimisation will only hide that.
  4. Optimise a small number of parameters. Inspect the surface for a plateau.
  5. Walk forward across the whole history. Keep these results, discard the in-sample ones.
  6. Test on a second broker's data. Large divergence means you fitted one feed's quirks.
  7. Forward test on demo for a month minimum, on the broker and VPS you will actually use.
  8. 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

Risk notice. This article is educational and is not financial advice. Trading leveraged products carries a substantial risk of loss and is not suitable for everyone. Past performance and backtested results do not predict future results. Broker and prop-firm links on this site are affiliate links — see the risk disclaimer.