Skip to content
Menu

Backtesting: Testing a Setup Before You Risk Money

By Chad Smith, Director of Research & Analysis · Updated

On this page
  1. What backtesting is
  2. Manual or automated
  3. Setting up the test
  4. What to record
  5. Calculating the results
  6. Common backtesting errors
  7. Out-of-sample testing
  8. After the backtest
  9. Key takeaways

In this lesson you will learn

  • Explain what backtesting can and cannot show
  • Run a manual backtest and record the right data
  • Calculate win rate, average win and loss, expectancy and drawdown
  • Avoid overfitting, look-ahead bias and other common errors

The previous lesson ended with a written setup, a hypothesis that a set of rules has an edge. This final lesson shows how to test it on historical data. You will learn how to run a backtest, which numbers to record, how to calculate the results and how to avoid the mistakes that make backtests look better than reality.

What backtesting is

Backtesting means applying your rules to past price data, trade by trade, as if you were trading at that time. You record each result and then study the totals.

A backtest can show:

  • Whether your rules are clear enough to apply consistently.
  • How often the setup appears.
  • The rough shape of results: win rate, average win and loss, and losing streaks.

A backtest cannot show what will happen in the future. Market conditions change. A good backtest makes a setup worth testing further; it does not make it proven.

Manual or automated

There are two ways to backtest.

  • Manual backtesting: scroll back on a chart, hide future candles, and step forward one candle at a time. When your rules trigger, record the trade. Many platforms, including TradingView, have a bar replay mode for this. It is slow, but it builds a deep feel for the setup.
  • Automated backtesting: code the rules, for example as an expert advisor in MetaTrader, and let software run them over years of data. It is fast, but every rule must be fully objective, and poor-quality data or wrong cost settings can distort results.

Most beginners should start manually. It forces you to face every judgment call in your rules.

Setting up the test

Before you start:

  1. Fix the rules. Write them down and do not change them during the test.
  2. Choose the data. Pick the pairs and timeframe the setup is designed for.
  3. Cover different conditions. Include trending periods, ranges and volatile news periods. A test only in one type of market tells you little.
  4. Plan the sample size. A few trades say almost nothing. Aim for a meaningful number, such as 100 trades or more, if the setup allows.
  5. Include costs. Add the typical spread and commission to every trade, as covered in the bid, ask and spread lesson.

What to record

Use a spreadsheet with one row per trade:

  • Date, time and pair
  • Direction, entry, stop and target prices
  • Exit price and reason (stop, target, rule-based exit)
  • Result in pips and in R, where R is the amount risked on the trade
  • Market condition at the time (trending, ranging, news)
  • A screenshot or note

Measuring results in R lets you compare trades with different stop sizes. A trade that risked 30 pips and made 60 pips is +2R. A trade that hit its stop is −1R.

Calculating the results

From your records, calculate:

Win rate = winning trades ÷ total trades
Average win (R) = total R of winners ÷ number of winners
Average loss (R) = total R of losers ÷ number of losers
Expectancy (R) = (win rate × average win) − (loss rate × average loss)

Worked example (illustrative numbers): 100 trades, 42 winners and 58 losers. Winners total 92R, so the average win is about 2.19R. Losers total 58R, so the average loss is 1R.

Expectancy = (0.42 × 2.19) − (0.58 × 1) = 0.92 − 0.58 = about 0.34R per trade.

100 tradesExample42 winners × +2.19R ≈ +92R58 losers × −1R = −58RNet ≈ +34R over 100 trades≈ +0.34R per tradeWin rate only 42%, but winnersare about twice the size of losers.Expectancy = (win rate × avg win) − (loss rate × avg loss)
A win rate under 50% can still give positive expectancy when the average win is larger than the average loss.

A positive expectancy means the setup made more than it lost on average in the test. Note that the win rate was below 50%, yet the result was positive, because winners were larger than losers. The risk course explores this in risk/reward and win rate.

Also record the largest losing streak and the largest drawdown, the biggest fall from a peak in cumulative R. These tell you how painful the setup is to trade, which matters as much as the average.

Common backtesting errors

Overfitting. Adjusting rules until the past results look excellent. The more you tune, the more the rules describe the past instead of a lasting pattern. Keep rules simple and test changes on fresh data.

Look-ahead bias. Using information that was not available at the time, such as seeing that a level held before you would have known it. Hide future candles and make each decision only on what is visible.

Cherry-picking. Skipping trades that "did not look right" when the rules said to take them. Every trigger counts.

Ignoring costs and slippage. Small costs can turn a positive test negative, especially on short timeframes.

Too small a sample. Twenty trades can look great or terrible by chance alone.

One market, one period. A setup that only worked in one year on one pair may not be robust.

Out-of-sample testing

A good habit is to split your test data. Build and adjust the rules on one period, the in-sample data. Then run the final rules, unchanged, on a separate period you have not looked at, the out-of-sample data. If results collapse on the new data, the rules were probably overfitted.

Fix the ruleswritten down, unchanged during the testIn-sample testbuild and adjust on one periodOut-of-sample testsame rules on a period you have not seenForward testreal time, demo or very small sizeLive, linked to a risk planrisk set from drawdown and streaksResults collapse?rules probablyoverfittedInclude spread andcommission atevery stage
A backtest is the first filter, not the finish line: unseen data and real-time testing come next.

After the backtest

If the results look promising, the next step is forward testing: trading the same rules in real time on a demo account or with very small live size. This tests execution, your discipline and live costs. The demo to live lesson explains how to make that transition. Compare forward results with the backtest; large differences point to a problem in the rules or in how you apply them.

Before going live, link the setup to a risk plan. The backtest's drawdown and losing streaks help you choose your risk per trade, as covered in the risk of ruin lesson.

Key takeaways

  • Backtesting applies fixed rules to past data to see how a setup would have behaved.
  • Start manually, record every trade in R, and include realistic costs.
  • Calculate win rate, average win and loss, expectancy, losing streaks and drawdown.
  • Avoid overfitting, look-ahead bias, cherry-picking and small samples, and test on out-of-sample data.
  • A promising backtest leads to forward testing, not straight to large live positions.

Check your understanding

  1. 1. A setup wins 40% of trades, with an average win of 2R and an average loss of 1R. What is the expectancy per trade?

    • A. −0.2R
    • B. 0.2R
    • C. 0.4R
    • D. 0.8R
    Show answer

    B. 0.2R

  2. 2. What is look-ahead bias in a manual backtest?

    • A. Using information that would not have been known at the time of the trade
    • B. Testing on too many years of data
    • C. Using a demo account
    • D. Recording losing trades
    Show answer

    A. Using information that would not have been known at the time of the trade

  3. 3. What is a sensible step after a backtest looks promising?

    • A. Trade the largest size possible
    • B. Forward test the setup on demo or very small live size
    • C. Add more rules until every trade wins
    • D. Stop keeping records
    Show answer

    B. Forward test the setup on demo or very small live size

Brokers with strong charting to practice on

Put this into practice on a demo account first, with no money at risk.