Objective: Find truly complementary alpha combinations for composite stratégies.
Problem Statement
Current composite stratégies often have Sharpe ratios that are linear combinations of underlying alphas. We need to find: - Uncorrelated return streams (différent market regimes, différent asset classes) - Asymmetric complementarity (one alpha profits when the other is flat/losing) - Synergistic combinations (combined Sharpe > weighted average)
Methodology
Return Stream Collection: Gather daily returns from all alpha stratégies
Regime Analysis: Map performance across market regimes (bull/bear/sideways, high/low vol)
Complementarity Score: Rank pairs by asymmetric performance
Downside Protection: Measure how well stratégies protect in drawdowns
Alpha Universe
Trend Following
EMA-Cross-Alpha: Tech stock EMA crossover (Sharpe ~1.0)
TrendStocks-Alpha: 5-stock trend following (Sharpe ~0.6)
Trend-Following: Competition EMA 50/200
Momentum
SectorMomentum: 9 sector ETF relative momentum
DualMomentumNoTLT: IEF/GLD/XLP dual momentum
Defensive/All-Weather
AllWeather: Permanent portfolio style allocation
MeanReversion: Short-term mean reversion
Factor-Based
FamaFrench: 5-factor model implementation
Pairs/Market Neutral
ETF-Pairs: Cointegration-based pairs trading
Options-VGT: Wheel strategy (income generation)
# QuantConnect Research Environmentfrom datetime import datetime, timedeltatry:from AlgorithmImports import*except (ImportError, ModuleNotFoundError):passimport pandas as pdimport numpy as np# Initialize QuantBooktry: qb = QuantBook()# Set analysis period qb.SetStartDate(2020, 1, 1) qb.SetEndDate(2024, 12, 31)print(f"Analysis period: {qb.StartDate} to {qb.EndDate}")exceptNameError:print("Local environment detected - yfinance will be used for data")
Local environment detected - yfinance will be used for data
Note sur l’environnement d’exécution (#8772) :
Ce notebook déclare en cell[1] la période 2020-01-01 → 2024-12-31 via qb.SetStartDate / qb.SetEndDate. Dans l’environnement qui a produit les sorties committées, QuantBook() lève NameError (QC Cloud / Lean indisponible) et la branche de repli yfinance prend le relais (mécanisme 2 de #8772).
Conséquences : - Les sorties ci-dessous proviennent du chemin local yfinance, pas d’un QuantBook. Ce repli est une source de données externe légitime (#7066), annoncée ici explicitement plutôt que silencieuse. - La cellule de chargement fige la fenêtre sur la période déclarée, avec les mêmes bornes sur les deux chemins : qb.History(symbole, début, fin) côté QuantBook, history(start=..., end=...) côté yfinance. Les sorties ne dépendent donc plus de la date d’exécution ; deux exécutions successives ne diffèrent qu’au-delà de la troisième décimale (arrondis de la source sur les prix ajustés). - Auparavant, le repli demandait les cinq dernières années (period="5y"), comptées depuis l’heure d’exécution : la fenêtre glissait d’une exécution à l’autre, et les chiffres cités en prose avec elle (#21836).
Dans un environnement Lean local dont les données equity s’arrêtent avant la fin de la période (#8734), le chemin QuantBook prolongerait la dernière barre en constante (fillDataForward=True), ce qui produit l’artefact de ligne plate de #8719. Vérifier la plage de dates imprimée par la cellule de chargement avant de lire les chiffres.
1. Define Alpha Stratégies
We’ll implement simplified versions of each alpha to generate return streams.
# Define tickers for our alpha universetickers = ['SPY', 'TLT', 'GLD', 'XLP', 'IEF', 'AAPL', 'MSFT', 'GOOGL', 'AMZN', 'NVDA','XLK', 'XLE', 'XLF', 'XLV', 'XLY', 'XLB', 'XLRE', 'XLU']# Register assets on QC Cloudassets = {}try:for t in tickers: assets[t] = qb.AddEquity(t, Resolution.DAILY).Symbolprint(f"Added {len(assets)} assets to QuantBook")exceptNameError:print(f"Using {len(tickers)} tickers (local environment)")
Using 18 tickers (local environment)
Pivot de la série ‘close’ en DataFrame large, avec remapping des colonnes Symbol → ticker pour Alpha-Correlation-Analysis.
# Fetch historical data per-ticker (avoids MultiIndex Symbol lookup issue)# Fixed window = the period declared in cell[1], identical on both paths# (end bound exclusive): the outputs no longer depend on the execution date.WINDOW_START = datetime(2020, 1, 1)WINDOW_END = datetime(2025, 1, 1)data = {}for ticker in tickers: loaded =False# Try QC Cloud per-tickertry: hist = qb.History(assets[ticker], WINDOW_START, WINDOW_END, Resolution.Daily)ifhasattr(hist, 'shape') andlen(hist) >0:ifisinstance(hist.index, pd.MultiIndex):try: df_t = hist.loc[ticker]exceptKeyError: df_t = hist.droplevel(0)else: df_t = hist.copy()iflen(df_t) >0: data[ticker] = df_t['close'] loaded =Trueexcept (NameError, KeyError, TypeError):pass# yfinance fallback for this ticker, same fixed windowifnot loaded:try:import yfinance as yf raw = yf.Ticker(ticker).history(start=WINDOW_START.strftime("%Y-%m-%d"), end=WINDOW_END.strftime("%Y-%m-%d"), auto_adjust=True)iflen(raw) >0: series = raw['Close'] series.index = series.index.tz_localize(None) data[ticker] = series loaded =TrueexceptExceptionas e:print(f" WARNING: {ticker} unavailable ({e})")closes = pd.DataFrame(data)# Forward-fill to align different ticker date ranges, drop entirely-empty columnscloses = closes.ffill().dropna(axis=1, how='all').dropna(how='all')print(f"Data shape: {closes.shape}")print(f"Tickers loaded: {len(closes.columns)}/{len(tickers)}")print(f"Date range: {closes.index.min()} to {closes.index.max()}")print(f"Columns: {list(closes.columns)}")
Calculate how well pairs complement each other: - Low correlation: Uncorrelated return streams - Regime diversification: One excels where the other struggles - Drawdown protection: One protects when the other draws down
def calculate_complementarity_score(alpha1_returns, alpha2_returns, regimes):"""Calculate complementarity score for a pair of alphas. Score components: 1. Correlation: Lower is better (0-1) 2. Regime diversification: One excels where other struggles 3. Downside protection: One protects when other draws down """# Remove NaN common_idx = alpha1_returns.dropna().index.intersection(alpha2_returns.dropna().index) r1 = alpha1_returns.loc[common_idx] r2 = alpha2_returns.loc[common_idx] regimes_common = regimes.loc[common_idx]# 1. Correlation score (lower correlation = higher score) corr = r1.corr(r2) corr_score = (1-abs(corr)) # 0-1 scale# 2. Regime diversification regime_scores = []for regime in regimes_common['regime'].unique(): mask = regimes_common['regime'] == regimeif mask.sum() >10: sharpe1 = r1.loc[mask].mean() / r1.loc[mask].std() * np.sqrt(252) if r1.loc[mask].std() >0else0 sharpe2 = r2.loc[mask].mean() / r2.loc[mask].std() * np.sqrt(252) if r2.loc[mask].std() >0else0# Diversification benefit: one positive when other negativeif (sharpe1 >0and sharpe2 <0) or (sharpe1 <0and sharpe2 >0): regime_scores.append(abs(sharpe1 - sharpe2) /2)elif sharpe1 >0and sharpe2 >0: regime_scores.append(max(sharpe1, sharpe2) -min(sharpe1, sharpe2)) regime_div_score = np.mean(regime_scores) if regime_scores else0# 3. Downside protection# When alpha1 is in drawdown, does alpha2 protect? cumret1 = (1+ r1).cumprod() cumret2 = (1+ r2).cumprod()# Find drawdown periods for alpha1 drawdown_mask = (cumret1 < cumret1.cummax()) & (cumret1 < cumret1.iloc[0])if drawdown_mask.sum() >10:# Alpha2's performance during alpha1's drawdowns dd_return2 = r2.loc[drawdown_mask].mean() *252 protection_score =max(0, dd_return2) # Positive returns during drawdown = goodelse: protection_score =0# Combined score (weighted) total_score =0.4* corr_score +0.4*min(regime_div_score, 1) +0.2*min(protection_score, 1)return {'correlation': corr,'corr_score': corr_score,'regime_diversification': regime_div_score,'downside_protection': protection_score,'total_score': total_score }# Calculate scores for all pairscomplementarity = []for i, alpha1 inenumerate(returns_df.columns):for alpha2 in returns_df.columns[i+1:]: scores = calculate_complementarity_score( returns_df[alpha1], returns_df[alpha2], regimes ) complementarity.append({'Pair': f"{alpha1} / {alpha2}",**scores })comp_df = pd.DataFrame(complementarity).sort_values('total_score', ascending=False)print("Top 10 Most Complementary Pairs:")print(comp_df.head(10)[['Pair', 'correlation', 'regime_diversification', 'downside_protection', 'total_score']].round(3))
Template for validating pairs with out-of-sample testing.
def walk_forward_validation(returns_df, alpha1, alpha2, train_periods=252, test_periods=63):"""Walk-forward validation for a pair. Args: returns_df: DataFrame with all returns alpha1, alpha2: Names of alphas to test train_periods: Training period in days test_periods: Test period in days """ results = []for start inrange(0, len(returns_df) - train_periods - test_periods, test_periods): train_end = start + train_periods test_end = train_end + test_periodsif test_end >len(returns_df):break# Training period metrics train_corr = returns_df.iloc[start:train_end][alpha1].corr( returns_df.iloc[start:train_end][alpha2] )# Test period combined performance test_combined = ( returns_df.iloc[train_end:test_end][alpha1] + returns_df.iloc[train_end:test_end][alpha2] ) /2 results.append({'Period': f"{returns_df.index[start].date()} to {returns_df.index[test_end-1].date()}",'Train_Corr': train_corr,'Test_Return': test_combined.mean() *252,'Test_Sharpe': test_combined.mean() / test_combined.std() * np.sqrt(252) if test_combined.std() >0else0 })return pd.DataFrame(results)# Example: Test top pairtop_pair = comp_df.iloc[0]['Pair']alpha1, alpha2 = [a.strip() for a in top_pair.split('/')]wf_results = walk_forward_validation(returns_df, alpha1, alpha2)print(f"\nWalk-Forward Validation: {top_pair}")print(wf_results)print(f"\nAverage Test Sharpe: {wf_results['Test_Sharpe'].mean():.2f}")
Walk-Forward Validation: EMA-Cross-Tech / Mean-Reversion
Period Train_Corr Test_Return Test_Sharpe
0 2020-01-02 to 2021-04-01 0.196052 -0.016206 -0.133264
1 2020-04-02 to 2021-07-01 0.070892 0.316264 3.309655
2 2020-07-02 to 2021-09-30 0.081561 0.093083 1.035006
3 2020-10-01 to 2021-12-30 0.129635 0.354952 2.752040
4 2020-12-31 to 2022-03-31 0.120971 -0.194254 -2.840700
5 2021-04-05 to 2022-07-01 0.079755 -0.081086 -1.138385
6 2021-07-02 to 2022-09-30 0.058772 -0.308550 -3.301252
7 2021-10-01 to 2022-12-30 0.033750 -0.014223 -1.651654
8 2021-12-31 to 2023-04-03 0.011628 0.246734 2.004357
9 2022-04-01 to 2023-07-05 0.009998 0.470357 3.948303
10 2022-07-05 to 2023-10-03 0.021766 -0.023141 -0.214532
11 2022-10-03 to 2024-01-03 0.031235 -0.115057 -1.345589
12 2023-01-03 to 2024-04-04 0.017079 0.441859 4.148449
13 2023-04-04 to 2024-07-05 0.022092 0.430853 3.962713
14 2023-07-06 to 2024-10-03 0.140989 -0.280142 -2.305780
Average Test Sharpe: 0.55
10. Next Steps
For Production Implementation:
Build Composite Algorithms: Implement top pairs as QCAlphaModel composites
Dynamic Weighting: Add regime-based allocation between alphas
Risk Management: Add position sizing based on correlation regime
Validation: Run full backtests on recommended pairs
Top Candidates for Composites:
Based on this analysis, prioritize: 1. Trend + Defensive: EMA-Cross + All-Weather 2. Momentum + Mean Reversion: Dual-Momentum + Mean-Reversion
3. Multi-Asset: Dual-Momentum + All-Weather 4. Regime Switching: Trend-Following + Mean-Reversion
Research Extensions:
Add sector-specific alphas (SectorMomentum)
Include options-based stratégies (Options-VGT)
Test with crypto assets (BTC, ETH)
Factor analysis: loadings to Size, Value, Momentum factors