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

@@ -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 MeanReversionStrategy(Strategy):
class MeanReversionStrategy(StockStrategy):
"""
Monthly "buy the dip" with momentum confirmation.
@@ -20,17 +20,18 @@ class MeanReversionStrategy(Strategy):
self.rebal_freq = rebal_freq
self.top_n = top_n
def generate_signals(self, data: pd.DataFrame) -> pd.DataFrame:
long_mom = data.pct_change(self.mom_lookback)
def generate_signals(self, data: pd.DataFrame, eligible: pd.DataFrame | None = None) -> pd.DataFrame:
long_mom = data.pct_change(self.mom_lookback, fill_method=None)
has_momentum = long_mom > 0
short_ret = data.pct_change(self.dip_window)
short_ret = data.pct_change(self.dip_window, fill_method=None)
# Among positive-momentum stocks, rank by biggest dip
scores = short_ret.where(has_momentum, np.nan)
# Rank: most negative = rank 1 (biggest dip)
rank = scores.rank(axis=1, ascending=True, na_option="bottom")
rank = mask_scores(scores, eligible).rank(axis=1, ascending=True, na_option="bottom")
scores = mask_scores(scores, eligible)
n_valid = scores.notna().sum(axis=1)
effective_n = n_valid.clip(upper=self.top_n)