Look-ahead bias occurs when information unavailable at decision time influences a prediction or action. It can make a backtest look excellent while hiding performance that would vanish in live use. section 12.9.3
Prevent it through the design of the decision process:
- Build the state from information available up to the decision time. section 12.9.3
- Commit to the action before revealing the next period’s returns. section 12.4
- Train on earlier data and evaluate on a later chronological holdout. section 12.9.3
A suspiciously strong backtest
An agent that sees tomorrow’s returns could hold assets that rise and avoid those that fall. In the cited test, this leak produced a Sharpe ratio above 20, versus 8.47 for the best honest mode; the apparent advantage would disappear when the future is unknown. section 12.9.3
Listing 12.4 The step function, ordered to prevent look-ahead (summarized)
def step_target_weights(self, target_w, action_name="custom"):
target_w = np.asarray(target_w,
➥ dtype=np.float64).copy()
turnover = 0.5 * np.sum(np.abs(target_w - self.weights)) #A
transaction_cost = self.cfg.tx_cost_rate * turnover #B
next_ret = np.zeros(self.n_slots)
next_ret[: self.n_assets] = self.returns[self.day + 1] #C
gross_return = float(np.dot(target_w, next_ret)) #D
net_return = gross_return - transaction_cost
self.value *= (1.0 + net_return)
self.peak_value = max(self.peak_value, self.value)
drawdown = self.value / self.peak_value - 1.0 #E
grown = target_w * (1.0 + next_ret)
denom = grown.sum()
self.weights = grown / denom if denom >
➥ 1e-12 else target_w
port_vol = float(np.std(self.returns_history)) if
➥ len(self.returns_history) >= 2 else 0.0
reward = (gross_return
- transaction_cost
- self.cfg.turnover_penalty_coef * turnover
- self.cfg.drawdown_penalty_coef * (-drawdown)
- self.cfg.vol_penalty_coef * port_vol) *
➥ self.cfg.reward_scale #F
self.day += 1 #G
self.step_count += 1
done = self.step_count >= self.cfg.horizon
return self._get_state(), float(reward), done, infoSee how the step function calculates the next return only after the target allocation is set. section 12.4

