What is Backtesting?
Backtesting is like a simulated test of your trading strategy using historical market data.
Imagine you have a rule: “Buy a stock if its 10-day moving average crosses above the 50-
day moving average.”
Backtesting applies this rule to past data to see how it would have performed.
It helps you estimate potential profits and risks before trading live.
2 Look-ahead bias
Definition: When your strategy accidentally uses future information that wouldn’t be available
at the time of trading.
Example:
You use tomorrow’s closing price to decide today’s trade.
This is cheating, because in real life, you cannot know the future.
Effect: Overestimates your strategy’s performance.
Avoid it: Always make sure your strategy only uses data available at that time.
3 Survivorship bias
Definition: Only testing your strategy on companies that survived until today, ignoring ones
that went bankrupt or were delisted.
Example:
You backtest S&P 500 stocks over 10 years but ignore companies that failed and got
removed from the index.
You’ll see better returns than reality because failing companies are excluded.
Effect: Overestimates profitability.
Avoid it: Use historical datasets that include delisted companies.
4 Overfitting
Definition: Making a strategy fit past data too perfectly, like memorizing it instead of learning
patterns.
Example:
You tweak a strategy to make it perfect on last 5 years of data.
But it fails on new data, because the strategy only fits the past noise.
Simple analogy:
It’s like memorizing the answers to a past exam but failing the new exam.
Formula to measure overfitting (simplified):
One method is to compare training vs testing performance:
Overfitting risk=Performance on backtest−Performance on new data\text{Overfitting risk} =
\text{Performance on backtest} - \text{Performance on new
data}Overfitting risk=Performance on backtest−Performance on new data
If the backtest return is 20% but new data return is 2%, your strategy is overfitted.
5 Slippage
Definition: The difference between the expected price of a trade and the actual price you get in
the market.
Example:
You plan to buy a stock at $100, but by the time your order executes, the price is
$100.50.
That extra $0.50 is slippage.
Effect: Reduces actual profits, especially in fast-moving or low-volume markets.
Tip: Include slippage in backtesting for realism.
6 Transaction costs
Definition: Fees, commissions, or taxes you pay when trading.
Example:
Buying 100 shares with $0.10 per share commission = $10 cost.
Selling also costs $10.
Effect: Can turn a profitable strategy into a losing one if costs are ignored.
Backtest formula including costs:
Net Profit=Gross Profit−Transaction Costs−Slippage\text{Net Profit} = \text{Gross Profit} -
\text{Transaction Costs} - \text{Slippage}Net Profit=Gross Profit−Transaction Costs−Slippage
✅Summary:
Backtesting isn’t just running your strategy on past data. You must account for:
1. Look-ahead bias → don’t cheat by using future data.
2. Survivorship bias → include failed/delisted stocks.
3. Overfitting → avoid memorizing past noise; test on new data.
4. Slippage → market price can differ.
5. Transaction costs → fees reduce profits.
1 Equity Curve
Definition: A graph showing how your account balance changes over time when using your
strategy.
Example:
You start with $10,000.
After some trades, it grows to $12,000, drops to $11,000, then goes up to $13,500.
Plotting these values over time gives your equity curve.
Why it’s important:
Shows profit trends and risk periods.
A smooth upward curve → stable strategy.
A jagged curve with big drops → risky strategy.
Formula (simple):
Equity at time t=Starting capital+∑(profits/losses from trades up to time t)\text{Equity at time t}
= \text{Starting capital} + \sum (\text{profits/losses from trades up to time
t})Equity at time t=Starting capital+∑(profits/losses from trades up to time t)
2 Rolling Metrics
Definition: Measuring performance using moving windows over time instead of a single
number.
Why: Markets change. A strategy might perform well now but fail later. Rolling metrics show
performance trends.
Examples:
Rolling Sharpe ratio: Measures risk-adjusted return over the last 30 days (or any
period).
Rolling max drawdown: Largest loss over the last 60 days.
Formula (Rolling Sharpe ratio, simplified):
Sharpe ratiorolling=mean(Returns over window)std(Returns over window)\text{Sharpe
ratio}_{\text{rolling}} = \frac{\text{mean(Returns over window)}}{\text{std(Returns over
window)}}Sharpe ratiorolling=std(Returns over window)mean(Returns over window)
mean(Returns over window) → average return in the window.
std(Returns over window) → standard deviation of returns in the window.
3 Out-of-sample Testing
Definition: Testing your strategy on data that was NOT used to create or tune the strategy.
Why: Prevents overfitting. It shows real-world performance.
Example:
1. You use 2010–2018 data to design your strategy → in-sample.
2. Then test it on 2019–2020 data → out-of-sample.
Good practice:
If strategy works in-sample but fails out-of-sample → probably overfitted.
If it works well in both → strategy is more robust.
Formula (performance on out-of-sample):
Out-of-sample return=Final equity−Starting equityStarting equity\text{Out-of-sample return} =
\frac{\text{Final equity} - \text{Starting equity}}{\text{Starting equity}}Out-of-
sample return=Starting equityFinal equity−Starting equity
Example: Start $10,000 → End $11,500 → Return = (11,500 - 10,000)/10,000 = 0.15 →
15%.
✅Summary:
Metric What it tells you Formula/Idea
Growth of account over Equity = Starting capital + cumulative
Equity curve
time profits/losses
Performance trends over Rolling Sharpe = mean/SD of returns over a
Rolling metrics
time moving window
Out-of-sample Return = (Final equity - Starting equity)/Starting
Real-world robustness
testing equity