HKUDS/Vibe-Trading · error · ValueError

delay requires n >= 1 (lookahead ban)

Error message

delay requires n >= 1 (lookahead ban)

What it means

The _delay helper in alpha_045.py raises ValueError for n < 1 to enforce the lookahead ban — df.shift(0) would return current-bar data and taint the factor with information unavailable at decision time. The guard fires before any pandas work happens.

Source

Thrown at agent/src/factors/zoo/alpha101/alpha_045.py:62

    'extras_required': [],
    'requires_sector': False,
    'universe': ['equity_us', 'equity_in', 'equity_kr'],
    'frequency': ['1D'],
    'decay_horizon': 5,
    'min_warmup_bars': 25,
    'notes': '',
}


def _rolling_sum(df: pd.DataFrame, n: int) -> pd.DataFrame:
    """Rolling window sum; warmup -> NaN."""
    return df.rolling(window=n, min_periods=n).sum()


def _delay(df: pd.DataFrame, n: int) -> pd.DataFrame:
    """Backward shift by n (lookahead-safe; n>=1 required)."""
    if n < 1:
        raise ValueError("delay requires n >= 1 (lookahead ban)")
    return df.shift(n)


def compute(panel: dict) -> pd.DataFrame:
    """Compute the alpha on the OHLCV+ panel and return a wide DataFrame."""
    close = panel["close"]
    volume = panel["volume"]


    # Helper aliases (local closures keep the file standalone & purity-safe).
    rolling_sum = _rolling_sum
    delay = _delay
    out = -1.0 * (rank(rolling_sum(delay(close, 5), 20) / 20.0) * ts_corr(close, volume, 2) * rank(ts_corr(rolling_sum(close, 5), rolling_sum(close, 20), 2)))
    return out

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Audit all _delay call sites in alpha_045.py for arguments >= 1
  2. Replace _delay(x, 0) with x where identity is intended
  3. If lags are external, validate them at the boundary with a clear error naming the parameter
  4. Run the factor over synthetic data to confirm the fix

Example fix

// before
v = _delay(volume, 0) / _delay(volume, 5)
// after
v = volume / _delay(volume, 5)
Defensive patterns

Strategy: validation

Validate before calling

def clamp_lag(n):
    return max(1, int(n)) if str(n).strip() not in ('', '0') else 1
# better: reject explicitly
if n < 1: raise ValueError(f"lag must be >= 1, got {n}")

Type guard

def is_valid_delay(n: int) -> bool:
    return isinstance(n, int) and not isinstance(n, bool) and n >= 1

Prevention

When it happens

Trigger: A _delay call inside alpha #45's compute() receives 0 or a negative int, e.g. `_delay(x, k)` where k is computed from another window and hits 0.

Common situations: Transcribing Alpha#45 (which combines rank/correlation with delays) and mis-entering one lag; refactoring the helper's callers; config-driven lag grids including 0.

Related errors


AI-assisted analysis of HKUDS/Vibe-Trading@80ffdda44c (2026-08-28). Data as JSON: /api/errors/544ca8ec2b6dc275. Report an issue: GitHub.