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 MomentumStrategy(Strategy):
|
||||
class MomentumStrategy(StockStrategy):
|
||||
"""
|
||||
Classic cross-sectional momentum strategy (Jegadeesh & Titman, 1993).
|
||||
|
||||
@@ -19,14 +19,16 @@ class MomentumStrategy(Strategy):
|
||||
self.skip = skip # ~1 month — skip to avoid reversal
|
||||
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: return from T-(lookback) to T-skip
|
||||
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 = momentum.where(data.notna())
|
||||
momentum = mask_scores(momentum, eligible)
|
||||
n_valid = momentum.notna().sum(axis=1)
|
||||
enough_data = 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_data.values.reshape(-1, 1)
|
||||
|
||||
raw = top_mask.astype(float)
|
||||
|
||||
Reference in New Issue
Block a user