How to backtest a trading strategy - MarketsAll Trading Strategies cover

How to Backtest a Trading Strategy

How to backtest a trading strategy: the six steps, why the rules must be fixed before the data is examined, what costs to include, how large a sample is meaningful, and the four ways a backtest lies.

Backtesting is applying a strategy's rules to historical data to see what it would have produced. Done carefully it is the cheapest way to discard an idea that does not work. Done carelessly it is an elaborate way to confirm what you already believed, because with enough adjustment any set of rules can be made to fit any past.

Key Takeaways

  • Fix the rules before examining the data. Every adjustment made after seeing results is fitting, not testing.
  • Include costs: spread, swap, commission and a slippage allowance. A backtest without them is not a result.
  • Sample size matters more than the result. A few dozen trades tells you almost nothing.
  • Hold back a portion of the data, and follow with forward-testing on demo before live money.

The Six Steps

1. Write the rules completely. All five components from trading strategies: market and timeframe, entry, stop, target, size. Specific enough that two people would take the same trades. If a rule needs judgement, the test measures your judgement, not the rule.

2. Choose the data. A period long enough to include trending, ranging and volatile conditions — for a daily-chart approach that means years, for an intraday one months. Testing a trend strategy only on a trending year proves nothing.

3. Split the data. Use part for development and hold the rest back. Rules tuned on the first part are then run once on the second. If they fall apart, they were fitted to the first.

4. Run the test. Manually, bar by bar, or with the MetaTrader 5 Strategy Tester for rule-based systems. Record every trade: date, entry, stop, target, exit, result in R.

5. Add the costs. Spread on every trade, swap on every night held, commission where it applies, and a slippage allowance on stop exits — a pip or two on majors, more elsewhere. See slippage.

6. Read the right numbers. Not the total return. Number of trades, win rate, average win and loss in R, expectancy per trade, maximum drawdown, and the longest losing streak — the number that tells you whether the approach is holdable.

Manual or Automated

The MT5 Strategy Tester runs coded expert advisors over historical data and handles cost modelling and optimisation. It requires the rules to be coded, which is only possible for fully mechanical approaches.

Manual bar-by-bar testing works for discretionary approaches such as price action. It is slow and it has a specific weakness: the tester can see the next bar. The discipline of hiding the right side of the chart and advancing one bar at a time is the whole method.

The Four Ways a Backtest Lies

Overfitting. Adding rules and adjusting parameters until the past looks good. A strategy with eight conditions tuned on three years of data describes those three years; it says nothing about the next one. Fewer rules, chosen for a reason, generalise better.

Hindsight. Marking levels on a chart where the outcome is visible. Every level looks obvious after the fact. This is why the held-back data and bar-by-bar advancement matter.

Ignoring costs. A strategy with a 5-pip average edge is profitable in a costless backtest and negative in reality — see scalping strategy for how quickly the arithmetic changes.

Survivorship and data quality. Testing on instruments that still exist and on tick data that does not reflect real spreads at the time. Historical spread data is rarely accurate; assume it was worse than the record shows.

Sample Size

TradesWhat it tells you
Under 30Essentially nothing
30–100A rough sense of direction; wide error bars
100–300Meaningful, if it spans different conditions
300+Reasonable confidence in the distribution

A 60% win rate over 20 trades and a 45% win rate over 20 trades are statistically indistinguishable. The number of trades matters more than the result they produced.

Worked Example: Before and After Costs

A 4-hour breakout strategy, 180 trades over three years, 1.0 lot EURUSD, average win 45 pips, average loss 22 pips, 38% win rate.

CalculationAmount
Gross wins68 × 45 × $10+$30,600
Gross losses112 × 22 × $10−$24,640
Gross result+$5,960
Spread180 × 0.9 × $10−$1,620
Slippage allowance112 losing exits × 1 pip × $10−$1,120
Swap (avg 2 nights per trade)180 × 2 × $3.50−$1,260
Net+$1,960

Costs took two-thirds of the gross. That result is still positive, but a strategy with a thinner edge would not have survived the same deduction — and this is the calculation most backtests skip.

(Illustrative figures; swap rate assumed.)

After the Backtest

A backtest that survives is a reason to forward-test, not to trade live. Run the rules on a demo account at the position size you will actually use, keeping a journal with the compliance column from the first trade. Demo execution is cleaner than live, so subtract a further slippage allowance from the result.

Key Risks

  • Treating a backtest as a forecast. It describes one past.
  • Re-optimising after live losses. That is a new strategy with no track record.
  • Testing during only one regime.
  • Confusing a good total return with a survivable one. Maximum drawdown and the longest losing streak decide whether it can be traded.

How much data do I need?

Enough to include trending, ranging and volatile periods, and enough trades for the sample to mean something — usually well over 100.

Can I backtest a discretionary strategy?

Manually, bar by bar, with the right side of the chart hidden. It is slower and less rigorous than testing mechanical rules, and it is the only option for discretionary approaches.

What is overfitting?

Tuning rules until they fit past data closely. The result describes the sample rather than the market, and it does not repeat.

Does the MT5 Strategy Tester include spreads?

It models costs based on the settings and data provided. Historical spread data is imperfect, so treat modelled costs as optimistic.

Is a backtest enough to start trading live?

No. Forward-testing on demo with the intended position size, and a journal, is the step between a backtest and live money.

Related Guides

Trading strategies · Price action trading · Scalping strategy · Drawdown · Risk-to-reward ratio · Demo vs live accounts · How to keep a trading journal

Put this into practice

Open an account with MarketsAll and trade spot FX and CFDs on MetaTrader 5, with the spreads and account types set out on our account types page.

Register