HKUDS/Vibe-Trading · error · ValueError

delay requires n >= 1 (lookahead ban)

Error message

delay requires n >= 1 (lookahead ban)

What it means

alpha_037.py defines the same guard: _delay raises ValueError when n < 1 because a zero/negative shift would introduce lookahead bias into the computed factor. The check runs unconditionally at the top of the helper.

Source

Thrown at agent/src/factors/zoo/alpha101/alpha_037.py:57

    'id': 'alpha101_037',
    'nickname': 'Kakushadze Alpha #37',
    'theme': ['momentum'],
    'formula_latex': 'rank(correlation(delay(open-close,1),close,200)) + rank(open-close)',
    'columns_required': ['open', 'close'],
    'extras_required': [],
    'requires_sector': False,
    'universe': ['equity_us', 'equity_in', 'equity_kr'],
    'frequency': ['1D'],
    'decay_horizon': 5,
    'min_warmup_bars': 201,
    'notes': 'Very long lookback (>= ~100 bars); produces NaN warmup on short panels which may trigger the >95% NaN registry guard.',
}


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"]
    open_ = panel["open"]


    # Helper aliases (local closures keep the file standalone & purity-safe).
    delay = _delay
    out = rank(ts_corr(delay(open_ - close, 1), close, 200)) + rank(open_ - close)
    return out

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Check each _delay(...) argument in alpha_037.py against the reference formula and fix any 0/negative value
  2. Use the operand directly if the current bar is genuinely wanted
  3. Bound user-supplied parameters: `n = max(1, n)` only if 0 is semantically acceptable as 'no lag'
  4. Add a smoke test over a small OHLCV panel

Example fix

// before
sig = _delay(close, 0) - _delay(close, 5)
// after
sig = close - _delay(close, 5)
Defensive patterns

Strategy: validation

Validate before calling

for name, n in {'lag_short': lag_short, 'lag_long': lag_long}.items():
    if not (isinstance(n, int) and n >= 1):
        raise ValueError(f"{name} must be an int >= 1, got {n!r}")

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: compute(panel) for alpha #37 passes a non-positive n to _delay — a mistyped constant (0) or a computed offset that collapses to 0 for certain parameter values.

Common situations: Editing the delay chain of Alpha#37 (which mixes multiple lags) and getting one argument wrong; parameter sweeps that include degenerate values; copy-paste between alpha files with off-by-one edits.

Related errors


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