fix(equity-factor): guard non-positive recent price in momentum_12_1 (delisted-name safety)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
jgrusewski
2026-06-17 12:24:29 +02:00
parent d7c055c927
commit a93cf05fab
2 changed files with 13 additions and 1 deletions

View File

@@ -29,7 +29,7 @@ def momentum_12_1_from_closes(closes: list[float]) -> float | None:
return None
p_then = closes[-_LOOKBACK]
p_recent = closes[-_SKIP]
if p_then is None or p_then <= 0:
if p_then is None or p_then <= 0 or p_recent is None or p_recent <= 0:
return None
return p_recent / p_then - 1.0

View File

@@ -19,6 +19,18 @@ def test_momentum_12_1_none_when_insufficient_history():
assert momentum_12_1_from_closes(_ramp(50)) is None
def test_momentum_12_1_none_when_recent_price_nonpositive():
closes = _ramp(300)
closes[-21] = 0.0 # the "recent" (skip) price is non-positive
assert momentum_12_1_from_closes(closes) is None
def test_momentum_12_1_none_when_lookback_price_nonpositive():
closes = _ramp(300)
closes[-252] = 0.0 # the lookback base price is non-positive
assert momentum_12_1_from_closes(closes) is None
def test_realized_vol_zero_for_constant_series():
v = realized_vol_from_closes([100.0] * 120)
assert v is not None and v == 0.0