Add 12 strategy modules including adaptive blend, composite alpha, cross-asset momentum, ensemble alpha, trend rider v5/v6, and more.
100 lines
3.8 KiB
Python
100 lines
3.8 KiB
Python
"""
|
|
Enhanced Recovery Momentum Strategy.
|
|
|
|
Improvements over base RecoveryMomentumStrategy:
|
|
1. Inverse-volatility weighting (allocate more to lower-vol winners → better risk-adjusted)
|
|
2. Monthly rebalancing (controls turnover)
|
|
3. Momentum filter gate: only pick stocks with positive 6-month momentum
|
|
(avoids "dead cat bounces" — recovery without underlying trend)
|
|
4. Volatility regime scaling: reduce exposure when market vol is elevated
|
|
"""
|
|
|
|
import numpy as np
|
|
import pandas as pd
|
|
from strategies.base import Strategy
|
|
|
|
|
|
class EnhancedRecoveryMomentumStrategy(Strategy):
|
|
"""
|
|
Recovery + momentum with inverse-vol weighting and regime awareness.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
recovery_window: int = 63,
|
|
mom_lookback: int = 252,
|
|
mom_skip: int = 21,
|
|
intermediate_mom: int = 126,
|
|
vol_window: int = 60,
|
|
rebal_freq: int = 21,
|
|
top_n: int = 20,
|
|
regime_scale: bool = True,
|
|
):
|
|
self.recovery_window = recovery_window
|
|
self.mom_lookback = mom_lookback
|
|
self.mom_skip = mom_skip
|
|
self.intermediate_mom = intermediate_mom
|
|
self.vol_window = vol_window
|
|
self.rebal_freq = rebal_freq
|
|
self.top_n = top_n
|
|
self.regime_scale = regime_scale
|
|
|
|
def generate_signals(self, data: pd.DataFrame) -> pd.DataFrame:
|
|
# Factor 1: Recovery — price / rolling min
|
|
recovery = data / data.rolling(self.recovery_window, min_periods=self.recovery_window).min() - 1
|
|
|
|
# Factor 2: 12-1 month momentum
|
|
momentum = data.shift(self.mom_skip).pct_change(self.mom_lookback - self.mom_skip)
|
|
|
|
# Factor 3: Intermediate momentum (6m) as a filter gate
|
|
# Only consider stocks with positive intermediate trend
|
|
intermediate = data.shift(self.mom_skip).pct_change(self.intermediate_mom - self.mom_skip)
|
|
trend_gate = intermediate > 0
|
|
|
|
# Cross-sectional percentile ranks
|
|
rec_rank = recovery.rank(axis=1, pct=True, na_option="keep")
|
|
mom_rank = momentum.rank(axis=1, pct=True, na_option="keep")
|
|
|
|
# Composite score (50/50), gated by intermediate trend
|
|
composite = 0.5 * rec_rank + 0.5 * mom_rank
|
|
composite = composite.where(trend_gate, np.nan)
|
|
|
|
# Select top_n
|
|
rank = composite.rank(axis=1, ascending=False, na_option="bottom")
|
|
n_valid = composite.notna().sum(axis=1)
|
|
enough = n_valid >= self.top_n
|
|
top_mask = (rank <= self.top_n) & enough.values.reshape(-1, 1)
|
|
|
|
# Inverse-vol weighting among selected stocks
|
|
returns = data.pct_change()
|
|
vol = returns.rolling(self.vol_window, min_periods=30).std().replace(0, np.nan)
|
|
inv_vol = (1.0 / vol).where(top_mask, 0.0)
|
|
|
|
row_sums = inv_vol.sum(axis=1).replace(0, np.nan)
|
|
signals = inv_vol.div(row_sums, axis=0).fillna(0.0)
|
|
|
|
# Regime scaling: reduce exposure when market vol is high
|
|
if self.regime_scale:
|
|
market_vol = vol.mean(axis=1)
|
|
vol_median = market_vol.rolling(252, min_periods=126).median()
|
|
vol_ratio = (vol_median / market_vol).clip(0.5, 1.2)
|
|
signals = signals.mul(vol_ratio, axis=0)
|
|
# Re-normalize: cap at 1.0
|
|
row_totals = signals.sum(axis=1)
|
|
overflow = row_totals > 1.0
|
|
signals.loc[overflow] = signals.loc[overflow].div(
|
|
row_totals[overflow], axis=0
|
|
)
|
|
|
|
# Monthly rebalance: keep only rebal-day signals, forward-fill
|
|
warmup = max(self.mom_lookback, self.recovery_window, self.vol_window + 252)
|
|
rebal_mask = pd.Series(False, index=data.index)
|
|
rebal_indices = list(range(warmup, len(data), self.rebal_freq))
|
|
rebal_mask.iloc[rebal_indices] = True
|
|
|
|
signals[~rebal_mask] = np.nan
|
|
signals = signals.ffill().fillna(0.0)
|
|
signals.iloc[:warmup] = 0.0
|
|
|
|
return signals.shift(1).fillna(0.0)
|