Skip to Content
While25SDK docs
Backtesting

Without peeking ahead

The leaks that make a backtest look better than it can trade, and how to avoid each one.

A backtest here is your own Python: any model, any number of instruments, any mix of timeframes. That freedom has one classic way to go wrong. The logic uses something it could not have known at the moment it decided, and the results look far better than anything it could repeat live. The seal checks your trades against the history you name; it cannot check what your logic knew when it decided. That part is yours, and these are the usual leaks.

A bar is known only once it has closed. A bar's time is when it starts. It is known at its start plus its interval: a 1D bar stamped Monday is known after Monday's close, never at 10:30 on Monday. A decision at time t may read a bar only if it had ended by t:

import bisect

STEP_MS = {"1m": 60_000, "5m": 300_000, "15m": 900_000, "30m": 1_800_000,
           "1h": 3_600_000, "1D": 86_400_000}

def closed_by(bars, interval, t_ms):
    """The bars of one line that had ended by t_ms, oldest first."""
    ends = [b["time"] + STEP_MS[interval] for b in bars]
    return bars[:bisect.bisect_right(ends, t_ms)]

Mixing timeframes. A daily trend filter over 15-minute entries reads the daily line as it stood when each 15-minute bar ended, never today's daily bar:

daily = while25.history_span("EQ:US:SPY", "1D", "2024-01-01", "2025-06-30")["bars"]
m15 = while25.history_span("EQ:US:SPY", "15m", "2024-06-03", "2025-06-30")["bars"]

for i, bar in enumerate(m15[:-1]):
    decided_at = bar["time"] + STEP_MS["15m"]          # this bar has just closed
    known = closed_by(daily, "1D", decided_at)          # yesterday's close at most
    if len(known) < 50:
        continue
    trend_up = known[-1]["close"] > sum(b["close"] for b in known[-50:]) / 50
    ...                                                  # decide on bar, fill on m15[i + 1]

Decide on a close, fill after it. A signal computed from a bar's close cannot fill at that bar's open, or at that same close as if you had been quicker than the print. Fill at the next bar's open: backtest.fill(bars[i + 1], "buy", "open", instrument=inst).

Fit only on the past. Anything learned from data (a model's weights, a threshold you tuned, the mean and spread you scale features by) is learned from bars before the stretch it is tested on. Walk forward: train on one window, test on the next, then move both along, and seal only the test windows' trades. Tuning a parameter until the whole span looks good, then reporting that span, is fitting on the test.

Today's index is not the past's. while25.universe("sp500") is the membership as of universe_info("sp500")["as_of"]. A backtest of "the S&P 500" over earlier years with today's members leaves out the companies that fell out, and flatters the result. Say so in notes, or test on a fixed list you chose for a reason.

Revised numbers. Split-adjusted history (adjust="split") changes old prices to today's share count; compute on one adjustment throughout and fill on the prices the seal will check.

Worked versions of each are in examples/backtests/.