Skip to content
PipsMorrow

Backtesting trading strategies means taking a fixed set of rules and replaying them on past price data to see what they would have done. Done well, it is the cheapest way to find out whether an idea deserves real money. Done badly, it produces a lovely equity curve that disappears the day you go live.

This article answers what backtesting in trading is, then walks through how to backtest a trading strategy in six steps. It spends most of its time on the ways a test lies to you, because those are what separate a useful result from a flattering one.

Key takeaways

  • Backtesting replays written, mechanical rules on historical prices and records every trade in R (one R is the amount you risk per trade). It tests whether the rules had an edge, not whether you can trade them.
  • Write every rule before you look at results, then split your data. Build on the first part (in-sample) and judge on a part the rules have never seen (out-of-sample).
  • Try 20 strategies with no edge on 100 trades each, and the best one wins about 59% of the time. Picking the winner from many tries mostly finds luck.
  • Costs decide many results. With a $3.00 stop on gold, a $0.30 spread costs 0.10R per trade, enough to turn a +0.08R system into a −0.02R one.
  • A short sample says little. After 30 trades the expectancy is uncertain by about ±0.36R, after 100 by ±0.20R, and after 400 by ±0.10R.
  • A passed backtest earns a forward test on a demo or tiny live account. It never earns full size.

What backtesting in trading actually is

A backtest is a simulation. You write down exactly when a strategy enters, where it places the stop loss (the price where the idea is proven wrong), where it takes profit, and how large each position is. Then you apply those rules to old charts, one candle at a time, and log what would have happened.

The output is a list of trades. From that list you can compute the win rate, the average win and loss, the expectancy (average result per trade) and the worst drawdown (the fall from a peak in your balance to the next low). If those words are new, the lesson on the metrics that actually matter defines each one.

There are two ways to do it. In a manual backtest you scroll a chart forward candle by candle and note each trade by hand. In an automated backtest a program applies coded rules to the whole history in seconds. Manual testing is slow but forces you to see each setup. Automated testing is fast but only as honest as the code. The method below works for both.

One limit to keep in mind from the start. A backtest tests the rules, not you. It cannot show how you would feel during a ten-trade losing run, and it cannot tell you whether the future will resemble the past. It can only tell you whether the idea was ever worth taking seriously.

How to backtest a trading strategy in six steps

The order matters. Most self-deception comes from doing the steps out of sequence, usually by looking at results before the rules are fixed.

Six boxes in a row: write the rules, split the data, run the rules, add real costs, test on unseen data, forward test
Six steps, in order. Rules are locked before any result is seen, and the unseen data is used once.
  1. Write the rules. Entry, stop, target, position size and the market and timeframe. If a rule needs judgment such as “a clean break”, define it with numbers or leave the setup out. The lesson on turning an idea into written rules shows how.
  2. Split the data. Decide now which stretch of history you will build on and which you will keep untouched. A common split is about 70% to build and 30% to judge.
  3. Run the rules on the first part. Log every trade in R. Do not skip trades you dislike in hindsight, because the live version of you will not know they were bad.
  4. Add real costs. Subtract the spread (the gap between the buy and sell price), any commission and the swap (the overnight financing charge) from every trade.
  5. Test once on the unseen data. Run the exact same rules, unchanged, on the part you held back. This is the result that counts.
  6. Forward test. Trade the rules on a demo account or at a very small size for a set number of trades. The lesson on forward testing and the limits of demo covers what this stage can and cannot prove.

Steps 2 and 5 are the ones people skip, and they are the ones that protect you. Without them, you are marking your own homework.

Split your data so the test has an exam

Imagine a student who is given the exam questions to study from. A perfect score proves nothing. A backtest run and tuned on the same data is the same: you will always find settings that fit the past.

The fix is to divide history into two blocks. The in-sample block is where you build and adjust. The out-of-sample block is the exam: you run on it once, with no changes afterward. If you change the rules after seeing the exam result, the exam is spent and you need fresh data.

A ten-year timeline split into years 1 to 7 labeled build and tune, and years 8 to 10 labeled judge once
Seven years to build, three to judge. The unseen block is used one time.

A stricter version is walk-forward testing. You build on a window, test on the next stretch, slide both windows forward and repeat. Every test period is data the rules never touched, and you see whether the result holds across several market conditions instead of one.

Whichever way you split, include different conditions. A system tested only on a long, calm uptrend has not been tested at all against a range or a sudden reversal. Make sure both blocks contain trending, ranging and fast-moving stretches.

Why picking the best of many tests finds luck

Suppose you try 20 variations of an idea: different moving average lengths, different stop sizes, different sessions. You then keep the best. That feels like research. Statistically it is a lottery in which you only show the winning ticket.

Here is a clean way to see it. Take 20 strategies that flip a coin for every trade, each with a 1:1 payoff and a true win rate of exactly 50%. Run each for 100 trades. None has any edge. Yet the best of the 20 will usually look good.

Exact binomial probabilities for coin-flip strategies with a 50% win rate and 1:1 payoff. The median best of 20 is 59 wins.
Result over 100 tradesOne strategy reaches itBest of 20 reaches it
55 wins or more18.4%98.3%
58 wins or more6.7%74.8%
60 wins or more2.8%43.8%
62 wins or more1.1%19.0%
Two bars: a single coin-flip strategy averages 50 wins in 100 trades, the best of 20 coin-flip strategies typically shows 59 wins
Every strategy here has zero edge. The best of 20 still shows 59 wins out of 100.

A 59% win rate at 1:1 is a +0.18R expectancy, which would look excellent in a report. It came from nothing. The same thing happens, more quietly, every time you nudge a setting and re-run the test until the curve looks smooth.

This is overfitting, sometimes called curve fitting: the rules have learned the noise of one data set. Count your tries, because each one raises the bar the final result has to clear. The lesson on over-optimization and curve fitting goes deeper, and the cure is the out-of-sample test above.

Signs a result is overfit

Be suspicious when tiny changes to a setting swing the result a lot, when the rules have many parameters for a small number of trades, or when the best settings sit alone with poor neighbors. A real edge usually survives small changes to the numbers.

Sample size: how many trades is enough

Even an honest test is noisy if it is short. Each trade is a coin flip with a payoff, so the average of a few trades jumps around. The uncertainty shrinks slowly, with the square root of the number of trades.

Rough 95% margin on expectancy ≈ 1.96 ÷ √trades (for a 1:1 payoff, in R)

What each variable means:

  • trades — The number of trades in the test
  • R — The amount risked per trade, for example 1% × $2,400 = $24
A rule of thumb for a 1:1 payoff, where each trade swings about 1R either way. Payoffs with larger wins are noisier still.
Trades in the testExpectancy could be off by aboutIn dollars at $24 risk
30±0.36R±$8.60 per trade
100±0.20R±$4.70 per trade
400±0.10R±$2.35 per trade

Read the middle row. A system that shows +0.08R over 100 trades is inside the noise, because the true figure could sit anywhere from about −0.12R to +0.28R. Treat 100 trades as a minimum for a first look and several hundred as the target. If the strategy rarely trades, you need more years of data, not a looser rule.

You can test how a result behaves under a losing run with the risk of ruin calculator and the drawdown calculator. Both take the win rate and the payoff your backtest produced.

Costs can erase an edge

A frictionless backtest assumes you buy and sell at the middle price with no fee. Real trades pay the spread on entry, and often a commission and a swap on top. These costs are fixed in dollars, so they hurt most when your stop is small.

Take a gold (XAU/USD) system with a $3.00 stop, a 1:1 target and a 54% win rate. Gross, it earns 0.54 − 0.46 = +0.08R per trade. Now add a 30-cent spread. That is $0.30 ÷ $3.00 = 0.10R lost on every trade, win or lose.

A step chart: gross expectancy of +0.08R minus a spread cost of 0.10R leaves a net expectancy of −0.02R per trade
The edge is +0.08R before costs. A 30-cent spread takes 0.10R and leaves −0.02R.

On the $2,400 account at 1% risk, one R is $24. Over 200 trades the gross edge is worth +0.08 × $24 × 200 = $384, and the spread costs 0.10 × $24 × 200 = $480. The system loses $96 while looking profitable on the chart.

So put costs in as a rule, not an afterthought. Use a spread at least as wide as you see live, and a wider one for news hours, when spreads widen. What is spread in forex explains how it behaves, and the profit and loss calculator lets you check what a trade nets after costs.

Other ways a backtest flatters you

  • Look-ahead bias. The rule quietly uses information that was not available yet, such as a candle’s final high or close before it has closed. Decide using only completed candles.
  • Hindsight on the setup. On a manual test you already know where price went, so ambiguous setups drift toward the winners. Hide the future part of the chart, and follow the written rule only.
  • Survivorship in your choice of market. Testing only the pairs or periods that later became famous leaves out the ones where the rules would have struggled.
  • Perfect fills. Real orders slip, especially in fast markets and around data releases. Assume your stop fills a little worse than the line, and your target a little short of it.
  • Position sizing that quietly changes. Compounding or risking more after a win inflates the curve. Keep risk fixed, say 1% per trade, so the result reflects the rules and not the sizing.
  • Counting only the average. Look at the longest losing streak and the maximum drawdown too. A system with a good average and a 30% drawdown may be one you cannot actually trade.

Data quality matters too. Free historical data can have gaps or wrong spreads. If a result depends on a few odd candles, check them against a second source before you believe it.

From a passed backtest to a live trade

Suppose your rules pass the out-of-sample test with costs in. That earns a promotion, not a launch. Forward test next: trade the rules on a demo account or at a tiny size for at least 50 trades, and compare the live numbers with the backtest. Execution, slippage and your own hesitation all show up here.

Set a fail condition before you start, for example: stop and review if the live expectancy is more than 0.2R below the backtest after 100 trades, or if drawdown exceeds the worst figure in the test by half. Then log every trade in a journal so the comparison is honest. Keep sizing rules in line with how to size a position.

A backtest is not a forecast

Markets change. A pattern that worked in one decade of rates and volatility can fade in the next. Even a clean test only shows the rules were sound in that period. Trade small, expect the live result to be worse than the test, and never risk money you cannot afford to lose.

Frequently asked questions

What is backtesting in trading?

It is replaying fixed trading rules on historical price data to see what they would have earned. You log each simulated trade and compute metrics such as win rate, expectancy and drawdown. It shows whether an idea had an edge in the past, not that it will in the future.

How do I backtest a trading strategy manually?

Hide the right side of a chart, then scroll forward one candle at a time. Apply your written rules, record each entry, stop, exit and result in R, and never skip a setup in hindsight. A spreadsheet is enough. Aim for at least 100 trades before you read anything into the totals.

How many trades do I need for a reliable backtest?

There is no magic number, but 30 trades is far too few and 100 is a bare minimum. At 100 trades the expectancy is uncertain by roughly ±0.20R, so several hundred is better. Strategies that trade rarely need more years of data to get there.

What is the difference between a backtest and a forward test?

A backtest uses past data you already have, so the outcome is known and rules can be tuned against it. A forward test runs the same rules on live prices as they arrive, on demo or at small size. It checks execution and costs that a backtest can only assume.

Can a profitable backtest still lose money live?

Yes, and it often does. Overfitting, missing costs, look-ahead bias and a changed market are the usual causes. Splitting your data, adding realistic spreads, and forward testing before sizing up remove most of them, though none removes the risk.

The PipsMorrow Team

A team of writers covering XAU/USD gold trading education. We focus on explaining the mechanics and risk management in a structured way, with no trade signals and no promotion of any specific broker.

Last updated

Keep reading

Related articles

Ready for the structured path

Build on this article with the full gold trading curriculum, ordered from the basics to advanced topics, free.

See the full curriculum →