Home Guides

Backtesting a forex strategy

By Liaqat Ali · Published · Updated

Backtesting applies your written rules to historical prices and records what would have happened. It is only useful if the rules are specific enough to apply the same way every time, and if the test is honest about costs and sample size.

Done well, a backtest tells you whether an idea is worth trading with real money, what a normal losing run looks like, and how much the strategy depends on low costs. Done badly, it produces a curve that looks convincing and means nothing. The difference is mostly in the process.

Before you start

Manual or automated?

There are two ways to run a backtest:

Many traders start manually. If you can't apply a rule the same way by hand every time, it isn't ready to be automated either.

Step by step

  1. Open the chart at the start of your test period, with future prices hidden.
  2. Move forward one candle at a time. When every setup condition is met and the entry triggers, record the trade exactly as the rules define it.
  3. Place the stop and target where the plan puts them, and move forward until one is hit.
  4. Record the result in pips and in R, after costs.
  5. Repeat until you reach the end of the period or the number of trades you planned, then stop. Don't extend the test because the results are disappointing, or end it early because they look good.

Record every trade

Log every signal the rules produce, including the losers and the trades you would have skipped in real life. A test that keeps only the trades you like is not a test. A simple spreadsheet is enough:

ColumnExample
Trade number and date#014, 12 March 2024, 09:00
Pair and directionGBP/USD, long
Entry, stop and target1.2710 / 1.2680 / 1.2770
Result in pips+60
Costs in pips1.2
Result in R, after costs+1.96R
NotesEntry on the second candle; rule followed

Include realistic costs

Costs matter more than most traders expect, especially with short stops. A 1-pip spread on a 20-pip stop costs 0.05R on every trade, win or lose. Over 100 trades that is 5R, which can be a large share of a modest edge. Use the typical spread for the pair and session you trade, add any commission, and allow a little slippage on entries and stops.

How many trades do you need?

Fewer than you'd hope can tell you very little. With 100 trades and a 45% win rate, the true win rate could plausibly be anywhere from about 35% to 55%. That range can be the difference between a strategy with an edge and one without.

Aim for at least 100 trades before drawing conclusions, across different market conditions: trending and ranging periods, quiet and volatile ones. If the rules produce very few trades, you need a longer period or more pairs, not a smaller sample.

Watch for overfitting

If you tune the rules until the historical curve looks good, you have fitted the strategy to past noise. Warning signs include many parameters, rules changed after seeing results, and a result that depends on one or two periods. Test the final rules on data you did not use to design them, and treat a small sample as uncertain rather than as proof.

A practical way to do this is an out-of-sample test. Design and refine the rules on one period, for example 2019 to 2022. Then, once the rules are fixed, run them once on a later period you haven't looked at, such as 2023 to 2024. If the later results are broadly similar, that is encouraging. If they collapse, the rules were probably fitted to the first period. Some traders repeat this in rolling blocks, known as walk-forward testing.

Read the results with their limits

Summarise the test with a few numbers, all in R:

A backtest shows how the rules behaved on one data set under one set of assumptions. It does not show what will happen next. Live trading adds slippage, missed fills and emotional decisions that a test does not capture. Simulated and backtested results do not guarantee future results.

From backtest to live trading

Treat a good backtest as permission to test further, not as proof. A sensible sequence is to trade the same rules on a demo account or with very small size for a set number of trades, then compare those results with the backtest. If your live results are much worse, look first at whether you followed the rules, then at costs and execution.

Questions traders ask

Can I backtest on TradingView for free?

TradingView's Bar Replay is available on its plans, though the timeframes and history it supports vary by plan. Check the current details on TradingView before relying on it.

How far back should I test?

Far enough to collect at least 100 trades across different market conditions. For a strategy that trades a few times a week, that usually means a year or more of data.

Is manual backtesting accurate?

It can be, if the rules are precise and you record every signal. The main risks are hindsight and skipping trades you don't like, so hide future prices and log everything.

My backtest is profitable. Is the strategy good?

It is a good start. Check the sample size, the costs, the out-of-sample result and whether you could have followed the rules in real time. Then test it live at small size.

The Sigma Trader's 12-week Traderpreneur programme covers writing a plan, fixed risk and reviewing trades. See the programme.

Risk warning: Trading foreign exchange on margin carries a high level of risk and may not be suitable for all investors. You could lose more than your initial deposit. This page is education only and is not financial advice. Simulated and backtested results do not guarantee future results.