How to Backtest in MetaTrader 5 With the Strategy Tester

Backtesting is running a fixed set of trading rules over past price data to see how those rules would have behaved. In MetaTrader 5 you do it in the Strategy Tester, which reports profit factor, expected payoff and drawdown for the period you choose.

Module 14 · Lesson 2 of 5

What you'll learn

New to the platform? Start with how to use MetaTrader 5.

What is backtesting?

Backtesting is running a fixed set of trading rules over past price data to see how those rules would have behaved. It answers one precise question: what did these exact rules do on this exact history? That makes it the fastest way to filter a rule set before you spend time on a demo forward test.

A backtest needs rules precise enough for a computer to follow, which is why the trading rules lesson comes first. In MetaTrader 5 the rules live inside an Expert Advisor (EA), a program that places and manages trades, covered in the Expert Advisors lesson. If you trade by hand, you can still backtest by replaying past candles in visual mode, as shown below.

How do you open the MT5 Strategy Tester?

You open the MT5 Strategy Tester by pressing Ctrl+R, or by right-clicking an Expert Advisor in the Navigator window and choosing Test. MetaQuotes lists Ctrl+R as the shortcut that opens and closes the Tester window. Choosing Test loads that EA into the tester.

How do you set up a backtest in the MT5 Strategy Tester?

To set up a backtest in the MT5 Strategy Tester, choose the Expert Advisor, symbol, timeframe, dates, modelling mode, deposit and leverage, then press Start. Each setting changes the result, so write all of them down with every test, or you cannot repeat or compare runs later.

Setting What it controls
Expert The EA whose rules are tested
Symbol and timeframe The instrument and chart period the EA runs on
Date A preset period or a custom start and end date
Forward Splits off a later part of the dates for a forward test (No, 1/2, 1/3, 1/4 or Custom)
Delays No delay, random delay or a fixed delay between an order and its fill
Modelling How the tester builds prices inside each bar
Deposit and leverage The starting balance and the leverage used for margin
Visualization Replays the test on a chart so you can watch each trade; off during optimisation

Leverage in the tester changes how much margin each position ties up, not what each price move is worth. The margin calculator shows the margin a position needs.

What are the MT5 modelling modes?

The modelling mode decides which prices the tester feeds the EA inside each bar, trading accuracy against speed. MetaQuotes' testing help page lists four history-based modes, plus a Math calculations mode that loads no price history.

Mode What the tester uses Accuracy and speed
Every tick based on real ticks Your broker's recorded ticks Closest to real conditions; the first run can be slow while ticks download
Every tick Every tick, simulated by the tester Described by MetaQuotes as the most accurate but the slowest mode
1 minute OHLC Open, high, low and close of each minute bar Faster; misses moves inside each minute
Open prices only The open price of each bar Fastest; ignores every move inside the bar

How do you backtest a manual strategy in MT5?

To backtest a manual strategy in MT5, run the Strategy Tester in visual mode on an indicator, not an Expert Advisor, so the chart replays past candles one by one. Slow it with the speed slider, pause at each setup, write down the entry, stop and target, then resume and record how each trade would have ended.

Most traders who place trades by hand have no EA, and they do not need one to test their rules on history. Visual testing turns on automatically when you test an indicator, and the replay shows only the candles up to the current moment, so you cannot see what comes next.

  1. Write the rules first. Entry signal, stop, target and when you skip a trade, as in writing trading rules. Rules you decide on the spot cannot be tested.
  2. Set up an indicator test. Press Ctrl+R, switch the tester from Expert to Indicator, and choose an indicator your rules use, such as Moving Average from the Examples folder. Pick the symbol, the timeframe and a custom date range. The replay chart's timeframe cannot be changed once it starts, so choose it here.
  3. Choose the modelling. Use Every tick based on real ticks, or 1 minute OHLC for a faster replay of higher timeframes.
  4. Start the replay. Click Start. The visualization window opens and candles begin to form from the first date.
  5. Control the speed. Drag the speed slider down so candles form slowly enough to read. Pause when a setup appears; to move forward a candle or two at a time, resume at the lowest speed and pause again.
  6. Record each trade. While paused, note the time, direction, entry, stop and target. The Data Window (Ctrl+D) shows the open, high, low and close of the candle under your cursor, so you can read exact prices. Resume and note whether the stop or the target was hit first.
  7. Total the results. After 30 or more setups, convert each result into R, the multiple of the amount risked, and work out the average.
Trade Direction Stop distance Result In R
1 Buy EURUSD H1 20 pips Target hit, +40 pips +2.0R
2 Sell EURUSD H1 20 pips Stop hit, −20 pips −1.0R
3 Buy EURUSD H1 20 pips Closed by rule, +30 pips +1.5R
Total +50 pips +2.5R, or 0.83R a trade

Each R is the result divided by the stop distance: 40 ÷ 20 = 2.0, −20 ÷ 20 = −1.0 and 30 ÷ 20 = 1.5. The total is 2.0 − 1.0 + 1.5 = 2.5R, and 2.5R ÷ 3 ≈ 0.83R a trade. Three trades say very little; the same arithmetic over 30 to 100 setups gives a figure worth comparing. The expectancy calculator turns a win rate and average win and loss into the same figure, and a trading journal keeps the log.

A replay like this produces your notes rather than a tester report. To get a full report from a manual strategy, run an EA that adds Buy and Sell buttons to the visual chart, so the tester records each trade you click; free examples of such tools are shared on the MQL5 site.

How do you read a Strategy Tester report?

You read a Strategy Tester report by starting from gross profit and gross loss, because almost every other figure is built from them. Then check drawdown, which shows how far the account fell along the way. MetaQuotes' report help page defines the main fields:

A synthetic MT5 Strategy Tester report showing gross profit, gross loss, profit factor 1.75, expected payoff and two drawdowns, beside a chart where the profit factor falls from 1.75 on the tuned period to 0.78 on a held-back period
  1. Gross profit $4,312 minus gross loss $2,465 is net $1,847; $4,312 ÷ $2,465 gives a profit factor of 1.75.
  2. Expected payoff is $1,847 ÷ 150 trades = $12.31 a trade.
  3. Equity drawdown ($1,190) is nearly double balance drawdown ($612), because open trades sank further.
  4. The same settings on a held-back period: 48 trades, net −$392, profit factor 0.78.

Read your own reports the same way, and log each test's settings and figures in your trading journal.

How do you calculate the results of a backtest?

Profit factor = gross profit ÷ gross loss, and expected payoff = net profit ÷ number of trades. Both come straight from the report's totals, so you can check them by hand. Working them out on a simple synthetic test shows how sensitive both numbers are to costs.

Worked example: a synthetic 100-trade test

A test produces 100 trades: 45 wins averaging $60 and 55 losses averaging $40.

Now suppose the test understated costs by $3 a trade, for example a wider spread or a missing commission. Each win shrinks to $57 and each loss grows to $43:

As tested With $3 extra cost per trade
Gross profit $2,700 $2,565
Gross loss $2,200 $2,365
Net profit $500 $200
Profit factor 1.23 1.08
Expected payoff $5.00 $2.00

Three dollars a trade removed 60% of the net result.

Why do costs matter in a backtest?

Costs matter because a small cost on every trade adds up across hundreds of trades, so a test that understates costs overstates the result. Spread, commission and swap are explained in the spread, commission and swap lesson. If a small extra cost per trade turns a result negative, the test had little room for error.

The tester's advanced settings let you charge a commission after each trade, so set it to match your account's contract specification.

What is overfitting in backtesting?

Overfitting is tuning a strategy's settings until they fit past data so closely that they describe that history rather than a repeatable pattern. MetaQuotes defines optimisation as many runs of an EA on history with different sets of inputs. With enough runs, some setting will look good by chance alone.

In-sample, out-of-sample and forward testing

How do you validate a backtest?

You validate a backtest by running the chosen settings, unchanged, on a period they were not tuned on, then forward testing them on a demo account and comparing all three reports. When profit factor, expected payoff and equity drawdown stay in a similar range across them, the rules are capturing a repeatable pattern rather than one stretch of history.

  1. Optimise on the in-sample period and pick one set of inputs.
  2. Run that set once on the out-of-sample period, or use the Forward setting to do it for you.
  3. Run the same inputs on a demo account with live prices and real fills.
  4. Compare the three reports side by side, using the same fields each time.

How do you make a backtest more realistic?

You make a backtest more realistic by using real-tick modelling, charging your account's real costs, adding order delays, testing across different market conditions and collecting enough trades. Each step closes a gap between how the tester prices a trade and how a live account fills it, so the report you read is closer to live trading.

Try it on demo

On a demo account in MetaTrader 5, press Ctrl+R and test any Expert Advisor from your Navigator on one symbol and timeframe over one year, using "Every tick based on real ticks". Write down the profit factor, expected payoff and equity drawdown. Then run the same settings on the year before, unchanged, and compare.

This lesson is part of the Trading Automation module.

—
—
—

—
—

—
—
—
—
—
—

1.10000

Quick check

  1. In the MT5 report, a profit factor of 1.00 means
  2. A test shows gross profit $2,700 and gross loss $2,200 over 100 trades. The expected payoff is
  3. Adding $3 of extra cost to each of those 100 trades changes the net result to
  4. Overfitting is
  5. An out-of-sample test means running

Frequently asked questions

Where is the Strategy Tester in MT5?

The Strategy Tester opens with Ctrl+R, which shows or hides the Tester window in MetaTrader 5. You can also right-click an Expert Advisor in the Navigator window and choose Test.

Does backtesting work?

Yes, for the job it is built for: showing how fixed rules behaved on past data and filtering out rules that fail even there. Pair it with an out-of-sample test and a demo forward test to see how the rules hold up on data they were not tuned on.

What is a good profit factor in backtesting?

Above 1.00 means gross profit exceeded gross loss on that history. Compare the figure after costs and on a held-back period: a profit factor that holds up there tells you more than a high one on tuned data.

How many trades do you need for a backtest?

Aim for enough trades to cover trending, ranging and volatile periods. More trades across different kinds of market give a steadier picture than a few dozen from one kind of market.

What is the difference between backtesting and forward testing?

Backtesting runs rules on past data. Forward testing runs the chosen settings on data they were not tuned on: a held-back later period in the tester, or live prices on a demo account.

Sources

Practise what you learned

Open an account to use MetaTrader 5. Start on a demo account before risking real money.

Open Account