fix: rank-based allocator + momentum signal (reversal->momentum flip)

This commit is contained in:
Yuxuan Yan
2026-06-07 09:50:08 +08:00
parent da01312292
commit bd0605072f
7 changed files with 117 additions and 44 deletions
+7 -11
View File
@@ -1,10 +1,6 @@
#!/usr/bin/env python3
"""End-to-end pipeline: HS300 universe -> reversal signal -> cross-sectional IC
-> multi-stock backtest (AlphaStrategy + PercentSizer) -> reports.
Note: this runs the first 30 HS300 constituents to keep runtime manageable.
Downloading daily bars for the full ~300 names takes roughly 10 minutes.
"""
"""End-to-end pipeline: HS300 universe -> momentum signal -> cross-sectional IC
-> multi-stock backtest (AlphaStrategy + RankEqualWeightBuilder) -> reports."""
import logging
import pandas as pd
@@ -15,8 +11,8 @@ from backtest.runner import BacktestRunner
from data.downloader import download_batch
from data.universe import SYMBOLS
from eval.metrics import evaluate_cross_sectional
from portfolio.builder import ThresholdBuilder
from signals.reversal import ReversalSignal
from portfolio.builder import RankEqualWeightBuilder
from signals.momentum import MomentumSignal
from strategies.alpha_strategy import AlphaStrategy
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
@@ -33,8 +29,8 @@ def main():
data = {s: df for s, df in data.items() if df is not None and not df.empty}
logger.info(f"Downloaded {len(data)}/{len(symbols)} symbols")
# 3. Compute the reversal signal per stock.
signal = ReversalSignal(lookback=5)
# 3. Compute the momentum signal per stock.
signal = MomentumSignal(lookback=5)
signal_series: dict[str, pd.Series] = {}
forward_returns: dict[str, pd.Series] = {}
for sym, df in data.items():
@@ -60,7 +56,7 @@ def main():
sizer_percent=0.95,
)
runner = BacktestRunner(config)
builder = ThresholdBuilder(buy_threshold=0.02, sell_threshold=-0.02, size_pct=0.95)
builder = RankEqualWeightBuilder(top_n=5)
for sym, df in data.items():
df = df.copy()
df["signal"] = signal.compute(df).values