HKUDS/Vibe-Trading · error · ValueError

delay requires n >= 1 (lookahead ban)

Error message

delay requires n >= 1 (lookahead ban)

What it means

alpha_046.py's _make_one/_delay helpers back the alpha computation; _delay refuses n < 1 with ValueError because a zero-lag shift equals using the current bar, violating the repo's lookahead-safety contract for factors.

Source

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

    'id': 'alpha101_046',
    'nickname': 'Kakushadze Alpha #46',
    'theme': ['momentum'],
    'formula_latex': 'complex piecewise; see paper',
    '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. Verify every _delay argument in alpha_046.py is >= 1
  2. Use the unshifted frame when the formula means 'today'
  3. Sanitize parameter grids: filter out n < 1 before invoking the zoo
  4. Add a regression test pinning the lag values used

Example fix

// before
out = where(cond, _delay(close, 0), _make_one(close))
// after
out = where(cond, close, _make_one(close))
Defensive patterns

Strategy: validation

Validate before calling

if any(not (isinstance(n, int) and n >= 1) for n in [lag_a, lag_b]):
    raise ValueError('all lags must be ints >= 1')

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 #46 calls _delay(df, n) with n <= 0 while building its condition/ternary structure around _make_one frames.

Common situations: Off-by-one when porting Alpha#46's nested conditional formula; changing lag constants during tuning; sweep configs that allow 0 as a 'baseline' lag.

Related errors


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