Heuristic:24mlight A Share investment Agent Look ahead Bias Prevention
| Knowledge Sources | |
|---|---|
| Domains | Finance, Backtesting, Debugging |
| Last Updated | 2026-10-10 14:00 GMT |
Overview
Backtesting design rule that uses the previous trading day's close as the decision cutoff, preventing future information from leaking into historical trading decisions.
Description
The backtester ensures that when making a trading decision for day T, the agent workflow only receives data up to day T-1 (the previous trading day's close). The decision is made using a 30-day lookback window ending at T-1, then the trade is executed at day T's opening price. This prevents look-ahead bias — the most common and devastating error in backtesting where future data incorrectly influences historical decisions.
Usage
Use this heuristic when:
- Designing or modifying the backtest loop
- Deciding what data to pass to the agent workflow during backtesting
- Validating that backtest results are not artificially inflated
The Insight (Rule of Thumb)
- Action: Use
prev_rows.iloc[-1]["date"](the previous trading day) as the decision end date, not the current trading day. - Value:
lookback_start = decision_end - timedelta(days=30); trade executes atcurrent_date's open price. - Trade-off: Decisions are based on slightly stale data (yesterday's close) vs. today's open, but this correctly simulates real-world conditions where you cannot know today's close at the time of decision.
- Compatibility: Applies to all agents in the workflow; enforced at the
run_backtestlevel.
Reasoning
Look-ahead bias is the single most common source of inflated backtest results. If the agent sees today's closing price when making a decision for today, it effectively has a crystal ball. In the real world, at the time you decide to buy at today's open price, you only know yesterday's close. The backtester explicitly comments: 仅使用前一交易日作为决策截止,避免当日开盘成交时引入前视偏差 ("Use only the previous trading day as decision cutoff to avoid look-ahead bias when executing at today's open").
The 30-day lookback window (lookback_start = decision_end - timedelta(days=30)) provides sufficient historical context for technical analysis while staying within the no-future-data constraint.
Code Evidence
Look-ahead bias prevention from src/backtester.py:366-374:
if traded_days % max(1, int(self.config.decision_interval)) == 0:
# 仅使用前一交易日作为决策截止,避免当日开盘成交时引入前视偏差
prev_rows = price_df.loc[price_df["date"] < pd.to_datetime(current_date_str)]
if not prev_rows.empty:
decision_end = prev_rows.iloc[-1]["date"]
decision_end_str = decision_end.strftime("%Y-%m-%d")
lookback_start = (decision_end - timedelta(days=30)).strftime("%Y-%m-%d")
output = self._get_agent_decision(decision_end_str, lookback_start)
Trade execution at open price from src/backtester.py:357-360:
current_price = float(day_rows.iloc[-1].get("open", 0.0) or 0.0)
# ... decision is made, then trade executes at current_price (today's open)