HKUDS/Vibe-Trading · error · ValueError

delay requires n >= 1 (lookahead ban)

Error message

delay requires n >= 1 (lookahead ban)

What it means

alpha_051.py's _delay helper raises ValueError('delay requires n >= 1 (lookahead ban)') whenever the requested shift is zero or negative, preventing current/future bars from leaking into the factor. It is a caller-argument bug, not a data problem.

Source

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

    'id': 'alpha101_051',
    'nickname': 'Kakushadze Alpha #51',
    'theme': ['momentum'],
    'formula_latex': '(...< -0.05) ? 1 : -1*(close-delay(close,1))',
    'columns_required': ['close'],
    'extras_required': [],
    'requires_sector': False,
    'universe': ['equity_us', 'equity_in', 'equity_kr'],
    'frequency': ['1D'],
    'decay_horizon': 5,
    'min_warmup_bars': 21,
    'notes': '',
}


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 _where_ternary(cond, a, b):
    """Vectorised ternary `(cond ? a : b)` returning a DataFrame.

    ``cond`` is a boolean DataFrame; ``a`` / ``b`` may be DataFrame or scalar.
    """
    if isinstance(a, (int, float)):
        a_arr = np.full_like(cond.to_numpy(dtype=np.float64), float(a))
    else:
        a_arr = a.to_numpy(dtype=np.float64, na_value=np.nan)
    if isinstance(b, (int, float)):

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Audit _delay call sites in alpha_051.py for arguments >= 1 and correct them
  2. Replace _delay(x, 0) with x where identity is intended
  3. Filter parameter grids to n >= 1 before running the zoo
  4. Add a pinned unit test for the lag constants

Example fix

// before
left = _delay(close, 0) * _make_one(close)
// after
left = close * _make_one(close)
Defensive patterns

Strategy: validation

Validate before calling

for n in (lag_x, lag_y):
    if not (isinstance(n, int) and n >= 1):
        raise ValueError(f'lag must be 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() for alpha #51 invokes _delay with n <= 0 while combining it with _make_one constant frames in the alpha's conditional logic.

Common situations: Porting Alpha#51 and mistyping a lag; sweeps/tuning configs that pass 0 lags; off-by-one arithmetic on lag expressions.

Related errors


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