How to Backtest a Trading Strategy
Backtesting is the practice of simulating a trading rule on historical price data to estimate how it would have performed. It is the single most important step before risking real capital on a discretionary or rule-based system. A well-designed backtest answers three questions: does the edge exist?, how painful is the drawdown?, and is the return worth the risk?
This calculator runs entirely in your browser. Your symbols, parameters, and results never leave your device. Data is fetched from Yahoo Finance (21,000+ symbols covering US stocks, ETFs, ADRs, Indonesian stocks on IDX, cryptocurrencies like BTC-USD, forex pairs like EURUSD=X, and commodities).
The 10 Strategies Explained
1. SMA Crossover
Buy when the fast simple moving average crosses above the slow SMA; sell when it crosses below. A classic trend-following rule. Common choices: 20/50, 10/30, or the famous 50/200 "golden cross".
2. EMA Crossover
Same logic as SMA crossover but with exponential moving averages, which react faster to recent price changes. Often produces more signals (and more whipsaws in choppy markets).
3. RSI Mean Reversion
Buy when the Relative Strength Index drops below the oversold threshold (typically 30); sell when it rises above overbought (typically 70). Works best in range-bound markets; tends to lose money in strong trends.
4. MACD Crossover
Go long when the MACD line crosses above its signal line; exit when it crosses below. MACD combines trend (12/26 EMA difference) with momentum (9-period signal line smoothing).
5. Bollinger Bands — Mean Reversion
Buy when the price touches or breaches the lower band (2 standard deviations below the 20-day SMA); sell when it reaches the upper band. Profits from short-term extremes reverting to the mean.
6. Bollinger Bands — Breakout
Opposite of mean reversion: buy when price breaks above the upper band (strength signal), sell when it closes back inside. Works in trending regimes, fails in choppy ones.
7. Donchian Breakout
Buy when price makes a new N-day high; exit when it makes a new N-day low. Popularized by the "Turtle Traders" (Richard Dennis, 1983) with a 20/10 variant. Simple but surprisingly robust.
8. Golden Cross
The most famous long-term trend filter: buy when the 50-day SMA crosses above the 200-day SMA, sell when it crosses below ("death cross"). Rare signals, but each one tends to capture major bull/bear phases.
9. ATR Trailing Stop
A dynamic stop-loss that follows price at a fixed multiple of Average True Range. Re-enters on breakouts above the prior stop level. Balances giving the trend room to breathe with cutting losses quickly.
10. Buy & Hold (Baseline)
Not a strategy, but the benchmark every active system must beat. If your MACD crossover delivered 9% CAGR but buy-and-hold on the same asset returned 12%, your model destroyed value.
Interpreting the Metrics
- Total Return — cumulative percentage gain over the full backtest window.
- CAGR — compound annual growth rate, normalizes returns to a yearly figure so you can compare strategies run over different timeframes.
- Max Drawdown (MDD) — the worst peak-to-trough loss during the period. A 40% drawdown means you held through a loss from $100,000 down to $60,000. Most traders can't stomach more than 20–25%.
- Sharpe Ratio — annualized excess return per unit of volatility. >1 is good, >2 is exceptional, <0.5 is probably noise.
- Win Rate — percentage of trades closed at a profit. Note: many profitable trend systems have 40–45% win rates but huge average wins vs. small average losses.
- Number of Trades — sample size matters. Under 20 trades, any result could easily be luck.
Parameter Optimization — Use With Caution
The Optimize button runs a grid search over reasonable parameter ranges and reports the top 10 combinations. This is powerful but dangerous. Curve-fitting to historical data is the #1 way retail traders delude themselves. A rule of thumb: if you optimize 50 parameter combinations on 5 years of data, at least one will look spectacular by pure chance.
Professional practice: split data into an in-sample window (say, first 70%) for optimization and an out-of-sample window (remaining 30%) for validation. A strategy that dominates in-sample but collapses out-of-sample is overfit and should be discarded. You can manually approximate this by running a 5Y backtest to find parameters, then re-running with a 10Y range to see if the edge persists on older data.
Limitations of This Backtester
Transparent assumptions:
- Long-only (no short selling)
- Full-capital allocation per trade (no position sizing)
- Executes at the close of the signal bar (perfect fills assumed)
- Zero slippage, zero commissions, zero financing cost
- Daily bars only (no intraday)
- No dividends or corporate actions — returns are price-only
Real-world results will be worse than shown here. Use this tool for relative comparison between strategies, not for absolute return forecasting.
Related Tools
- Multi-Asset Return & Volatility Calculator — compare returns and rolling volatility across symbols.
- Compound Interest — project savings growth with contributions.
- FIRE / Retirement Calculator — when can you retire?
- Bond YTM — yield to maturity, duration, convexity.
Disclaimer
This tool is for educational and research purposes only. It does not constitute investment advice. Past performance is not indicative of future results. Trading involves substantial risk of loss. Always consult a licensed financial advisor before deploying real capital.
