In Level 4 you met look-ahead bias: a test using a price it couldn't have known yet. In code, it's an easy bug to write and a hard one to see. By the end of this lesson, you'll know why code is so prone to it, and how one shift fixes the usual case. You'll also have a simple way to catch the bug before it fools you.
1 of 9
What you'll learn
- Spot the peek. Every row is there at once, so code can use prices it couldn't know.
- Shift signals to the next bar. This stops a signal using a bar it hasn't seen.
- Shift rolling highs too. A rolling max includes today's bar unless you shift it.
- Catch other peeks. Code like shift(-1), centred windows and whole-file levels looks ahead.
- Test on random prices. A strong, steady edge there points to a bug.
Exercise
- Add one shift to your own test, then deliberately remove it and compare the results.
- Then make a file of random made-up prices and run your test on it: a big edge there means something peeks.
Quick check
1. What does .shift(1) on a signal column do?
2. Your test shows a big, steady edge on random made-up prices. What does that point to?
3. Why does close > df["high"].rolling(20).max() never fire?
0 of 3 answered.
Finished this lesson?
Tick it off and keep your place
Read the full notes
In a table every row is there at once, so code can peek at prices it couldn't have known. Shifting a signal to the next bar fixes the usual case, and a strong edge on random prices points to a bug.
- A backtest mistake: using a price the strategy couldn’t have known yet at the moment it made its decision.More in the glossary (Look-ahead bias) is easy in code: every row of the table is there at once, past and future.
- The bug: taking each day's move on the signal days themselves buys at the open before the close that made the signal. On made-up random prices: 45.8 One whole unit of a price. On the S&P 500, a move from 5,000 to 5,001 is one point, and each futures contract sets what a point is worth in dollars.More in the glossary a trade.
- The fix: trade_day = df["cross_up"].shift(1, fill_value=False) moves each signal to the next bar. Same prices: 0.48 points a trade. Random prices have nothing to find, so the honest test finds about nothing.
- The silent cousin: df["high"].rolling(20).max() includes today's bar, so close > high20 never happens (0 signals). Add .shift(1): 55.
- Other peeks: shift(-1) and rolling(center=True) look forward; levels from the whole file; a day's high or low used before the day ends. Check: run the test on random made-up prices. A strong, steady edge there points to a bug. More: Look-ahead bias.
Worked example
Made-up numbersWhat the bug is worth on paper
Made-up numbers · A small futures contract on the S&P 500, traded at CME Group. Each point is worth $5, and its smallest step of 0.25 is worth $1.25.More in the glossary, $5 a point · 60 signals
- Bug: 45.8 × $5 = $229 a trade
- × 60 signals = a fake $13,740
- Fixed: 0.48 × $5 = $2.40 a trade, before costs
- Same prices, same rule: one line apart.
A look-ahead bug can invent a result with no warning on screen.
Your turn
DrillA signal fires on bar 120's close
Trading is risky. You can lose money. Test results are hypothetical, and a test can't tell you what will happen next. This lesson explains how testing works. It isn't advice.