Skip to content
Wildflower
Join free
Backtesting in Python (optional)

Free Trading School · Level 7 · Course 7.5

Avoiding look-ahead bugs

Lesson 10 of 15 in Backtesting in Python (optional)Module 2: Building a test

Advanced · Backtesting in Python

Avoiding look-ahead bugs

Wildflower Trading School · Lesson 7.5.2.5

Wildflower

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

  1. Add one shift to your own test, then deliberately remove it and compare the results.
  2. Then make a file of random made-up prices and run your test on it: a big edge there means something peeks.

Next: Results table and equity curve.

Quick check

  1. 1. What does .shift(1) on a signal column do?

  2. 2. Your test shows a big, steady edge on random made-up prices. What does that point to?

  3. 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

Make a free account and we'll remember where you are, so “Results table and equity curve” is waiting next time.

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.

  • (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 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 numbers

What the bug is worth on paper

Made-up numbers · , $5 a point · 60 signals

  1. Bug: 45.8 × $5 = $229 a trade
  2. × 60 signals = a fake $13,740
  3. Fixed: 0.48 × $5 = $2.40 a trade, before costs
  4. Same prices, same rule: one line apart.

A look-ahead bug can invent a result with no warning on screen.

Your turn

Drill

A 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.