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 AdaptiveMomentumStrategy(Strategy):
|
||||
class AdaptiveMomentumStrategy(StockStrategy):
|
||||
"""
|
||||
Momentum with dynamic position sizing via inverse-volatility weighting.
|
||||
|
||||
@@ -25,18 +25,19 @@ class AdaptiveMomentumStrategy(Strategy):
|
||||
self.vol_window = vol_window
|
||||
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:
|
||||
# Momentum ranking
|
||||
momentum = data.shift(self.skip).pct_change(self.lookback - self.skip)
|
||||
momentum = data.shift(self.skip).pct_change(self.lookback - self.skip, fill_method=None)
|
||||
|
||||
momentum = mask_scores(momentum, eligible)
|
||||
n_valid = momentum.notna().sum(axis=1)
|
||||
enough = n_valid >= self.top_n
|
||||
|
||||
score_rank = momentum.rank(axis=1, ascending=False, na_option="bottom")
|
||||
score_rank = mask_scores(momentum, eligible).rank(axis=1, ascending=False, na_option="bottom")
|
||||
top_mask = (score_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).std().replace(0, np.nan)
|
||||
inv_vol = (1.0 / vol).where(top_mask, 0.0)
|
||||
|
||||
@@ -44,7 +45,7 @@ class AdaptiveMomentumStrategy(Strategy):
|
||||
signals = inv_vol.div(row_sums, axis=0).fillna(0.0)
|
||||
|
||||
# Regime scaling: compare current market vol to its 1-year median
|
||||
market_vol = vol.mean(axis=1)
|
||||
market_vol = mask_scores(vol, eligible).mean(axis=1)
|
||||
vol_median = market_vol.rolling(252).median()
|
||||
vol_scale = (vol_median / market_vol).clip(0.3, 1.5)
|
||||
signals = signals.mul(vol_scale, axis=0)
|
||||
|
||||
Reference in New Issue
Block a user