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:
@@ -27,7 +27,7 @@ import numpy as np
|
||||
import pandas as pd
|
||||
from numpy.lib.stride_tricks import sliding_window_view
|
||||
|
||||
from strategies.base import BarStrategy
|
||||
from strategies.base import BarStrategy, mask_scores
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -60,10 +60,10 @@ def sharpe_momentum(close: pd.DataFrame) -> pd.DataFrame:
|
||||
return momentum_12_1(close) / _returns(close).rolling(252, min_periods=180).std()
|
||||
|
||||
|
||||
def low_beta(close: pd.DataFrame, window: int = 252) -> pd.DataFrame:
|
||||
def low_beta(close: pd.DataFrame, window: int = 252, eligible=None) -> pd.DataFrame:
|
||||
"""Negative rolling beta to the equal-weight average of the panel."""
|
||||
r = _returns(close)
|
||||
m = r.mean(axis=1).fillna(0.0)
|
||||
m = mask_scores(r, eligible).mean(axis=1).fillna(0.0)
|
||||
minp = window * 180 // 252
|
||||
e_xy = r.mul(m, axis=0).rolling(window, min_periods=minp).mean()
|
||||
e_x = r.rolling(window, min_periods=minp).mean()
|
||||
@@ -73,12 +73,12 @@ def low_beta(close: pd.DataFrame, window: int = 252) -> pd.DataFrame:
|
||||
|
||||
|
||||
def idiosyncratic_vol(close: pd.DataFrame, beta_window: int = 252,
|
||||
vol_window: int = 63) -> pd.DataFrame:
|
||||
vol_window: int = 63, eligible=None) -> pd.DataFrame:
|
||||
"""Negative 63-day volatility of returns net of the equal-weight market
|
||||
times a trailing (lagged) 252-day beta; Ang, Hodrick, Xing & Zhang (2006)."""
|
||||
r = _returns(close)
|
||||
m = r.mean(axis=1).fillna(0.0)
|
||||
beta = -low_beta(close, beta_window).shift(1)
|
||||
m = mask_scores(r, eligible).mean(axis=1).fillna(0.0)
|
||||
beta = -low_beta(close, beta_window, eligible).shift(1)
|
||||
resid = r - beta.mul(m, axis=0)
|
||||
return -resid.rolling(vol_window, min_periods=40).std()
|
||||
|
||||
@@ -111,28 +111,29 @@ def _rank(df: pd.DataFrame) -> pd.DataFrame:
|
||||
return df.rank(axis=1, pct=True)
|
||||
|
||||
|
||||
def _composite(*factors: pd.DataFrame) -> pd.DataFrame:
|
||||
return sum(_rank(f) for f in factors) / len(factors)
|
||||
def _composite(*factors: pd.DataFrame, eligible=None) -> pd.DataFrame:
|
||||
return sum(_rank(mask_scores(f, eligible)) for f in factors) / len(factors)
|
||||
|
||||
|
||||
def signal_overnight_recovery(bars):
|
||||
return _composite(overnight_momentum(bars["open"], bars["close"]), recovery(bars["close"]))
|
||||
return _composite(overnight_momentum(bars["open"], bars["close"]), recovery(bars["close"]), eligible=bars.get("eligible"))
|
||||
|
||||
|
||||
def signal_lowrisk_momentum(bars):
|
||||
c = bars["close"]
|
||||
return _composite(amplitude_cut_momentum(bars["high"], bars["low"], c), low_beta(c),
|
||||
price_volume_corr(c, bars["volume"]), sharpe_momentum(c))
|
||||
return _composite(amplitude_cut_momentum(bars["high"], bars["low"], c), low_beta(c, eligible=bars.get("eligible")),
|
||||
price_volume_corr(c, bars["volume"]), sharpe_momentum(c), eligible=bars.get("eligible"))
|
||||
|
||||
|
||||
def signal_lowbeta_sharpe_mom(bars):
|
||||
c = bars["close"]
|
||||
return _composite(low_beta(c), sharpe_momentum(c))
|
||||
return _composite(low_beta(c, eligible=bars.get("eligible")), sharpe_momentum(c), eligible=bars.get("eligible"))
|
||||
|
||||
|
||||
def signal_low_risk(bars):
|
||||
c = bars["close"]
|
||||
return _composite(idiosyncratic_vol(c), low_beta(c))
|
||||
return _composite(idiosyncratic_vol(c, eligible=bars.get("eligible")),
|
||||
low_beta(c, eligible=bars.get("eligible")), eligible=bars.get("eligible"))
|
||||
|
||||
|
||||
# name -> (bar fields, score function)
|
||||
@@ -202,6 +203,8 @@ class RobustMomentumStrategy(BarStrategy):
|
||||
def generate_weights(self, bars: dict[str, pd.DataFrame]) -> pd.DataFrame:
|
||||
close = bars["close"]
|
||||
panel = {f: bars[f].reindex(index=close.index, columns=close.columns) for f in self.fields}
|
||||
if "eligible" in bars:
|
||||
panel["eligible"] = bars["eligible"]
|
||||
score = self._score(panel)
|
||||
# names without a price are not eligible; a backtest also passes
|
||||
# "eligible" (point-in-time index membership) so signals can use a
|
||||
|
||||
Reference in New Issue
Block a user