{"record":{"id":"e9626d144fe33604","repo":"HKUDS/Vibe-Trading","slug":"delay-requires-n-1-lookahead-ban-e9626d","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_046.py","lineNumber":57,"sourceCode":"    'id': 'alpha101_046',\n    'nickname': 'Kakushadze Alpha #46',\n    'theme': ['momentum'],\n    'formula_latex': 'complex piecewise; see paper',\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_046.py#L39-L75","documentation":"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.","triggerScenarios":"compute() for alpha #46 calls _delay(df, n) with n <= 0 while building its condition/ternary structure around _make_one frames.","commonSituations":"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.","solutions":["Verify every _delay argument in alpha_046.py is >= 1","Use the unshifted frame when the formula means 'today'","Sanitize parameter grids: filter out n < 1 before invoking the zoo","Add a regression test pinning the lag values used"],"exampleFix":"// before\nout = where(cond, _delay(close, 0), _make_one(close))\n// after\nout = where(cond, close, _make_one(close))","handlingStrategy":"validation","validationCode":"if any(not (isinstance(n, int) and n >= 1) for n in [lag_a, lag_b]):\n    raise ValueError('all lags must be ints >= 1')","typeGuard":"def is_valid_delay(n: int) -> bool:\n    return isinstance(n, int) and not isinstance(n, bool) and n >= 1","tryCatchPattern":null,"preventionTips":["Use x directly instead of _delay(x, 0) in conditionals","Validate lag grids at sweep configuration time","Review ported formulas term by term"],"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"}