HKUDS/Vibe-Trading · error · ValueError

delay requires n >= 1 (lookahead ban)

Error message

delay requires n >= 1 (lookahead ban)

What it means

In alpha_047.py, _delay guards df.shift(n) with an explicit n >= 1 requirement (lookahead ban) and raises ValueError otherwise. The helper sits next to _make_one, used to construct constant frames for the alpha's where/ternary logic.

Source

Thrown at agent/src/factors/zoo/alpha101/alpha_047.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 _make_one(ref: pd.DataFrame) -> pd.DataFrame:
    """A DataFrame of 1.0 with the same shape/index/columns as ``ref``."""
    return pd.DataFrame(1.0, index=ref.index, columns=ref.columns)


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

    # Helper aliases (local closures keep the file standalone & purity-safe).
    rolling_sum = _rolling_sum

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Read each _delay call in alpha_047.py and correct non-positive lags
  2. Substitute the raw DataFrame where delay 0 is intended
  3. Validate lag configs at load time (reject < 1 loudly and early)
  4. Re-run compute on a fixture panel to verify

Example fix

// before
x = _delay(close, 0) * _delay(close, 1)
// after
x = close * _delay(close, 1)
Defensive patterns

Strategy: validation

Validate before calling

lags = [int(l) for l in lags]
bad = [l for l in lags if l < 1]
if bad:
    raise ValueError(f"lags must be >= 1, offending values: {bad}")

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: Any _delay(..., 0) (or negative) call in alpha #47's compute chain — typically a lag typo or derived expression hitting 0.

Common situations: Hand-porting Alpha#47's conditional expression and dropping a lag to 0; batch-editing alpha files; parameterized backtests with unvalidated lag lists.

Related errors


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