fix: unify backtest execution and add forward validation candidates
Share point-in-time signals and a cash-funded order ledger across backtests and trading. Correct eligibility, missing-price fills, quality factors, regime inputs, warmup windows, and cash-injection accounting. Add reproducible registered-strategy comparisons and paired forward candidates with a 20% resize band. Validation: 125 Python tests, 23 web tests, TypeScript/Vite build, 87 strategy causality checks, and real-data target/order/equity parity for 10 strategies.
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from strategies.base import Strategy
|
||||
from strategies.base import StockStrategy, mask_scores
|
||||
|
||||
|
||||
class RecoveryMomentumStrategy(Strategy):
|
||||
class RecoveryMomentumStrategy(StockStrategy):
|
||||
"""
|
||||
Recovery + momentum composite factor strategy.
|
||||
|
||||
@@ -29,26 +29,27 @@ class RecoveryMomentumStrategy(Strategy):
|
||||
self.rebal_freq = rebal_freq
|
||||
self.top_n = top_n
|
||||
|
||||
def generate_signals(self, data: pd.DataFrame) -> pd.DataFrame:
|
||||
def generate_signals(self, data: pd.DataFrame, eligible: pd.DataFrame | None = None) -> pd.DataFrame:
|
||||
# Factor 1: Recovery — price / rolling min
|
||||
recovery = data / data.rolling(self.recovery_window).min() - 1
|
||||
|
||||
# Factor 2: 12-1 month momentum
|
||||
momentum = data.shift(self.mom_skip).pct_change(self.mom_lookback - self.mom_skip)
|
||||
momentum = data.shift(self.mom_skip).pct_change(self.mom_lookback - self.mom_skip, fill_method=None)
|
||||
|
||||
# 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")
|
||||
rec_rank = mask_scores(recovery, eligible).rank(axis=1, pct=True, na_option="keep")
|
||||
mom_rank = mask_scores(momentum, eligible).rank(axis=1, pct=True, na_option="keep")
|
||||
|
||||
# Composite score (50/50)
|
||||
composite = 0.5 * rec_rank + 0.5 * mom_rank
|
||||
|
||||
# Select top_n
|
||||
rank = composite.rank(axis=1, ascending=False, na_option="bottom")
|
||||
rank = mask_scores(composite, eligible).rank(axis=1, ascending=False, na_option="bottom")
|
||||
composite = mask_scores(composite, eligible)
|
||||
n_valid = composite.notna().sum(axis=1)
|
||||
enough = n_valid >= self.top_n
|
||||
top_mask = (rank <= self.top_n) & enough.values.reshape(-1, 1)
|
||||
|
||||
Reference in New Issue
Block a user