Stocks course · Section 5: Risk, Leverage and Testing
How to Backtest a Stock Strategy Properly: Survivorship, Look-Ahead and Costs
Test a stock strategy honestly: complete rules, survivorship-free adjusted data, no look-ahead, realistic costs, fair benchmarks and overfitting checks.
Advanced6 min read·7:00 video·Lesson 21 of 26·quiz: 4 questions
▶ Video lesson · 7:00Watch on YouTube ↗
On this page +
- What a backtest can and cannot tell you
- Step 1: write the rules completely
- Step 2: use honest data
- Survivorship bias
- Corporate actions
- Step 3: avoid look-ahead bias
- Step 4: include realistic costs
- Step 5: judge results with the right measures
- Step 6: guard against overfitting
- How DanioX tests stock strategies
A backtest applies a strategy's exact rules to past prices to see how it would have performed. Done properly, it uses data that includes companies that no longer exist, adjusts for splits and dividends, makes every decision with information that was available at the time, trades at realistic prices after costs, and compares the result with a fair benchmark. Done carelessly, it produces impressive numbers that vanish the moment you trade real money.
What a backtest can and cannot tell you
A good backtest can show that a rule is not worth trading, which saves money, and give a realistic picture of drawdowns, losing streaks and how often the rule trades. It cannot prove the future will look like the past. Treat a passed backtest as permission to paper trade or trade small, not as a promise.
Step 1: write the rules completely
If a rule needs your judgement, it cannot be tested. Write down:
- Universe: which stocks and how the list is chosen.
- Signal timing: decided on the daily close, traded at the next open.
- Entry, exit, stop and time limit.
- Sizing: fixed dollar amount, equal slots, or risk-based (see position sizing).
- Costs per trade.
- Pass/fail tests, written before you see a single result.
Step 2: use honest data
Survivorship bias
If your data only contains companies that exist today, it has quietly removed every company that went bankrupt, was delisted or shrank. Those are mostly losers, so any strategy that buys stocks looks better than it really was.
Example
You take today's 100 largest US-listed companies and test a strategy on them over 20 years. Many of them were mid-sized 20 years ago and became giants because their shares rose enormously. A strategy that simply holds them will look brilliant, and so will almost any rule that buys them. The companies that were large 20 years ago and later collapsed are not in your test at all.
Two fixes: use point-in-time data that lists which companies were really in the universe on each date, delisted ones included; or, if you must use today's list, compare your rule with simply holding the same list, which carries the same bias. Only beating that basket shows that the rule adds something.
Corporate actions
- Splits: in a 4-for-1 split, a $400 share becomes four $100 shares. Unadjusted data shows a 75% crash that triggers every stop. Use split-adjusted prices.
- Dividends: price-only data understates buy-and-hold returns for dividend payers. Use total-return (dividend-adjusted) prices, or add dividends separately.
- Mergers, spin-offs, ticker changes and delistings: each needs handling, or positions vanish or jump in the data.
Step 3: avoid look-ahead bias
Look-ahead bias means using information that was not available when the decision was made.
- Price signals: if a signal uses today's close, you cannot also buy at today's close. Buy at the next open.
- Fundamentals: a company's fourth-quarter results are often published weeks after the quarter ends. Use each figure from its publication date, not the period end.
- Index membership: use the members of the index on each date, not today's members.
- Stops: if the day's low touches your stop, assume you are filled at the stop or, if the stock gapped through it, at the open.
Step 4: include realistic costs
Costs per trade include commission, the bid-ask spread, slippage and, where relevant, margin interest and borrow fees.
Example
A strategy averages +0.4% per trade before costs and trades 120 times a year. At 0.05% per side, a round trip costs 0.1% and the average trade keeps +0.3%. At 0.15% per side, a round trip costs 0.3% and only +0.1% is left. At 0.25% per side the strategy loses money. The more often a strategy trades, the more its result depends on cost assumptions.
Taxes, too, are higher for frequent traders in many countries (see stock taxes and record keeping).
Step 5: judge results with the right measures
| Measure | What it tells you |
|---|---|
| Number of trades | Whether the sample is big enough to mean anything |
| Average trade and profit factor (gross profits / gross losses) | Whether the edge survives costs |
| Win rate with average win and loss | The shape of returns and the losing streaks to expect |
| Compound annual return | Growth of the whole portfolio |
| Maximum drawdown | The worst peak-to-trough fall you would have lived through |
| Sharpe ratio | Return per unit of volatility, for comparing strategies with different risk |
| Results by year and by half of the period | Whether the edge is steady or came from one lucky stretch |
Compare every result with at least two benchmarks: an S&P 500 index fund, and buy-and-hold of the same stock list.
Step 6: guard against overfitting
Overfitting means tuning rules until they fit the noise of the past. Warning signs and defences:
- Too many settings. Every extra parameter makes it easier to fit noise. Prefer simple rules.
- Fragile settings. If 47/193-day averages work but 45/190 and 50/200 do not, the result is probably luck.
- Too many tries. Test 100 random rules and a few will look great by chance. Count and report every variation you tried.
- No out-of-sample check. Split the data: develop on one period, then check once on another you have not looked at.
- Rules from nowhere. Rules based on published research, written down before the first test, are less likely to be accidents of the data.
How DanioX tests stock strategies
The DanioX stocks backtest follows these principles and states its own limits. Its rules come from published research and were written down, untuned, before the first run. Signals are decided on a close and traded at the next open, stops fill when the day's low touches them, a fixed cost is charged on every buy and sell, and prices are adjusted for splits and dividends. Each strategy must pass checks fixed in advance, including enough trades and profits in both halves of the period. Because the universe is today's largest companies, the page measures every rule against an equal-weight basket of the same stocks as well as the S&P 500. The methodology page explains the approach across all DanioX markets.
Risk
Even an honest backtest is one path through history. Live results usually come in below backtests because of real fills, changing markets and your own behaviour. Start small and compare live trades with the test (see trading psychology and journaling).
Tip
Before running any test, write a one-page "test plan" with the rules, data source, costs and pass/fail checks, and date it. If you change anything after seeing results, write down what and why. This one habit prevents most self-deception.
Key takeaways
- Write every rule before testing: universe, entry, exit, stop, sizing, timing and costs.
- Survivorship bias (testing only companies that survived and grew) makes almost any long strategy look good.
- Decide on information available at the time and trade at the next realistic price; fundamentals count from their publication date.
- Costs can turn a small gross edge into a loss, especially for high-turnover strategies.
- Fix pass/fail tests in advance, check results in separate periods, and compare with a benchmark that shares the same biases.
Quiz: check what you learned
0/4 answeredAnswer every question to finish the lesson. Wrong answers are fine: read the explanation and try again.
⚠ Not financial advice — educational signals only. Read the disclaimer.