fix: preserve NaNs in cross-sectional ranks

This commit is contained in:
2026-04-18 16:14:25 +08:00
parent aa053605de
commit 40ec3b828a
3 changed files with 18 additions and 8 deletions

View File

@@ -36,9 +36,13 @@ class RecoveryMomentumStrategy(Strategy):
# Factor 2: 12-1 month momentum
momentum = data.shift(self.mom_skip).pct_change(self.mom_lookback - self.mom_skip)
# Cross-sectional percentile ranks
rec_rank = recovery.rank(axis=1, pct=True, na_option="bottom")
mom_rank = momentum.rank(axis=1, pct=True, na_option="bottom")
# Cross-sectional percentile ranks. na_option="keep" is critical:
# with "bottom" + ascending=True default, NaN stocks get pct=1.0, so
# non-member / delisted / pre-IPO names leak into the composite as
# "top" candidates. "keep" propagates NaN and the final ascending=False
# + "bottom" rank pushes them to the end where they are not selected.
rec_rank = recovery.rank(axis=1, pct=True, na_option="keep")
mom_rank = momentum.rank(axis=1, pct=True, na_option="keep")
# Composite score (50/50)
composite = 0.5 * rec_rank + 0.5 * mom_rank