Backtesting

What Is Backtesting in Trading? A Complete Beginner's Guide

Backtesting applies fixed trading rules to past prices to see how a strategy would have behaved. What it can tell you, what it cannot, and a worked example.

By AlgoXP4 min readUpdated

Equity curve of a strategy tested on historical data and then on new data

Backtesting means taking a trading strategy written as exact rules and applying it to historical prices, as if it had been running at the time. The output is a list of simulated trades and statistics: profit, drawdown, number of trades, win rate. It answers one question: would these rules have worked in the past, and how painful would it have been?

It is the cheapest way to reject bad ideas. Years of market history can be replayed in minutes, and every weakness the rules have in that history becomes visible before real money is involved.

What a backtest needs

A backtest is only possible when three things are defined:

  1. Rules without ambiguity. “Buy when the trend is strong” is not testable. “Buy when the 50-period moving average is above the 200-period average and the close breaks the high of the last 20 bars” is.
  2. Historical data for the instrument and timeframe, of a quality that matches how the strategy trades. A strategy that reacts to every tick needs tick data; a daily system can live with daily bars.
  3. Trading conditions: starting capital, position size, spread, commission, swap and slippage. Without costs, almost every short-term strategy looks better than it is.

A worked example

Suppose the idea is a simple breakout on EURUSD, H1:

  • Enter long when the close breaks above the highest high of the last 24 bars.
  • Stop loss 1.5 × ATR(14) below entry, take profit 3 × ATR(14) above.
  • Risk 1% of the account per trade, one position at a time.

Run it over five years and the report might show 410 trades, 37% winners, an average win of 2.1R and an average loss of 1R. The expectancy per trade is:

0.37 × 2.1 − 0.63 × 1.0 = 0.147R

So, on average, each trade would have earned about 0.15 of the amount risked, before you ask harder questions. The harder questions are the useful part:

  • Did most of the profit come from one year? Split the results by year.
  • What was the longest losing streak? At a 37% win rate, streaks of 8-10 losses are normal. Could you have sat through them?
  • What happens if the spread is doubled? If the edge disappears, it was too thin to trade.
  • Do slightly different settings (20 or 30 bars instead of 24) still work? If only 24 works, the result is probably luck.

What backtesting is good for

  • Rejecting ideas quickly. Most ideas fail even in a generous backtest. That is a cheap failure.
  • Understanding behaviour. How often the strategy trades, how long positions stay open, how deep and how long drawdowns are.
  • Setting expectations. A realistic picture of losing streaks makes it easier to keep following the rules later.
  • Comparing variants under identical conditions, for example two exit methods on the same data.

What backtesting cannot do

  • Predict the future. Markets change. A backtest describes one path that has already happened.
  • Prove an edge on its own. If you test many variants and pick the best one, part of its result is selection luck. The more variants you try, the bigger that part becomes.
  • Simulate everything about execution. Real fills, requotes, gaps over weekends, liquidity at news time and broker-specific behaviour are only approximated.
  • Test you. The simulation follows every signal without hesitation. A person often does not.

Common beginner mistakes

Mistake Why it misleads Fix
No costs Short-term strategies look profitable on paper only Add realistic spread, commission and slippage
Too few trades Luck looks like skill Collect at least about 100 trades, or treat results as anecdotes
Look-ahead bias Rules use information not yet available at the time Act only on closed bars; check every indicator’s timing
Endless tweaking Rules end up fitted to noise Decide the test plan first; keep the last part of the data untouched
One market, one period Results depend on one regime Test across years, regimes and, where it makes sense, related markets

From backtest to live trading

A good backtest is the first gate, not the last. A sensible path looks like this:

  1. Backtest on the main data period with realistic costs.
  2. Out-of-sample test on data the rules have never seen, for example the most recent year that you kept aside.
  3. Robustness checks: nearby parameters, higher costs, Monte Carlo reshuffling of trades.
  4. Forward test on a demo account or with very small size, long enough to collect a meaningful number of trades.
  5. Live trading with risk limits that assume the future drawdown can be worse than the backtest showed.

Backtesting on cTrader

On cTrader, strategies are written as cBots and tested in the Backtesting tab of cTrader Algo, which can replay tick data or bar data from the broker’s server with your commission and spread settings. The ideas above apply regardless of platform; the cTrader Algo guide explains where everything sits.

Key takeaways

  • A backtest turns a trading idea into numbers you can question.
  • Its main job is to reject weak ideas cheaply, not to promise returns.
  • Costs, enough trades and untouched out-of-sample data matter more than a pretty equity curve.
  • Every strategy that survives the backtest still needs a forward test before real risk.

FAQ

Does a profitable backtest mean the strategy will make money?

No. A backtest shows how fixed rules would have behaved on one historical path. It is evidence, not a forecast. Treat it as a filter that rejects bad ideas, then confirm the survivors with out-of-sample and forward tests.

How many trades does a backtest need?

As a rough rule, at least 100 trades before you read much into win rate or profit factor, and more if the strategy trades rarely or has large outliers. With 20 trades, a few lucky results can dominate everything.

How many years of data should I use?

Enough to include different market conditions: trends, ranges, quiet and volatile periods. For intraday systems on major FX pairs or gold, several years is common. What matters more than the calendar length is how many independent trades and regimes you cover.

Can I backtest a discretionary strategy?

Only the parts that can be written as exact rules. If a decision depends on a feeling you cannot describe, the backtest will quietly replace it with something else, and the result will not describe how you actually trade.

Sources and further reading

  1. Bailey, Borwein, López de Prado, Zhu: The Probability of Backtest Overfitting - papers.ssrn.com
  2. cTrader Help: Backtest a cBot - help.ctrader.com