Quantitative Trading Strategies: Momentum, Mean Reversion, and Beyond
Module: Fintech AI | Difficulty: Advanced
Momentum
Mean Reversion
Pairs Trading
Factor Returns
import numpy as np
from scipy.stats import zscore
class MomentumStrategy:
def __init__(self, lookback=252, holding=21):
self.lookback = lookback; self.holding = holding
def signal(self, prices):
returns = prices.pct_change(self.lookback)
return returns.iloc[-1]
def positions(self, signal, n_long=10, n_short=10):
ranked = signal.rank(ascending=True)
n = len(ranked)
long = ranked >= n - n_long
short = ranked <= n_short
return long.astype(int) - short.astype(int)
class PairsTrading:
def __init__(self, window=60, entry_z=2, exit_z=0):
self.window = window; self.entry_z = entry_z; self.exit_z = exit_z
def hedge_ratio(self, y, x):
from sklearn.linear_model import LinearRegression
reg = LinearRegression().fit(x.reshape(-1,1), y)
return reg.coef_[0]
def signal(self, y, x):
beta = self.hedge_ratio(y, x)
spread = y - beta * x
z = (spread[-1] - spread.mean()) / spread.std()
if z < -self.entry_z:
return 'long_spread'
elif z > self.entry_z:
return 'short_spread'
elif abs(z) < self.exit_z:
return 'close'
return 'hold'
Research Insight: Momentum and mean reversion are complementary strategies that perform well in different regimes. Combining them through a regime-switching model improves overall performance. The key insight is that momentum works in trending markets while mean reversion works in range-bound markets.