Skip to main content
All posts

What is Backtesting? Definition, Formula, and Example

Backtesting is the process of applying a trading strategy's rules to historical price data to measure how the strategy would have performed, quantified through metrics like return, drawdown, and Sharpe ratio.

What is Backtesting?

Backtesting is the process of simulating a trading strategy against historical market data to measure how that strategy would have performed had it been traded in the past. A backtest takes a complete, unambiguous set of rules — entry conditions, exit conditions, position sizing, and costs — and replays them bar by bar over a historical dataset, producing a track record of hypothetical trades. The output is a set of performance statistics: total return, win rate, maximum drawdown, Sharpe ratio, and profit factor. Backtesting is the quantitative foundation of systematic trading; a strategy that cannot survive a rigorous backtest has no business being traded with real capital.

How Backtesting Works

A valid backtest requires four components:

1. Rules — deterministic entry and exit logic (e.g., "buy when the 50-day moving average crosses above the 200-day; sell when it crosses below").

2. Data — survivorship-bias-free historical prices, adjusted for splits and dividends.

3. Cost model — commissions, slippage, and the bid-ask spread applied to every simulated fill.

4. Metrics — the evaluation layer. Core formulas:

  • CAGR = (Ending Equity / Starting Equity)^(1/years) − 1
  • Profit Factor = Gross Profits / Gross Losses
  • Sharpe Ratio = (Mean Return − Risk-Free Rate) / Standard Deviation of Returns

The gold standard is a walk-forward test: optimize parameters on an in-sample window, validate on the following out-of-sample window, then roll both windows forward. This tests whether the strategy adapts rather than memorizes.

Worked Example

A trader backtests a simple mean-reversion rule on SPY from 2015–2025: buy when the 2-period RSI closes below 10, exit when it closes above 55, risking 100% of equity per trade, with $0.005/share slippage. The test produces roughly 140 trades, a 72% win rate, an average gain of 0.9% per trade, a CAGR near 9% versus buy-and-hold's ~12%, but a maximum drawdown of only 8% versus the index's 34% COVID-era drawdown. The strategy underperforms on raw return but dominates on risk-adjusted terms — exactly the tradeoff a backtest exists to reveal.

When Traders Use Backtesting

Systematic traders backtest before deploying any new rule. Discretionary traders backtest to validate pattern edges (e.g., "does the opening range breakout actually work on QQQ?"). Options traders backtest premium-selling rules across volatility regimes. Portfolio managers backtest allocation and rebalancing logic.

Limitations and Common Misconceptions

  • Overfitting is the primary failure mode: a strategy tuned to 20 parameters will look brilliant in-sample and fail live. Fewer rules, more data.
  • Look-ahead bias — using data that wasn't available at the decision point (e.g., using a day's close to trigger a same-day entry at the open) — invalidates results silently.
  • Survivorship bias — testing only on stocks that exist today inflates returns by excluding delisted losers.
  • A backtest measures the past. Regime change — new market structure, new volatility regime, crowding of the same signal — breaks historical edges. A backtest is a filter, not a guarantee.