chore: remove pre-lab research and dead strategies; index them in research/ARCHIVE.md

Removed (79 files, all in history; research/ARCHIVE.md lists each with its
purpose and the commit to restore it from):
- root factor_*.py mining scripts (2026-04) and yearly_sweep.py
- scripts/run_daily.{py,sh} (superseded by the monitor service; no crontab)
- strategies no registry, test or live book uses: trend_rider_v8,
  adaptive_blend, cross_asset_momentum, long_hedged, ls_momentum
- 55 research/*.py scripts from 2026-04/05 written before research/lab,
  the us-alpha design spec, and the 7 tests that only covered them

Kept research/permanent_yearly.py: bridge.py and precompute_backtest.py
import equity_with_cashflows. CLAUDE.md gains the archive note and the
command lines of the studies committed in the previous commit.

Checks: 99 unit tests OK (125 before, minus the 26 removed with their
modules), all 157 remaining modules import, 94 registry strategies build,
bridge.py list OK, web 23 tests OK and build OK.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-09 22:20:59 -07:00
parent 04e2c5c3be
commit a9683b6d1f
81 changed files with 121 additions and 19668 deletions

View File

@@ -1,190 +0,0 @@
"""
PIT-Compliant Adaptive Multi-Strategy Blend — Sharpe 1.5+ target.
Combines three uncorrelated alpha sources with adaptive (trailing-Sharpe)
weighting and a vol-target overlay:
Leg 1: TrendRiderV5 (ETF regime/leverage timing) — inherently PIT
Leg 2: PIT Stock Momentum (cross-sectional 12-1 mom, masked universe)
Leg 3: Cross-Asset Time-Series Momentum (6m, top 2, inv-vol) — inherently PIT
Blending: weights proportional to trailing 126d rolling Sharpe (clipped ≥ 0).
Vol-target: scale combined exposure so realized 20d vol ≈ 22%.
Backtest (2017-06 to 2026-05, PIT-compliant):
Full: Sharpe 1.69, CAGR 33.6%, MaxDD -20.6%, Calmar 1.63
IS: Sharpe 1.36 (2017-2022)
OOS: Sharpe 2.21 (2023-2026)
Bootstrap P(Sharpe > 1.5) = 72%
All components are PIT-safe:
- V5: ETF-only (SPY/UPRO/TQQQ/GLD/DBC)
- Stock: uses universe_history.mask_prices() for S&P 500 membership
- XA: ETF-only (SPY/GLD/TLT/IEF/DBC)
- Adaptive weights: trailing 126d (no future info)
- Vol-target: 1-day lagged realized vol
"""
from __future__ import annotations
import numpy as np
import pandas as pd
from strategies.cross_asset_momentum import CrossAssetMomentum
from strategies.trend_rider_v5 import TrendRiderV5
class AdaptiveBlend:
"""
Adaptive multi-strategy blend with vol-target overlay.
Usage:
blend = AdaptiveBlend()
daily_returns = blend.run(etf_panel, pit_masked_prices)
"""
def __init__(
self,
# V5 params
v5_promote_thresholds=(0.35, 0.55),
v5_demote_thresholds=(0.25, 0.45),
# Stock params
stock_top_n: int = 12,
stock_rebal_freq: int = 42,
stock_mom_blend: float = 1.0,
stock_asym_vol: bool = True,
stock_asym_vol_floor: float = 0.50,
# XA params
xa_lookback: int = 126,
xa_top_k: int = 2,
xa_rebal_freq: int = 21,
# Blend params
adaptive_lookback: int = 126,
vol_target: float = 0.22,
vol_window: int = 20,
# Backtest range
start: str = "2017-06-01",
):
self.v5 = TrendRiderV5(
promote_thresholds=v5_promote_thresholds,
demote_thresholds=v5_demote_thresholds,
)
self.stock_params = dict(
top_n=stock_top_n, rebal_freq=stock_rebal_freq,
mom_blend=stock_mom_blend, asym_vol=stock_asym_vol,
asym_vol_floor=stock_asym_vol_floor, dd_dampen=False,
)
self.xa = CrossAssetMomentum(
lookback=xa_lookback, top_k=xa_top_k,
rebal_freq=xa_rebal_freq, vol_scale=True,
)
self.adaptive_lookback = adaptive_lookback
self.vol_target = vol_target
self.vol_window = vol_window
self.start = start
def run(
self,
etf_panel: pd.DataFrame,
pit_masked: pd.DataFrame,
transaction_cost: float = 0.001,
) -> pd.Series:
"""
Execute the full blend and return daily returns.
Parameters
----------
etf_panel : price panel including ETFs (SPY, UPRO, TQQQ, GLD, DBC, TLT, IEF, etc.)
pit_masked : S&P 500 prices masked by membership (NaN outside membership)
transaction_cost : one-way transaction cost (default 10bps)
Returns
-------
pd.Series of daily returns (after vol-target scaling)
"""
from research.pit_optimization import PITEnsemble
from research.trend_rider_robustness import portfolio_returns
# --- Leg 1: V5 ---
w_v5 = self.v5.generate_signals(etf_panel)
rets_v5 = portfolio_returns(w_v5, etf_panel, transaction_cost=transaction_cost)
rets_v5 = rets_v5.loc[self.start:]
# --- Leg 2: PIT Stock Momentum ---
strat_stock = PITEnsemble(**self.stock_params)
w_stock = strat_stock.generate_signals(pit_masked)
rets_stock = (w_stock * pit_masked.pct_change(fill_method=None).fillna(0.0)).sum(axis=1)
rets_stock = rets_stock.loc[self.start:]
# --- Leg 3: Cross-Asset Momentum ---
w_xa = self.xa.generate_signals(etf_panel)
rets_xa = portfolio_returns(w_xa, etf_panel, transaction_cost=transaction_cost)
rets_xa = rets_xa.loc[self.start:]
# --- Adaptive blending ---
idx = rets_v5.index.intersection(rets_stock.index).intersection(rets_xa.index)
df = pd.DataFrame({
"v5": rets_v5.loc[idx],
"stock": rets_stock.loc[idx],
"xa": rets_xa.loc[idx],
}).fillna(0.0)
roll_mu = df.rolling(self.adaptive_lookback).mean()
roll_std = df.rolling(self.adaptive_lookback).std()
roll_sharpe = (roll_mu / roll_std * np.sqrt(252)).clip(lower=0)
w_sum = roll_sharpe.sum(axis=1).replace(0, 1)
adaptive_weights = roll_sharpe.div(w_sum, axis=0).fillna(1.0 / 3)
combined_rets = (df * adaptive_weights).sum(axis=1)
# --- Vol-target overlay ---
realized = combined_rets.rolling(self.vol_window).std(ddof=1) * np.sqrt(252)
realized = realized.shift(1).fillna(self.vol_target)
scale = (self.vol_target / realized.replace(0.0, np.nan)).clip(upper=1.0).fillna(1.0)
final_rets = combined_rets * scale
return final_rets
def run_with_diagnostics(
self,
etf_panel: pd.DataFrame,
pit_masked: pd.DataFrame,
transaction_cost: float = 0.001,
) -> dict:
"""Run and return diagnostics (individual rets, weights, combined)."""
from research.pit_optimization import PITEnsemble
from research.trend_rider_robustness import portfolio_returns
w_v5 = self.v5.generate_signals(etf_panel)
rets_v5 = portfolio_returns(w_v5, etf_panel, transaction_cost=transaction_cost).loc[self.start:]
strat_stock = PITEnsemble(**self.stock_params)
w_stock = strat_stock.generate_signals(pit_masked)
rets_stock = (w_stock * pit_masked.pct_change(fill_method=None).fillna(0.0)).sum(axis=1).loc[self.start:]
w_xa = self.xa.generate_signals(etf_panel)
rets_xa = portfolio_returns(w_xa, etf_panel, transaction_cost=transaction_cost).loc[self.start:]
idx = rets_v5.index.intersection(rets_stock.index).intersection(rets_xa.index)
df = pd.DataFrame({"v5": rets_v5.loc[idx], "stock": rets_stock.loc[idx], "xa": rets_xa.loc[idx]}).fillna(0)
roll_mu = df.rolling(self.adaptive_lookback).mean()
roll_std = df.rolling(self.adaptive_lookback).std()
roll_sharpe = (roll_mu / roll_std * np.sqrt(252)).clip(lower=0)
w_sum = roll_sharpe.sum(axis=1).replace(0, 1)
adaptive_weights = roll_sharpe.div(w_sum, axis=0).fillna(1.0 / 3)
combined_rets = (df * adaptive_weights).sum(axis=1)
realized = combined_rets.rolling(self.vol_window).std(ddof=1) * np.sqrt(252)
realized = realized.shift(1).fillna(self.vol_target)
scale = (self.vol_target / realized.replace(0.0, np.nan)).clip(upper=1.0).fillna(1.0)
final_rets = combined_rets * scale
return {
"rets_v5": rets_v5,
"rets_stock": rets_stock,
"rets_xa": rets_xa,
"adaptive_weights": adaptive_weights,
"combined_rets": combined_rets,
"vol_scale": scale,
"final_rets": final_rets,
}

View File

@@ -1,93 +0,0 @@
"""
Cross-asset time-series momentum strategy (ETF-only, inherently PIT).
Alpha source: Moskowitz, Ooi, Pedersen (2012) — assets with positive
12-month returns continue to trend. Earns during equity crises when
bonds/gold trend up while stocks trend down.
Universe: SPY, GLD, TLT, IEF, DBC (broad, liquid ETFs)
Signal: 12-month total return; go long top K assets with positive momentum
Rebalance: monthly (21 trading days)
If no asset has positive 12m return → 100% cash (SHY proxy = 0 weights)
"""
import numpy as np
import pandas as pd
class CrossAssetMomentum:
"""Time-series momentum across major asset classes."""
UNIVERSE = ["SPY", "GLD", "TLT", "IEF", "DBC"]
def __init__(
self,
lookback: int = 252,
top_k: int = 3,
rebal_freq: int = 21,
vol_scale: bool = True,
vol_window: int = 63,
):
self.lookback = lookback
self.top_k = top_k
self.rebal_freq = rebal_freq
self.vol_scale = vol_scale
self.vol_window = vol_window
def generate_signals(self, data: pd.DataFrame) -> pd.DataFrame:
"""
Parameters
----------
data : DataFrame with columns including UNIVERSE ETFs (prices).
Returns
-------
DataFrame of weights aligned to data.index and data.columns.
"""
# Restrict to our universe (ignore missing gracefully)
available = [t for t in self.UNIVERSE if t in data.columns]
prices = data[available]
# 12-month return signal (shifted 1 day for execution lag)
mom = prices.pct_change(self.lookback).shift(1)
# Inverse-vol for position sizing (optional)
if self.vol_scale:
daily_ret = prices.pct_change()
vol = daily_ret.rolling(self.vol_window).std() * np.sqrt(252)
inv_vol = (1.0 / vol).replace([np.inf, -np.inf], np.nan)
else:
inv_vol = None
weights = pd.DataFrame(0.0, index=data.index, columns=data.columns)
n = len(prices.index)
last_w = pd.Series(0.0, index=available)
for i in range(self.lookback + 1, n, self.rebal_freq):
row_mom = mom.iloc[i]
# Only go long assets with positive momentum
positive = row_mom[row_mom > 0].sort_values(ascending=False)
if positive.empty:
last_w = pd.Series(0.0, index=available)
else:
selected = positive.head(self.top_k).index.tolist()
if self.vol_scale and inv_vol is not None:
iv = inv_vol.iloc[i][selected]
if iv.sum() > 0:
w = iv / iv.sum()
else:
w = pd.Series(1.0 / len(selected), index=selected)
else:
w = pd.Series(1.0 / len(selected), index=selected)
last_w = pd.Series(0.0, index=available)
last_w[selected] = w.values
# Hold until next rebalance
end_i = min(i + self.rebal_freq, n)
for col in available:
weights.iloc[i:end_i, weights.columns.get_loc(col)] = last_w[col]
return weights

View File

@@ -1,124 +0,0 @@
"""Market-hedged long-only stock portfolio.
Architecture
------------
Long: top-N stock portfolio (factor-selected, inv-vol weighted).
Short hedge: SPY (or SH ETF) at hedge_ratio × long_gross.
This isolates cross-sectional stock-selection alpha while removing the
broad-market beta. Unlike L/S of individual stocks, the short leg is on
the index — so:
* no meme-stock blowups on short side (GME/AMC type events)
* borrow cost on SPY is ≈ 5-15 bps annualized (very cheap)
* no short-dividend pass-through issue (pay SPY div, but that's offset
by long-side dividends roughly)
Because the long leg is monthly-rebalanced and the short hedge is fixed
at -1.0 × long_gross, total turnover is dominated by the long leg —
similar to V6 long-only.
Output is PIT-safe via terminal `.shift(1)`.
"""
from __future__ import annotations
import numpy as np
import pandas as pd
from strategies.base import Strategy
from strategies.factor_combo import SIGNAL_REGISTRY
class LongHedgedStock(Strategy):
"""Long-only stock momentum hedged with SPY short."""
def __init__(
self,
signal_name: str = "rec_mfilt+deep_upvol",
top_n: int = 15,
rebal_freq: int = 21,
hedge_symbol: str = "SPY",
hedge_ratio: float = 1.0,
long_gross: float = 1.0,
invvol_window: int = 60,
invvol_floor: float = 0.10,
invvol_cap: float = 0.20,
stock_universe: list[str] | None = None,
# Regime gate: zero out positions when regime_signal < its MA(ma_window)
regime_gate: bool = False,
regime_signal: str = "SPY",
ma_window: int = 200,
) -> None:
if signal_name not in SIGNAL_REGISTRY:
raise ValueError(f"Unknown signal: {signal_name}")
self.signal_name = signal_name
self.signal_func = SIGNAL_REGISTRY[signal_name]
self.top_n = top_n
self.rebal_freq = rebal_freq
self.hedge_symbol = hedge_symbol
self.hedge_ratio = hedge_ratio
self.long_gross = long_gross
self.invvol_window = invvol_window
self.invvol_floor = invvol_floor
self.invvol_cap = invvol_cap
self.stock_universe = stock_universe
self.regime_gate = regime_gate
self.regime_signal = regime_signal
self.ma_window = ma_window
def generate_signals(self, data: pd.DataFrame) -> pd.DataFrame:
if self.hedge_symbol not in data.columns:
raise ValueError(f"hedge_symbol {self.hedge_symbol!r} missing from panel")
universe = self.stock_universe or [
c for c in data.columns if c != self.hedge_symbol
]
universe = [c for c in universe if c in data.columns]
stock_panel = data[universe]
sig = self.signal_func(stock_panel)
rank = sig.rank(axis=1, ascending=False, na_option="bottom")
n_valid = sig.notna().sum(axis=1)
enough = n_valid >= self.top_n
top_mask = (rank <= self.top_n) & enough.values.reshape(-1, 1)
# Inv-vol weighting within selection
rets = stock_panel.pct_change(fill_method=None)
vol = rets.rolling(self.invvol_window,
min_periods=self.invvol_window // 2).std() * np.sqrt(252)
vol_clipped = vol.clip(lower=self.invvol_floor, upper=self.invvol_cap)
invvol = (1.0 / vol_clipped).where(top_mask, 0.0)
row_sums = invvol.sum(axis=1).replace(0, np.nan)
long_w = invvol.div(row_sums, axis=0).fillna(0.0) * self.long_gross
# Monthly rebalance
warmup = 252
rebal_mask = pd.Series(False, index=data.index)
rebal_idx = list(range(warmup, len(data), self.rebal_freq))
rebal_mask.iloc[rebal_idx] = True
long_w[~rebal_mask] = np.nan
long_w = long_w.ffill().fillna(0.0)
long_w.iloc[:warmup] = 0.0
# Build full weights frame: longs in stocks, short in SPY
out = pd.DataFrame(0.0, index=data.index, columns=data.columns)
for c in long_w.columns:
if c in out.columns:
out[c] = long_w[c]
# Short hedge: only when long leg is active (gross > 0)
long_gross_now = long_w.abs().sum(axis=1)
active = (long_gross_now > 0)
out[self.hedge_symbol] = -self.hedge_ratio * long_gross_now * active.astype(float)
# Regime gate: zero everything when regime signal is in bear regime.
# Avoids the negative-carry case where long stocks tank with SPY but
# the hedge can't fully offset (since long has higher beta).
if self.regime_gate and self.regime_signal in data.columns:
regime_px = data[self.regime_signal]
ma = regime_px.rolling(self.ma_window).mean()
risk_on = (regime_px > ma).astype(float).fillna(0.0)
out = out.mul(risk_on, axis=0)
return out.shift(1).fillna(0.0)
__all__ = ["LongHedgedStock"]

View File

@@ -1,251 +0,0 @@
"""Industry-neutral long/short momentum on the S&P 500.
Strategy
--------
At each rebalance date (default: monthly):
1. Compute 12-1 month momentum for every stock in the panel.
2. Group stocks by GICS sector.
3. Within each sector, rank by momentum.
4. Long the top `long_pct` (default 20%) of each sector.
5. Short the bottom `short_pct` (default 20%) of each sector.
6. Equal-weight within long-leg and short-leg, scaled so gross long = 1.0
and gross short = 1.0 → 200% gross exposure, ~0 net (β ≈ 0).
The β-neutrality comes from sector-level matching: each sector contributes
both long and short positions in equal $-amounts, so sector and (mostly)
market exposures cancel out.
Output
------
A weights DataFrame with positive (long) and negative (short) entries.
PIT-safe via terminal `.shift(1)`.
Costs
-----
Realistic backtest of L/S requires three additional costs not present in
long-only:
* borrow fee on the short leg (handled by the eval script, not here)
* higher slippage per turnover (this strategy churns more than V5)
* dividend payment on shorts (small for SP500 ~ 1.5% × |short_w|)
The strategy reports raw weights; the eval script applies costs.
"""
from __future__ import annotations
import os
import urllib.request
import io
import json
import numpy as np
import pandas as pd
from strategies.base import Strategy
SECTOR_CACHE = "data/us_sectors.csv"
WIKIPEDIA_SP500_URL = "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
def fetch_sp500_sectors(force: bool = False) -> pd.DataFrame:
"""Return a DataFrame indexed by ticker with GICS sector / sub-industry.
Cached at data/us_sectors.csv. Wikipedia is the canonical source for
current S&P 500 sector membership; for backtest purposes we use today's
sector — sector membership is stable enough year-over-year that this
introduces minimal lookahead bias for an industry-neutral strategy.
"""
if not force and os.path.exists(SECTOR_CACHE):
df = pd.read_csv(SECTOR_CACHE, index_col=0)
if "GICS Sector" in df.columns and len(df) > 100:
return df
print("--- Fetching S&P 500 GICS sectors from Wikipedia ---")
headers = {"User-Agent": "Mozilla/5.0 (quant-backtest)"}
req = urllib.request.Request(WIKIPEDIA_SP500_URL, headers=headers)
with urllib.request.urlopen(req) as resp:
html = resp.read().decode("utf-8")
tables = pd.read_html(io.StringIO(html))
df = tables[0]
df = df.rename(columns={"Symbol": "ticker"})
df["ticker"] = df["ticker"].str.replace(".", "-", regex=False)
df = df.set_index("ticker")
keep = [c for c in df.columns if c in ("GICS Sector", "GICS Sub-Industry",
"Security")]
df = df[keep]
os.makedirs(os.path.dirname(SECTOR_CACHE), exist_ok=True)
df.to_csv(SECTOR_CACHE)
print(f"--- Cached {len(df)} sector mappings to {SECTOR_CACHE} ---")
return df
def _signal_mom_12_1(prices: pd.DataFrame) -> pd.DataFrame:
"""12-1 month cross-sectional momentum (highest = long)."""
return prices.shift(21).pct_change(231)
def _signal_reversal_1m(prices: pd.DataFrame) -> pd.DataFrame:
"""1-month reversal: highest 21-day return → SHORT (so we negate)."""
return -prices.pct_change(21)
def _signal_reversal_5d(prices: pd.DataFrame) -> pd.DataFrame:
"""Short-term 5-day reversal."""
return -prices.pct_change(5)
def _signal_recovery_63(prices: pd.DataFrame) -> pd.DataFrame:
"""Recovery factor: price / 63d low (V-shape continuation, long-only-friendly)."""
return prices / prices.rolling(63, min_periods=63).min() - 1
def _signal_low_vol(prices: pd.DataFrame) -> pd.DataFrame:
"""Low-vol: invert 60-day realized vol so low vol → high signal."""
rets = prices.pct_change(fill_method=None)
vol = rets.rolling(60, min_periods=40).std() * np.sqrt(252)
return -vol
def _signal_quality_mom(prices: pd.DataFrame) -> pd.DataFrame:
"""Composite: 12-1 mom + consistency (% positive days over 252d) + low-vol.
Combines a positive long-side selection (mom × consistency) and avoids the
fragile far-tail of pure momentum by inverse-vol weighting.
"""
mom = prices.shift(21).pct_change(231)
rets = prices.pct_change(fill_method=None)
pos_days = (rets > 0).rolling(252, min_periods=126).mean()
vol = rets.rolling(60, min_periods=40).std() * np.sqrt(252)
mom_r = mom.rank(axis=1, pct=True, na_option="keep")
cons_r = pos_days.rank(axis=1, pct=True, na_option="keep")
inv_vol_r = (-vol).rank(axis=1, pct=True, na_option="keep")
return 0.4 * mom_r + 0.3 * cons_r + 0.3 * inv_vol_r
def _signal_mom_x_lowvol(prices: pd.DataFrame) -> pd.DataFrame:
"""Momentum filtered by low-vol — long winners, short LOW-vol losers.
Reduces meme-stock blowups on the short leg by avoiding high-vol losers.
"""
mom = prices.shift(21).pct_change(231)
rets = prices.pct_change(fill_method=None)
vol = rets.rolling(60, min_periods=40).std() * np.sqrt(252)
mom_r = mom.rank(axis=1, pct=True, na_option="keep")
inv_vol_r = (-vol).rank(axis=1, pct=True, na_option="keep")
return 0.5 * mom_r + 0.5 * inv_vol_r
SIGNAL_REGISTRY = {
"mom_12_1": _signal_mom_12_1,
"reversal_1m": _signal_reversal_1m,
"reversal_5d": _signal_reversal_5d,
"recovery_63": _signal_recovery_63,
"low_vol": _signal_low_vol,
"quality_mom": _signal_quality_mom,
"mom_x_lowvol": _signal_mom_x_lowvol,
}
class IndustryNeutralLSMomentum(Strategy):
"""Industry-neutral long/short portfolio with selectable signal."""
def __init__(
self,
rebal_freq: int = 21,
mom_lookback: int = 252,
mom_skip: int = 21,
long_pct: float = 0.20,
short_pct: float = 0.20,
min_sector_size: int = 5,
sector_map: pd.Series | None = None,
gross_long: float = 1.0,
gross_short: float = 1.0,
signal_name: str = "mom_12_1",
) -> None:
self.rebal_freq = rebal_freq
self.mom_lookback = mom_lookback
self.mom_skip = mom_skip
self.long_pct = long_pct
self.short_pct = short_pct
self.min_sector_size = min_sector_size
self.sector_map = sector_map
self.gross_long = gross_long
self.gross_short = gross_short
if signal_name not in SIGNAL_REGISTRY:
raise ValueError(f"Unknown signal: {signal_name}")
self.signal_name = signal_name
self.signal_func = SIGNAL_REGISTRY[signal_name]
def _resolve_sector_map(self, columns: list[str]) -> pd.Series:
if self.sector_map is not None:
return self.sector_map.reindex(columns)
df = fetch_sp500_sectors()
s = df["GICS Sector"]
return s.reindex(columns)
def generate_signals(self, data: pd.DataFrame) -> pd.DataFrame:
cols = list(data.columns)
sectors = self._resolve_sector_map(cols)
mom = self.signal_func(data)
weights = pd.DataFrame(0.0, index=data.index, columns=cols)
warmup = self.mom_lookback + 5
# Pre-compute which rows are rebal days
rebal_idx = list(range(warmup, len(data), self.rebal_freq))
rebal_set = set(rebal_idx)
# Group columns by sector
sector_to_cols: dict[str, list[str]] = {}
for c in cols:
s = sectors.get(c)
if pd.isna(s):
continue
sector_to_cols.setdefault(s, []).append(c)
for t in rebal_idx:
row_mom = mom.iloc[t]
longs: dict[str, float] = {}
shorts: dict[str, float] = {}
for sector, members in sector_to_cols.items():
ms = row_mom.reindex(members).dropna()
if len(ms) < self.min_sector_size:
continue
n_long = max(1, int(round(len(ms) * self.long_pct)))
n_short = max(1, int(round(len(ms) * self.short_pct)))
ranked = ms.sort_values(ascending=False)
long_picks = ranked.head(n_long).index
short_picks = ranked.tail(n_short).index
for sym in long_picks:
longs[sym] = longs.get(sym, 0.0) + 1.0
for sym in short_picks:
shorts[sym] = shorts.get(sym, 0.0) - 1.0
if not longs or not shorts:
continue
# Equal-weight within long leg and short leg
n_l = sum(longs.values())
n_s = -sum(shorts.values())
for sym in longs:
longs[sym] = self.gross_long * longs[sym] / n_l
for sym in shorts:
shorts[sym] = self.gross_short * shorts[sym] / n_s
for sym, w in longs.items():
weights.iat[t, cols.index(sym)] = w
for sym, w in shorts.items():
weights.iat[t, cols.index(sym)] = w
# Forward-fill between rebal dates
non_rebal_mask = pd.Series(True, index=data.index)
for i in rebal_idx:
non_rebal_mask.iat[i] = False
weights[non_rebal_mask.values] = np.nan
weights = weights.ffill().fillna(0.0)
weights.iloc[:warmup] = 0.0
return weights.shift(1).fillna(0.0)
__all__ = ["IndustryNeutralLSMomentum", "fetch_sp500_sectors"]

View File

@@ -1,215 +0,0 @@
"""TrendRider V8 — V7 + TMF risk-off upgrade.
Extends V7 (V3 regime + vol-target + profit-take) with a smarter risk-off
basket: when TLT is in a bull regime (above its MA200), TMF (3x long-duration
Treasuries) joins the risk-off momentum selection alongside GLD and DBC.
Why this works
--------------
V7 spends ~30-40% of time in risk-off, holding GLD or DBC which average
low single-digit returns. During equity bear markets the Fed typically cuts
rates, driving long bonds higher — TMF (3x TLT) captures this convexity.
2020 crash: TMF ~+60% while equities fell 34%.
The TLT MA200 gate prevents TMF allocation during bond bear markets
(e.g. 2022 rate-hiking cycle where TLT fell 31%).
PIT safety
----------
V3's generate_signals uses prices through day t-1 internally, then applies
a terminal shift(1). So V3's output weight at row i uses data through day
i-2. The TMF swap and TLT gate must match this information set: all
lookups use data through day i-2 (shift(2) for vectorized signals,
iloc[i-2] for point lookups).
Profit-take is applied ONLY to risk-on assets (TQQQ/UPRO). Risk-off
assets (GLD, DBC, TMF) are exempt because:
1. TMF can gain 30%+ during rate-cut cycles — PT would sell at the
worst possible time.
2. Risk-off is already regime-gated; PT on defensive assets is redundant.
"""
from __future__ import annotations
import numpy as np
import pandas as pd
from strategies.base import Strategy
from strategies.permanent import TrendRiderV3
class TrendRiderV8(Strategy):
"""V7 architecture + TMF risk-off with bond-regime gate.
Pipeline:
Layer 1 — V3 regime engine → risk-on / risk-off weights
Layer 1b — TMF swap (PIT-aligned to V3's info set: data through i-2)
Layer 2 — Vol-target overlay
Layer 3 — Profit-take (risk-on assets only; risk-off exempt)
"""
def __init__(
self,
# V3 regime engine
ma_long: int = 150,
signal: str = "SPY",
risk_on: tuple[str, ...] = ("TQQQ", "UPRO"),
risk_off: tuple[str, ...] = ("GLD", "DBC"),
# TMF risk-off
tmf_symbol: str = "TMF",
tlt_symbol: str = "TLT",
tlt_ma_window: int = 200,
tmf_mom_lookback: int = 63,
# Vol-target overlay
target_vol: float = 0.36,
vol_window: int = 60,
min_lev: float = 0.75,
max_lev: float = 1.0,
# Profit-take overlay
pt_threshold: float = 0.30,
pt_band: float = 0.10,
pt_park: str = "SHY",
# V3 passthrough
**v3_kwargs,
) -> None:
self.target_vol = target_vol
self.vol_window = vol_window
self.min_lev = min_lev
self.max_lev = max_lev
self.pt_threshold = pt_threshold
self.pt_band = pt_band
self.pt_park = pt_park
self.risk_on = risk_on
self.risk_off = risk_off
self.tmf_symbol = tmf_symbol
self.tlt_symbol = tlt_symbol
self.tlt_ma_window = tlt_ma_window
self.tmf_mom_lookback = tmf_mom_lookback
self.v3 = TrendRiderV3(
signal=signal, risk_on=risk_on, risk_off=risk_off,
ma_long=ma_long, **v3_kwargs,
)
def generate_signals(self, data: pd.DataFrame) -> pd.DataFrame:
# --- Layer 1: V3 regime weights (already shift(1)'d) ---
# w[i] uses data through day i-2.
w = self.v3.generate_signals(data)
# --- Layer 1b: TMF risk-off swap ---
# PIT: must use data through day i-2 to match V3's info set.
# shift(2) on vectorized signals; iloc[i-2] for point lookups.
tmf = self.tmf_symbol
tlt = self.tlt_symbol
if tmf in data.columns and tlt in data.columns:
tlt_ma = data[tlt].rolling(self.tlt_ma_window).mean()
tlt_bull = (data[tlt] > tlt_ma).shift(2).fillna(False)
roff_cols = [c for c in self.risk_off if c in w.columns]
if tmf not in w.columns:
w[tmf] = 0.0
lb = self.tmf_mom_lookback
for i in range(lb + 3, len(w)):
roff_weight = sum(float(w.iat[i, w.columns.get_loc(c)]) for c in roff_cols)
if roff_weight < 1e-8:
continue
if not tlt_bull.iloc[i]:
continue
best_sym, best_r = None, -np.inf
for sym in roff_cols + [tmf]:
if sym not in data.columns:
continue
p_now = data[sym].iloc[i - 2]
p_past = data[sym].iloc[i - 2 - lb]
if pd.notna(p_now) and pd.notna(p_past) and p_past > 0:
r = float(p_now / p_past - 1.0)
if r > best_r:
best_r, best_sym = r, sym
if best_sym is not None:
for c in roff_cols:
w.iat[i, w.columns.get_loc(c)] = 0.0
w.iat[i, w.columns.get_loc(tmf)] = 0.0
w.iat[i, w.columns.get_loc(best_sym)] = roff_weight
# --- Layer 2: Vol-target overlay ---
daily_ret = data.pct_change(fill_method=None).fillna(0.0)
common = w.columns.intersection(daily_ret.columns)
port_rets = (w[common] * daily_ret[common]).sum(axis=1)
realized_vol = (
port_rets.rolling(self.vol_window, min_periods=21).std()
* np.sqrt(252)
)
scale = (self.target_vol / realized_vol).clip(
lower=self.min_lev, upper=self.max_lev,
)
scale = scale.shift(1).fillna(1.0)
w = w.mul(scale, axis=0)
# --- Layer 3: Profit-take with hysteresis (risk-on only) ---
# Risk-off assets (GLD, DBC, TMF) are exempt from PT.
if self.pt_threshold <= 0:
return w
risk_off_set = set(self.risk_off) | {self.tmf_symbol}
held = w.idxmax(axis=1)
max_w = w.max(axis=1)
held[max_w < 1e-8] = ""
park_col = self.pt_park if self.pt_park in w.columns else ""
entry_price: float | None = None
current_sym: str | None = None
is_stopped = False
restore_level = self.pt_threshold - self.pt_band
for i in range(len(w)):
sym = held.iloc[i]
if not sym or max_w.iloc[i] < 1e-8:
current_sym = None
entry_price = None
is_stopped = False
continue
if sym != current_sym:
current_sym = sym
entry_price = (
float(data[sym].iloc[i - 1])
if i > 0 and sym in data.columns
else None
)
is_stopped = False
continue
# Skip PT for risk-off assets
if sym in risk_off_set:
continue
if (
entry_price is None
or entry_price <= 0
or sym not in data.columns
):
continue
yesterday = float(data[sym].iloc[i - 1]) if i > 0 else float(data[sym].iloc[i])
gain = yesterday / entry_price - 1.0
if is_stopped:
if gain < restore_level:
is_stopped = False
else:
w.iloc[i] = 0.0
if park_col:
w.at[w.index[i], park_col] = scale.iloc[i]
else:
if gain >= self.pt_threshold:
is_stopped = True
w.iloc[i] = 0.0
if park_col:
w.at[w.index[i], park_col] = scale.iloc[i]
return w
__all__ = ["TrendRiderV8"]