Backtesting

How to Backtest an EA in MT5

Run an Expert Advisor through MetaTrader 5's Strategy Tester: choosing the modelling mode, costs and period, reading the report, and the mistakes that make a backtest lie.

By the AURAX teamPublished 5 min read

On this page (6)

To backtest an EA in MT5, open the Strategy Tester (Ctrl+R), pick the EA, symbol, timeframe and date range, choose a modelling mode — "Every tick based on real ticks" is the most realistic — set the deposit and leverage, and press Start. The hard part is not running the test; it is making sure the result means something. A backtest with unrealistic costs, a short or calm period, or parameters tuned to that period will look far better than live trading ever will.

Part of the MT5 Expert Advisor guide.

Setting up a test

  1. Open the Strategy Tester

    View → Strategy Tester, or Ctrl+R. Choose Single test on the Overview if it asks.

  2. Choose the EA, symbol and timeframe

    Use the broker's exact gold symbol. Many EAs have their own internal timing, but indicators still use the timeframe you set.

  3. Set the date range

    Long enough to include different conditions — calm ranges, strong trends, news shocks. A few quiet months tell you almost nothing.

  4. Pick the modelling mode

    Covered below. For anything that trades intrabar or manages positions tick by tick, use real ticks.

  5. Deposit, leverage and delays

    Match what you will trade live. The Delays option can add execution latency, which is more honest than the default ideal execution.

  6. Inputs

    On the Inputs tab, load the settings (a .set file) you intend to use live, then Start.

Modelling modes

The modelling mode decides what prices the EA sees during the test. MetaQuotes' own help describes them roughly as follows:

ModeWhat the EA seesUse it for
Every tick based on real ticksThe broker's recorded tick history, with its real spreadsFinal testing — the closest to live conditions
Every tickTicks generated from minute barsMost EAs when real ticks are unavailable
1 minute OHLCFour prices per minute barQuick checks of logic that works on bar closes
Open prices onlyOne price per bar of the test timeframeEAs that only act on a new bar — fast optimisation
Math calculationsNo prices at allPure calculations, not trading tests

Getting the costs right

In MT5 the spread comes from the historical data the test uses: real tick data carries the spread the broker actually quoted; generated ticks use the spread stored with the minute bars. Also check:

  • Commission — on raw-spread accounts it is charged separately and must be part of the test (the symbol settings of the tester account determine it).
  • Swap — charged on positions held overnight. Strategies that hold baskets for days pay it every night.
  • Slippage — enable delays; a fill at the exact requested price on every news spike is not realistic.

Why it matters is covered in XAUUSD spread explained: the more often a strategy trades and the smaller its targets, the larger the share of profit the costs take.

Reading the report

FigureWhat it measuresWatch for
Total net profitEnd balance minus depositMeaningless on its own — compare with the drawdown it took
Equity drawdown maximal / relativeLargest fall of equity from a peak, in money and %The number that decides whether an account survives. See trading drawdown
Profit factorGross profit ÷ gross lossVery high values often mean the losses are still ahead
Recovery factorNet profit ÷ maximal drawdownHow much the strategy earned per unit of pain
Expected payoffAverage result per tradeCompare with the spread per trade
Largest loss tradeWorst single closeFor grids, look at basket closes, not single positions

How backtests lie

  1. Overfitting. Optimise enough parameters over one period and some combination will look excellent by chance. Use the Forward option to hold back part of the period and test the chosen settings on data the optimiser never saw.
  2. Cherry-picked periods. A test that starts after the last crash has no crash in it.
  3. Survivorship. Showing the one setting that worked out of hundreds tried.
  4. Ideal execution. No delays, no slippage, and fills during the minutes the spread was widest.
  5. Ignoring the tail. A strategy that wins 99 times and loses the account once has a great win rate and a terrible outcome.

After the tester, run the EA on a demo account in real time for weeks, and compare the fills, spreads and results with what the test predicted. Differences there are cheap lessons.

From backtest to live

A good backtest earns a strategy the right to a forward test, not the right to real money. The order that protects capital: tester with realistic costs → demo in real time → small live allocation → size up only if live matches expectations.

Sources

Educational content, not financial advice. Trading gold and other leveraged products carries a high level of risk, including the possible loss of some or all of your capital. Automated strategies do not guarantee profit, and past or simulated performance is not indicative of future results. Risk disclosure.