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:
2026-10-01 02:24:01 -07:00
parent 761725d2c8
commit 722cade974
45 changed files with 1561 additions and 1021 deletions

View File

@@ -11,10 +11,10 @@ Improvements over base RecoveryMomentumStrategy:
import numpy as np
import pandas as pd
from strategies.base import Strategy
from strategies.base import StockStrategy, mask_scores
class EnhancedRecoveryMomentumStrategy(Strategy):
class EnhancedRecoveryMomentumStrategy(StockStrategy):
"""
Recovery + momentum with inverse-vol weighting and regime awareness.
"""
@@ -39,34 +39,35 @@ class EnhancedRecoveryMomentumStrategy(Strategy):
self.top_n = top_n
self.regime_scale = regime_scale
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_periods=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)
# Factor 3: Intermediate momentum (6m) as a filter gate
# Only consider stocks with positive intermediate trend
intermediate = data.shift(self.mom_skip).pct_change(self.intermediate_mom - self.mom_skip)
intermediate = data.shift(self.mom_skip).pct_change(self.intermediate_mom - self.mom_skip, fill_method=None)
trend_gate = intermediate > 0
# Cross-sectional percentile ranks
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), gated by intermediate trend
composite = 0.5 * rec_rank + 0.5 * mom_rank
composite = composite.where(trend_gate, np.nan)
# 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)
# Inverse-vol weighting among selected stocks
returns = data.pct_change()
returns = data.pct_change(fill_method=None)
vol = returns.rolling(self.vol_window, min_periods=30).std().replace(0, np.nan)
inv_vol = (1.0 / vol).where(top_mask, 0.0)
@@ -75,7 +76,7 @@ class EnhancedRecoveryMomentumStrategy(Strategy):
# Regime scaling: reduce exposure when market vol is high
if self.regime_scale:
market_vol = vol.mean(axis=1)
market_vol = mask_scores(vol, eligible).mean(axis=1)
vol_median = market_vol.rolling(252, min_periods=126).median()
vol_ratio = (vol_median / market_vol).clip(0.5, 1.2)
signals = signals.mul(vol_ratio, axis=0)