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:
@@ -18,7 +18,7 @@ Each can run at daily/weekly/biweekly/monthly rebalancing frequency.
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from strategies.base import Strategy
|
||||
from strategies.base import StockStrategy, mask_scores
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -26,11 +26,11 @@ from strategies.base import Strategy
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _mom_12_1(p):
|
||||
return p.shift(21).pct_change(231)
|
||||
return p.shift(21).pct_change(231, fill_method=None)
|
||||
|
||||
|
||||
def _mom_intermediate(p):
|
||||
return p.shift(21).pct_change(147)
|
||||
return p.shift(21).pct_change(147, fill_method=None)
|
||||
|
||||
|
||||
def _rec_63(p):
|
||||
@@ -46,29 +46,29 @@ def _above_ma200(p):
|
||||
|
||||
|
||||
def _up_volume_proxy(p):
|
||||
ret = p.pct_change()
|
||||
ret = p.pct_change(fill_method=None)
|
||||
return ret.where(ret > 0, 0).rolling(20, min_periods=15).sum()
|
||||
|
||||
|
||||
def _gap_up_freq(p):
|
||||
ret = p.pct_change()
|
||||
ret = p.pct_change(fill_method=None)
|
||||
return (ret > 0.01).astype(float).rolling(60, min_periods=40).mean()
|
||||
|
||||
|
||||
def _consistent_returns(p):
|
||||
ret = p.pct_change()
|
||||
ret = p.pct_change(fill_method=None)
|
||||
return (ret > 0).astype(float).rolling(252, min_periods=126).mean()
|
||||
|
||||
|
||||
def _rec_mom_filtered(p):
|
||||
rec = p / p.rolling(126, min_periods=126).min() - 1
|
||||
mom = p.shift(21).pct_change(105)
|
||||
mom = p.shift(21).pct_change(105, fill_method=None)
|
||||
return rec.where(mom > 0, np.nan)
|
||||
|
||||
|
||||
def _up_capture(p):
|
||||
ret = p.pct_change()
|
||||
mkt = ret.mean(axis=1)
|
||||
def _up_capture(p, eligible=None):
|
||||
ret = p.pct_change(fill_method=None)
|
||||
mkt = mask_scores(ret, eligible).mean(axis=1)
|
||||
up_mkt = mkt > 0
|
||||
arr = ret.values.copy()
|
||||
arr[~up_mkt.values, :] = np.nan
|
||||
@@ -79,9 +79,9 @@ def _up_capture(p):
|
||||
return stock_avg.div(mkt_avg, axis=0)
|
||||
|
||||
|
||||
def _down_resilience(p):
|
||||
ret = p.pct_change()
|
||||
mkt = ret.mean(axis=1)
|
||||
def _down_resilience(p, eligible=None):
|
||||
ret = p.pct_change(fill_method=None)
|
||||
mkt = mask_scores(ret, eligible).mean(axis=1)
|
||||
down_mkt = mkt < 0
|
||||
arr = ret.values.copy()
|
||||
arr[~down_mkt.values, :] = np.nan
|
||||
@@ -89,16 +89,16 @@ def _down_resilience(p):
|
||||
return -down_ret.rolling(120, min_periods=30).mean()
|
||||
|
||||
|
||||
def _quality_mom(p):
|
||||
mom_r = _mom_12_1(p).rank(axis=1, pct=True, na_option="keep")
|
||||
con_r = _consistent_returns(p).rank(axis=1, pct=True, na_option="keep")
|
||||
up_r = _up_volume_proxy(p).rank(axis=1, pct=True, na_option="keep")
|
||||
def _quality_mom(p, eligible=None):
|
||||
mom_r = _rank(_mom_12_1(p), eligible)
|
||||
con_r = _rank(_consistent_returns(p), eligible)
|
||||
up_r = _rank(_up_volume_proxy(p), eligible)
|
||||
return 0.4 * mom_r + 0.3 * con_r + 0.3 * up_r
|
||||
|
||||
|
||||
def _mom_x_gap(p):
|
||||
mom_r = _mom_12_1(p).rank(axis=1, pct=True, na_option="keep")
|
||||
gap_r = _gap_up_freq(p).rank(axis=1, pct=True, na_option="keep")
|
||||
def _mom_x_gap(p, eligible=None):
|
||||
mom_r = _rank(_mom_12_1(p), eligible)
|
||||
gap_r = _rank(_gap_up_freq(p), eligible)
|
||||
return mom_r * gap_r
|
||||
|
||||
|
||||
@@ -106,47 +106,47 @@ def _mom_x_gap(p):
|
||||
# Combo signal constructors (weighted rank sums)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _rank(df):
|
||||
return df.rank(axis=1, pct=True, na_option="keep")
|
||||
def _rank(df, eligible=None):
|
||||
return mask_scores(df, eligible).rank(axis=1, pct=True, na_option="keep")
|
||||
|
||||
|
||||
# US combos
|
||||
def signal_rec_mfilt_deep_upvol(p):
|
||||
rec_mfilt_r = _rank(_rec_mom_filtered(p))
|
||||
deep_upvol_r = _rank(_rec_126(p)) * _rank(_up_volume_proxy(p))
|
||||
deep_upvol_rr = _rank(deep_upvol_r)
|
||||
def signal_rec_mfilt_deep_upvol(p, eligible=None):
|
||||
rec_mfilt_r = _rank(_rec_mom_filtered(p), eligible)
|
||||
deep_upvol_r = _rank(_rec_126(p), eligible) * _rank(_up_volume_proxy(p), eligible)
|
||||
deep_upvol_rr = _rank(deep_upvol_r, eligible)
|
||||
return 0.5 * rec_mfilt_r + 0.5 * deep_upvol_rr
|
||||
|
||||
|
||||
def signal_ma200_mom7m_rec126(p):
|
||||
return (0.33 * _rank(_above_ma200(p))
|
||||
+ 0.33 * _rank(_mom_intermediate(p))
|
||||
+ 0.34 * _rank(_rec_126(p)))
|
||||
def signal_ma200_mom7m_rec126(p, eligible=None):
|
||||
return (0.33 * _rank(_above_ma200(p), eligible)
|
||||
+ 0.33 * _rank(_mom_intermediate(p), eligible)
|
||||
+ 0.34 * _rank(_rec_126(p), eligible))
|
||||
|
||||
|
||||
def signal_rec_mfilt_ma200(p):
|
||||
return 0.5 * _rank(_rec_mom_filtered(p)) + 0.5 * _rank(_above_ma200(p))
|
||||
def signal_rec_mfilt_ma200(p, eligible=None):
|
||||
return 0.5 * _rank(_rec_mom_filtered(p), eligible) + 0.5 * _rank(_above_ma200(p), eligible)
|
||||
|
||||
|
||||
def signal_mom7m_rec126(p):
|
||||
return 0.5 * _rank(_mom_intermediate(p)) + 0.5 * _rank(_rec_126(p))
|
||||
def signal_mom7m_rec126(p, eligible=None):
|
||||
return 0.5 * _rank(_mom_intermediate(p), eligible) + 0.5 * _rank(_rec_126(p), eligible)
|
||||
|
||||
|
||||
# CN combos
|
||||
def signal_up_cap_quality_mom(p):
|
||||
return 0.5 * _rank(_up_capture(p)) + 0.5 * _rank(_quality_mom(p))
|
||||
def signal_up_cap_quality_mom(p, eligible=None):
|
||||
return 0.5 * _rank(_up_capture(p, eligible), eligible) + 0.5 * _rank(_quality_mom(p, eligible), eligible)
|
||||
|
||||
|
||||
def signal_down_resil_qual_mom(p):
|
||||
return 0.5 * _rank(_down_resilience(p)) + 0.5 * _rank(_quality_mom(p))
|
||||
def signal_down_resil_qual_mom(p, eligible=None):
|
||||
return 0.5 * _rank(_down_resilience(p, eligible), eligible) + 0.5 * _rank(_quality_mom(p, eligible), eligible)
|
||||
|
||||
|
||||
def signal_rec63_mom_gap(p):
|
||||
return 0.5 * _rank(_rec_63(p)) + 0.5 * _rank(_mom_x_gap(p))
|
||||
def signal_rec63_mom_gap(p, eligible=None):
|
||||
return 0.5 * _rank(_rec_63(p), eligible) + 0.5 * _rank(_mom_x_gap(p, eligible), eligible)
|
||||
|
||||
|
||||
def signal_up_cap_mom_gap(p):
|
||||
return 0.5 * _rank(_up_capture(p)) + 0.5 * _rank(_mom_x_gap(p))
|
||||
def signal_up_cap_mom_gap(p, eligible=None):
|
||||
return 0.5 * _rank(_up_capture(p, eligible), eligible) + 0.5 * _rank(_mom_x_gap(p, eligible), eligible)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -171,7 +171,7 @@ SIGNAL_REGISTRY = {
|
||||
# Strategy class
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class FactorComboStrategy(Strategy):
|
||||
class FactorComboStrategy(StockStrategy):
|
||||
"""
|
||||
Generic factor-combination strategy with configurable rebalancing frequency.
|
||||
|
||||
@@ -192,11 +192,12 @@ class FactorComboStrategy(Strategy):
|
||||
self.rebal_freq = rebal_freq
|
||||
self.top_n = top_n
|
||||
|
||||
def generate_signals(self, data: pd.DataFrame) -> pd.DataFrame:
|
||||
sig = self.signal_func(data)
|
||||
def generate_signals(self, data: pd.DataFrame, eligible: pd.DataFrame | None = None) -> pd.DataFrame:
|
||||
sig = self.signal_func(data, eligible=eligible).where(data.notna())
|
||||
|
||||
# Select top_n by signal rank
|
||||
rank = sig.rank(axis=1, ascending=False, na_option="bottom")
|
||||
rank = mask_scores(sig, eligible).rank(axis=1, ascending=False, na_option="bottom")
|
||||
sig = mask_scores(sig, eligible)
|
||||
n_valid = sig.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