◆ New — Strategy Validator, Code Fixer & Prop Compliance Checker are live. Try one free →
Home / Learn / What Is Backtesting (and Its Traps)
Education

What Is Backtesting (and Its Traps)

Backtesting means running a fixed set of trading rules against historical price data to see how they would have behaved in the past. It is a useful research and learning tool, but it is easy to fool yourself with it. This article explains what backtesting is, how it works, and the common traps that make past results look far better than anything you should expect going forward. Educational only. No indicator or method is predictive, and all trading carries the risk of loss.

What backtesting actually is

Backtesting is the process of taking a defined set of trading rules and running them against historical price data to see what would have happened if those rules had been followed in the past. The rules need to be specific and mechanical: when to enter, when to exit, how much to risk, and what conditions must be true. The output is usually a record of simulated trades plus summary statistics such as the number of trades, average gain and loss, and the shape of the equity curve over time.

The goal is not to prove that a method 'works'. It cannot do that, because the future is not contained in the past. What a backtest can do is help you understand how a set of rules behaves: how often it trades, how long it tends to hold, how deep its drawdowns get, and whether its logic is even coherent. Think of it as a structured way to study an idea, similar to how a scientist runs an experiment to learn, not to guarantee an outcome.

Why traders use it (and what it cannot tell you)

Traders often use backtesting to sanity-check an idea before risking money, to compare how different rule sets behave on the same data, and to get a feel for the kind of losing streaks a method can produce. Seeing a long stretch of consecutive losses in a historical test, for example, can help a trader think through what a similar streak might feel like in real time.

What a backtest cannot tell you is what will happen next. It says nothing definitive about future returns, win rates, or whether the market conditions that produced past results will ever repeat. Markets change: volatility shifts, liquidity dries up, and relationships between instruments break down. A backtest is a description of one possible past, not a prediction. No indicator or strategy is predictive, and a strong historical result is not a promise of anything. Past performance does not indicate future results.

The traps that make results look better than reality

Overfitting is the most common trap. If you keep adjusting parameters until the historical curve looks great, you are often just fitting the noise of one specific dataset rather than finding a durable pattern. The more settings you tweak, the easier it is to produce a beautiful past that means little going forward.

Lookahead bias happens when your rules accidentally use information that would not have been available at the time of the trade, such as referencing the close of a candle before it has actually closed, or calculating on a bar and acting on the same bar as if you already knew its outcome. Survivorship bias creeps in when you test only instruments that still exist today, quietly excluding the ones that failed or were delisted, which flatters the results.

Finally, ignoring real-world costs is a silent killer. Spread, commission, slippage, swap or financing charges, and the gap between an assumed fill price and a realistic one can all turn a tidy backtest into a losing approach once applied to live markets. A test that omits these costs is describing a market that does not exist.

How to make a backtest more honest

A few practices reduce the chance of fooling yourself. The first is splitting your data: design and tune your rules on one portion (in-sample) and then test them, untouched, on a separate portion the rules have never seen (out-of-sample). If performance collapses on the unseen data, that is a strong sign the rules were overfit.

Walk-forward analysis extends this idea by repeatedly optimising on one window and testing on the next, rolling forward through history, which better mimics how you would actually update a method over time. Forward testing, often called paper trading, runs the rules on new data going forward in real time, with no benefit of hindsight at all.

It also helps to keep rules simple, to include realistic costs, to use enough data to cover different market conditions, and to be honest about how many variations you tried before settling on one. The more ideas you test, the more likely one looks good by pure chance. None of these practices can make a method predictive; they only help you avoid mistaking a tuned past for a reliable future.

A minimal, neutral Pine Script example

Backtesting in a charting platform usually starts with a strategy script rather than an indicator, because a strategy can simulate entries and exits. The example below is a minimal, illustrative skeleton in Pine Script v6. It is intentionally simple and is shown only to demonstrate structure, not as a method to trade. Including realistic commission and slippage matters, and the resulting report describes the past only.

//@version=6
strategy("Backtest skeleton (educational)", overlay = true, commission_type = strategy.commission.percent, commission_value = 0.05, slippage = 2)

fastLen = input.int(10, "Fast MA length")
slowLen = input.int(30, "Slow MA length")

fast = ta.sma(close, fastLen)
slow = ta.sma(close, slowLen)

plot(fast, "Fast", color = color.aqua)
plot(slow, "Slow", color = color.orange)

// Illustrative cross logic only; not advice and not predictive
longCond = ta.crossover(fast, slow)
exitCond = ta.crossunder(fast, slow)

if longCond
    strategy.entry("Long", strategy.long)
if exitCond
    strategy.close("Long")

Whatever numbers this report produces, treat them as a starting point for investigation, not a conclusion. Traders often read a backtest report as a description of behaviour to study further, rather than as a signal to act on. No setup here is predictive, and all trading carries the risk of loss.

Key takeaways

Educational only — not financial advice. Trading involves substantial risk of loss.

Catch the bug that compiles.Run auditGet Pro