Skip to content
Level IV · Systematic Investing & Trading

What Is Backtesting and How Do You Do It Properly?

Updated 27 November 2026 · 7 min read · Reviewed against our editorial policy

In brief

A backtest applies a fixed set of rules to historical data to see how those rules would have behaved. Done properly, it means writing the rules before looking at results, cleaning the data, simulating step by step with costs, and recording everything. It shows how a rule behaved in the past. It does not show how it will behave in the future.

  • A backtest simulates fixed, pre-written rules on historical data to see how they would have behaved, not to predict what happens next.
  • Define every rule, including entries, exits, sizing and costs, before looking at any results, so that the test cannot be shaped by hindsight.
  • Record the whole path (equity curve, drawdown, number of trades, costs) rather than a single headline return.
  • A backtest is one historical path, so it can falsify a weak idea but can never prove a strategy will work in future.

The intuition: a flight simulator for rules

A pilot trainee does not learn by flying a real aircraft into a storm. A simulator replays difficult conditions so mistakes cost nothing. A backtest does something similar for investing rules. It takes a precise set of instructions, applies them to historical prices as if they had been followed at the time, and records what would have happened.

The key word is precise. A backtest cannot test a feeling such as "buy when things look strong." It can only test an instruction that a computer, or a very disciplined person, could follow without judgement. That is why backtesting sits at the heart of systematic investing: it is the step that turns an idea into something measurable.

It helps to be clear about the purpose from the start. A backtest is a tool for rejecting weak ideas and understanding the behaviour of a rule: how bumpy it is, how long it can stay down, how much it costs to run. It is not a forecast.

Step 1: Define the rules before you test

The most important step happens before any data is touched. Write the rules down in full. A complete rule set answers each of these questions:

  • Universe: what can the strategy hold? (For example, one broad stock index.)
  • Signal: what condition triggers a position? (For example, the previous year's return was positive.)
  • Timing: when is the signal measured and when is the trade made?
  • Exit: what ends a position?
  • Sizing: how much is held? (All-in, half, a fixed fraction?)
  • Costs: what does each trade cost, as a percentage of the amount traded?
  • Evaluation: which measures will judge the result, and what result would count as a failure?

That last item matters more than it looks. If the failure condition is written down in advance, the result cannot be quietly reinterpreted later. Writing the rules first is the main protection against tuning the test until it looks good, a problem covered in more depth in the lessons on overfitting and data snooping.

Step 2: Prepare the data

A simulation is only as honest as its inputs. Before running anything, check:

  • Coverage: does the history include different conditions (rising markets, falling markets, sideways periods)?
  • Adjustments: are prices adjusted for splits and dividends, so a stock split does not look like a crash?
  • Gaps and errors: are there missing days, obvious typos or stale prices?
  • Survivorship: does the data include assets that were delisted or failed, or only the ones that survived?
  • Timestamps: is each data point available when the rule says it is used? Using a figure that was only published later is look-ahead bias, and it quietly makes results look better than they could have been.

These topics each deserve their own lesson, but even a quick checklist catches many of the most common errors.

Step 3: Simulate step by step

The simulation itself is simple bookkeeping, repeated for each period:

  1. Using only information available at that moment, check the signal.
  2. Decide the position for the next period.
  3. If the position changes, deduct the trading cost.
  4. Apply the period's return to the position held.
  5. Record the new portfolio value.

The sequence in steps 1 and 4 is where many errors creep in. The signal must be computed from the past, and the return applied must be the following period's return. If both come from the same period, the rule has seen the future.

Step 4: Record the results

The output is an equity curve: the portfolio value at the end of each period. From it, record at least:

  • Total and annualised return
  • Maximum drawdown (the largest fall from a peak to a later low)
  • Number of trades and total costs paid
  • The comparison with a simple benchmark, such as holding the asset throughout

A single return figure hides the journey. Two strategies can finish at the same value while one of them fell by half along the way, which is much harder to live through.

Worked example: a one-year-lag rule

This example uses entirely hypothetical numbers. Assume an asset with these yearly returns: +10%, +20%, −25%, +5%, +15%.

The rules (written first):

  • Hold the asset during a year if the previous year's return was positive. Otherwise hold cash earning 0%.
  • In year 1 there is no previous year, so the rule starts invested.
  • Each switch between asset and cash costs 0.2% of the portfolio value.
  • Start with 100,000 (₹ or $; the arithmetic is identical).
  • For simplicity, ignore the cost of the initial purchase for both the rule and buy-and-hold.
  • Drawdowns are measured on year-end values only.

Year-by-year calculation:

  • Year 1: invested. 100,000 × 1.10 = 110,000.
  • Year 2: year 1 was positive, so stay invested. 110,000 × 1.20 = 132,000.
  • Year 3: year 2 was positive, so stay invested. 132,000 × 0.75 = 99,000.
  • Year 4: year 3 was negative, so sell to cash. Cost = 0.2% × 99,000 = 198, leaving 98,802. Cash earns 0%, so the year ends at 98,802.
  • Year 5: year 4 was positive (+5%), so buy back. Cost = 0.2% × 98,802 ≈ 198, leaving about 98,604. Then 98,604 × 1.15 ≈ 113,395.
Year Asset return Rule position Rule end value Buy-and-hold end value
1 +10% Asset 110,000 110,000
2 +20% Asset 132,000 132,000
3 −25% Asset 99,000 99,000
4 +5% Cash (after selling) 98,802 103,950
5 +15% Asset (after buying) 113,395 119,543

Buy-and-hold is simply 100,000 × 1.10 × 1.20 × 0.75 × 1.05 × 1.15 ≈ 119,543, with no trading costs.

Reading the results:

  • Rule total return: 113,395 / 100,000 − 1 ≈ 13.4% over five years.
  • Buy-and-hold total return: about 19.5%.
  • Rule maximum drawdown: from the 132,000 peak to 98,802 is about −25.2%.
  • Buy-and-hold maximum drawdown: from 132,000 to 99,000 is exactly −25.0%.

The rule did not help here. It stayed in during the year 3 fall because the signal looked backward, then it sold after the damage and missed the year 4 gain. Its drawdown was no smaller, and it paid costs on top.

This is a useful result, not a failure of the method. It shows that a simple one-year-lag rule would have lagged on this particular path. It also shows why the full record matters: the headline return, the drawdown and the costs together tell the story. Five data points are far too few to conclude anything general, and the example exists to show the mechanics, not to judge the rule.

What a backtest can tell you

  • Whether the rule is coherent. Can it be followed mechanically, and does it produce sensible positions?
  • How the rule behaved historically. Typical returns, worst drawdown, how often it trades, how much it pays in costs.
  • Whether an idea is obviously weak. If a rule fails even with generous assumptions, that is useful information.
  • Sensitivity. Small changes to parameters show whether results depend on one fragile setting.

What a backtest cannot tell you

  • That it will work in future. History is one path out of many that could have happened. Conditions, participants and costs change.
  • How you would have behaved. Living through a 25% fall is different from reading it in a table. Many people abandon rules at the worst moment.
  • Real execution. Simulated trades happen at clean prices. Real trades face spreads, slippage and sometimes no available liquidity.
  • Anything beyond the data. A test on a period of rising markets says little about a prolonged decline.

Common mistakes

  • Changing the rules after seeing results. Each adjustment made to improve the curve moves the test from evidence towards storytelling.
  • Ignoring costs. Spreads, commissions and taxes can turn a small edge into a loss, especially for frequent trading.
  • Look-ahead errors. Using information that was not yet available, or applying a signal and the return from the same period.
  • Survivorship bias. Testing only on assets that still exist today.
  • Reporting only the return. Leaving out drawdown, trade count and the benchmark comparison.
  • Trusting a short sample. A handful of trades or years is mostly noise.
  • Testing many ideas and reporting the best. The best of many random results will look impressive by chance.

Try this

Using the same five yearly returns (+10%, +20%, −25%, +5%, +15%), change only one rule: hold the asset if the previous year's return was greater than −10%, otherwise cash. Keep the 0.2% cost per switch.

  1. Work out the position in each year and the end value for each year.
  2. Compute the total return and the maximum drawdown on year-end values.
  3. Compare both with the table above.
  4. Write one sentence on whether this change shows anything meaningful, or whether five data points are simply too few to say.

If the result looks better, resist the urge to adopt it. The aim of the exercise is to notice how easy it is to change a rule after seeing the data.

Frequently asked questions

Is a good backtest evidence that a strategy will make money?

No. A good backtest shows that a rule would have performed well on one stretch of history, which is weak evidence at best. Markets change, costs differ in real trading, and a rule may have been refined until it fit the past. Treat a good backtest as a reason to investigate further, not as proof.

How much historical data do you need for a backtest?

There is no single number, because it depends on how often the strategy trades and how long its cycles are. A rule that trades many times a year can gather more observations in a short period than one that changes position once every few years. As a general principle, the data should cover varied market conditions, including falling and sideways periods, and contain enough trades that results are not driven by a few lucky ones.

Why must the rules be written before testing?

If rules are adjusted while looking at results, the test gradually becomes a search for whatever fits the past, and the final result reflects that search rather than the idea. Writing rules and failure conditions in advance keeps the test honest. It also makes it possible to see how many variations were tried, which matters for judging how much to trust the outcome.

Do you need coding skills to backtest?

Not necessarily. A simple rule applied to a few years of data can be tested in a spreadsheet, as in the worked example above. Coding becomes useful when the rule is more complex, the data set is large, or the test needs to be repeated many times. The discipline of precise rules and honest data matters more than the tool.

Continue learning

Educational content only — not investment, tax or legal advice. See our disclaimer.