{"record":{"id":"3b78143058ce37b6","repo":"HKUDS/Vibe-Trading","slug":"delay-requires-n-1-lookahead-ban-3b7814","errorCode":null,"errorMessage":"delay requires n >= 1 (lookahead ban)","messagePattern":"delay requires n >= 1 \\(lookahead ban\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"agent/src/factors/zoo/alpha101/alpha_051.py","lineNumber":57,"sourceCode":"    'id': 'alpha101_051',\n    'nickname': 'Kakushadze Alpha #51',\n    'theme': ['momentum'],\n    'formula_latex': '(...< -0.05) ? 1 : -1*(close-delay(close,1))',\n    'columns_required': ['close'],\n    'extras_required': [],\n    'requires_sector': False,\n    'universe': ['equity_us', 'equity_in', 'equity_kr'],\n    'frequency': ['1D'],\n    'decay_horizon': 5,\n    'min_warmup_bars': 21,\n    'notes': '',\n}\n\n\ndef _delay(df: pd.DataFrame, n: int) -> pd.DataFrame:\n    \"\"\"Backward shift by n (lookahead-safe; n>=1 required).\"\"\"\n    if n < 1:\n        raise ValueError(\"delay requires n >= 1 (lookahead ban)\")\n    return df.shift(n)\n\n\ndef _make_one(ref: pd.DataFrame) -> pd.DataFrame:\n    \"\"\"A DataFrame of 1.0 with the same shape/index/columns as ``ref``.\"\"\"\n    return pd.DataFrame(1.0, index=ref.index, columns=ref.columns)\n\n\ndef _where_ternary(cond, a, b):\n    \"\"\"Vectorised ternary `(cond ? a : b)` returning a DataFrame.\n\n    ``cond`` is a boolean DataFrame; ``a`` / ``b`` may be DataFrame or scalar.\n    \"\"\"\n    if isinstance(a, (int, float)):\n        a_arr = np.full_like(cond.to_numpy(dtype=np.float64), float(a))\n    else:\n        a_arr = a.to_numpy(dtype=np.float64, na_value=np.nan)\n    if isinstance(b, (int, float)):","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/factors/zoo/alpha101/alpha_051.py#L39-L75","documentation":"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.","triggerScenarios":"compute() for alpha #51 invokes _delay with n <= 0 while combining it with _make_one constant frames in the alpha's conditional logic.","commonSituations":"Porting Alpha#51 and mistyping a lag; sweeps/tuning configs that pass 0 lags; off-by-one arithmetic on lag expressions.","solutions":["Audit _delay call sites in alpha_051.py for arguments >= 1 and correct them","Replace _delay(x, 0) with x where identity is intended","Filter parameter grids to n >= 1 before running the zoo","Add a pinned unit test for the lag constants"],"exampleFix":"// before\nleft = _delay(close, 0) * _make_one(close)\n// after\nleft = close * _make_one(close)","handlingStrategy":"validation","validationCode":"for n in (lag_x, lag_y):\n    if not (isinstance(n, int) and n >= 1):\n        raise ValueError(f'lag must be int >= 1, got {n!r}')","typeGuard":"def is_valid_delay(n: int) -> bool:\n    return isinstance(n, int) and not isinstance(n, bool) and n >= 1","tryCatchPattern":null,"preventionTips":["Substitute the operand for delay 0","Filter lag parameters at config load","Test compute() on a fixture panel"],"tags":["pandas","alpha101","lookahead-bias","input-validation"],"backgroundTag":"invalid-window-argument","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}