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 DualMomentumStrategy(Strategy):
|
||||
class DualMomentumStrategy(StockStrategy):
|
||||
"""
|
||||
Dual momentum with multi-timeframe confirmation.
|
||||
|
||||
@@ -19,29 +19,31 @@ class DualMomentumStrategy(Strategy):
|
||||
def __init__(self, top_n: int = 20):
|
||||
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:
|
||||
# Three timeframe returns (each skipping most recent 5 days)
|
||||
skip = 5
|
||||
short_mom = data.shift(skip).pct_change(63 - skip) # ~3 month
|
||||
med_mom = data.shift(skip).pct_change(126 - skip) # ~6 month
|
||||
long_mom = data.shift(skip).pct_change(252 - skip) # ~12 month
|
||||
short_mom = data.shift(skip).pct_change(63 - skip, fill_method=None) # ~3 month
|
||||
med_mom = data.shift(skip).pct_change(126 - skip, fill_method=None) # ~6 month
|
||||
long_mom = data.shift(skip).pct_change(252 - skip, fill_method=None) # ~12 month
|
||||
|
||||
# All three must be positive (absolute momentum across timeframes)
|
||||
all_positive = (short_mom > 0) & (med_mom > 0) & (long_mom > 0)
|
||||
|
||||
# Composite score: average percentile rank across timeframes
|
||||
short_rank = short_mom.rank(axis=1, pct=True, na_option="keep")
|
||||
med_rank = med_mom.rank(axis=1, pct=True, na_option="keep")
|
||||
long_rank = long_mom.rank(axis=1, pct=True, na_option="keep")
|
||||
short_rank = mask_scores(short_mom, eligible).rank(axis=1, pct=True, na_option="keep")
|
||||
med_rank = mask_scores(med_mom, eligible).rank(axis=1, pct=True, na_option="keep")
|
||||
long_rank = mask_scores(long_mom, eligible).rank(axis=1, pct=True, na_option="keep")
|
||||
composite = (short_rank + med_rank + long_rank) / 3
|
||||
|
||||
# Only consider stocks passing absolute filter
|
||||
composite_filtered = composite.where(all_positive, np.nan)
|
||||
|
||||
composite_filtered = composite_filtered.where(data.notna())
|
||||
composite_filtered = mask_scores(composite_filtered, eligible)
|
||||
n_valid = composite_filtered.notna().sum(axis=1)
|
||||
enough = n_valid >= 1
|
||||
|
||||
rank = composite_filtered.rank(axis=1, ascending=False, na_option="bottom")
|
||||
rank = mask_scores(composite_filtered, eligible).rank(axis=1, ascending=False, na_option="bottom")
|
||||
effective_n = n_valid.clip(upper=self.top_n)
|
||||
top_mask = (rank <= effective_n.values.reshape(-1, 1)) & enough.values.reshape(-1, 1)
|
||||
top_mask = top_mask & all_positive
|
||||
|
||||
Reference in New Issue
Block a user