How backtesting works: data, trades and pitfalls

Backtesting works by replaying your trading rules over historical price data to see how they would have performed — the program walks bar by bar through the past, applies your entry and exit conditions, records each simulated trade, and reports the profit, drawdown and risk. In short, it is a time machine for a strategy: instead of guessing whether an idea is any good, you measure it against years of real market history. This guide walks through every stage — the data, the simulated trades, the equity curve, the metrics — and is honest about the pitfalls that make a backtest lie and why even a perfect one cannot promise live profit.

Last updated 21 June 2026 · by Mustafa Bilgic

On this page
  1. What a backtest is
  2. The data: OHLCV
  3. Simulating the trades
  4. The equity curve
  5. The metrics that matter
  6. The pitfalls that make it lie
  7. Why it cannot guarantee live results
  8. FAQ

What a backtest is

A backtest is a simulation that applies a set of trading rules to historical data and reports the outcome. It answers a single question: “If I had run this exact strategy over this period, what would have happened?” Because a trading algorithm is fully specified — no human judgement in the loop — its behaviour on past data is reproducible, and that is what makes the test meaningful. Backtesting is the cheapest way to reject a bad idea before it costs you real money, which is the main reason it matters. You can run one on the free in-browser backtester without writing any code.

The data: OHLCV

A backtest is only as good as the data feeding it. The standard format is OHLCV — for each time period (a day, an hour, a minute) you store the open, high, low, close and volume. The choice of timeframe shapes everything: a daily backtest and a one-minute backtest of the same idea can give wildly different results. The data must be clean — no gaps, no duplicated bars, no obviously bad ticks — and, for stocks, adjusted for splits and dividends. One quietly corrupted series can invalidate an entire study, so experienced testers spend more time checking data than they expect to.

Simulating the trades

With clean data loaded, the engine walks forward one bar at a time. On each bar it computes your indicators, checks the entry and exit rules, and — when a rule fires — opens or closes a simulated position at a realistic price. The golden rule here is that a decision on bar n may only use information that existed at the close of bar n−1 (or earlier). Acting on a bar before it has finished forming is the single most common way people accidentally cheat. A faithful engine also subtracts trading fees and models slippage — the gap between the price you expected and the price you actually got — because both eat directly into returns.

python · backtest_loop.py# walk the history one bar at a time — no peeking ahead
position = 0; cash = 10000.0; equity = []
for i in range(1, len(bars)):
    signal = strategy(bars[:i])      # only data up to bar i-1
    price  = bars[i]['open']      # fill on the NEXT bar's open
    if signal == 1 and position == 0:
        position = cash / (price * 1.001)   # 0.1% fee + slippage
        cash = 0
    elif signal == 0 and position > 0:
        cash = position * price * 0.999; position = 0
    equity.append(cash + position * price)

That loop is the heart of every backtester: read past data, get a signal, fill on the next bar with costs, and record equity.

The equity curve

As the simulation runs it records account value after every bar, producing the equity curve — the single most informative output. A smooth, steadily rising curve is encouraging; a jagged one that spikes up and crashes down tells you the strategy is fragile even if the final number is positive. Two strategies can end at the same return while feeling completely different to live through, and the equity curve is where you see that. Pay attention to the shape and the depth of the dips, not just the endpoint.

time → drawdown
The equity curve shows growth and, just as importantly, the drawdowns you would have had to sit through.

The metrics that matter

The equity curve gets summarised into numbers. The most useful ones are:

The pitfalls that make a backtest lie

This is the part most guides skip, and it is the most important. A backtest is extremely easy to fool yourself with:

  1. Look-ahead bias. Using data the strategy would not have had in real time — even by one bar. It produces gorgeous, fictional results.
  2. Survivorship bias. Testing only on assets that still exist today silently removes every company or coin that went to zero, flattering the result.
  3. Over-fitting. Tuning parameters until they fit the past perfectly. The strategy memorises noise and collapses on new data — see overfitting explained.
  4. Ignoring fees and slippage. A strategy that trades often can look great with zero costs and be a guaranteed loser once realistic costs go in.

Guard against these by holding out an out-of-sample period the strategy never sees during design, comparing backtesting versus forward testing, and keeping the rules simple. For the full method see how to backtest a trading strategy.

Why it cannot guarantee live results

Even a clean, honest, out-of-sample-validated backtest is a description of the past, not a forecast. Markets change regime, liquidity dries up, your own trades start to move the market, and edges that everyone discovers get competed away. Randomness alone means a genuinely good strategy can still have a losing year. So treat backtesting as a powerful filter for rejecting bad ideas and measuring risk — never as a promise of profit. The only way to keep narrowing the gap to reality is to follow a passing backtest with paper trading and then, if at all, very small live size.

Not financial advice. This content is educational. Automated and algorithmic trading carries a real risk of financial loss. Never trade money you cannot afford to lose. Review the SEC investor.gov and CFTC resources before trading.

Frequently asked questions

What is a backtest in simple terms?

A backtest replays your trading rules over historical price data to estimate how they would have performed. The program walks through each past bar, applies your entry and exit conditions, records the simulated trades, and produces a record of profit, losses and risk. It is a way to measure an idea against the past instead of guessing whether it works.

What data do you need to backtest a strategy?

Most backtests use OHLCV data: the open, high, low, close and volume for each time period such as a day, hour or minute. The data should be clean, adjusted where appropriate, and free of gaps. Realistic results also need assumptions for trading fees and slippage, because ignoring those makes almost any strategy look far better than it really is.

What are the biggest mistakes in backtesting?

The classic errors are look-ahead bias (using information that was not available at the moment of the trade), survivorship bias (testing only on assets that still exist today), over-fitting (tuning the rules until they fit past noise), and ignoring fees and slippage. Any one of these can turn a losing strategy into a backtest that looks brilliant but fails live.

Does a good backtest guarantee future profit?

No. A backtest only describes the past, and markets change. Even a clean, honest backtest can fail going forward because conditions shift, competition adapts, and randomness plays a role. Backtesting is essential for rejecting bad ideas and measuring risk, but it is a filter, not a promise. Always confirm with out-of-sample testing and paper trading before risking money.

MB

Mustafa Bilgic

Algorithmic trading practitioner · Founder, AITradingBot.us

Mustafa builds and backtests automated trading systems and writes about them without the hype. Every tool on this site is free and runs entirely in your browser.