{"record":{"id":"f97d265ece397ba8","repo":"HKUDS/Vibe-Trading","slug":"delay-requires-n-1-lookahead-ban-f97d26","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_049.py","lineNumber":57,"sourceCode":"    'id': 'alpha101_049',\n    'nickname': 'Kakushadze Alpha #49',\n    'theme': ['momentum'],\n    'formula_latex': '(((delay(close,20)-delay(close,10))/10 - (delay(close,10)-close)/10) < -0.1) ? 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_049.py#L39-L75","documentation":"In alpha_049.py, _delay enforces n >= 1 (lookahead ban) and raises ValueError on violation before df.shift runs. The neighboring _make_one helper shows the alpha builds conditional outputs, but the error always comes from the lag argument itself.","triggerScenarios":"A _delay(df, n) call with n <= 0 inside alpha #49's compute path — commonly a 0 typed in place of 1, or a lag derived as `k - 1` when k == 1.","commonSituations":"Manual transcription of Alpha#49's formula; copy-paste between alpha files; parameter grids including degenerate lags.","solutions":["Check each _delay argument in alpha_049.py against the intended formula","Use the unshifted operand for lag 0 semantics","Validate external lag parameters before compute() and fail with context","Smoke-test the factor after the fix"],"exampleFix":"// before\nterm = _delay(close, 0) - _delay(close, 3)\n// after\nterm = close - _delay(close, 3)","handlingStrategy":"validation","validationCode":"cleaned = [int(l) for l in lag_list if int(l) >= 1]\nif not cleaned:\n    raise ValueError('at least one valid lag (>= 1) required')","typeGuard":"def is_valid_delay(n: int) -> bool:\n    return isinstance(n, int) and not isinstance(n, bool) and n >= 1","tryCatchPattern":null,"preventionTips":["Never port 'delay(x, 0)' literally","Validate sweep grids for n >= 1","Use raw frame for identity lag"],"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"}