RGM Regime Detection
Markets don't behave the same way all the time — learning to identify the current state
Learning Objectives
- •Understand why regime detection is critical for adaptive trading
- •Implement Hurst exponent for trend vs. mean-reversion identification
- •Use clustering to identify distinct market regimes
- •Evaluate and avoid common regime detection pitfalls
Explain Like I'm 5
Markets alternate between different modes — trending, choppy, volatile, calm. Regime detection tries to figure out which mode the market is in right now, so your strategy can adapt.
Think of It This Way
It's like a weather system. You don't use the same clothing strategy for summer and winter. Regime detection is your market thermometer — it tells you whether to prepare for sunshine or storms.
1Why Regime Detection is a Meta-Signal
2The Hurst Exponent
3Regime Clustering
4ADX and Trend Strength
5Autoencoder Regime Detection
6Regime Detection Pitfalls
Key Formulas
Hurst Exponent (R/S Method)
H > 0.5 indicates trending behavior, H < 0.5 indicates mean-reversion, H ~ 0.5 is a random walk
Hands-On Code
Hurst Exponent and Regime Detection
import numpy as np
def compute_hurst(prices, max_lag=100):
"""Compute Hurst exponent using R/S analysis."""
returns = np.diff(np.log(prices))
lags = range(10, max_lag)
rs_values = []
for lag in lags:
rs_list = []
for start in range(0, len(returns) - lag, lag):
chunk = returns[start:start + lag]
mean = np.mean(chunk)
cumdev = np.cumsum(chunk - mean)
R = np.max(cumdev) - np.min(cumdev)
S = np.std(chunk, ddof=1)
if S > 0:
rs_list.append(R / S)
if rs_list:
rs_values.append((np.log(lag), np.log(np.mean(rs_list))))
if len(rs_values) < 3:
return 0.5
rs_arr = np.array(rs_values)
coeffs = np.polyfit(rs_arr[:, 0], rs_arr[:, 1], 1)
return coeffs[0]
def detect_regime(prices, lookback=252):
"""Classify current market regime using multiple indicators."""
returns = np.diff(np.log(prices[-lookback:]))
hurst = compute_hurst(prices[-lookback:])
vol = np.std(returns) * np.sqrt(252)
autocorr = np.corrcoef(returns[:-1], returns[1:])[0, 1]
if hurst > 0.55 and vol < 0.15:
regime = 'quiet_trend'
elif hurst > 0.55 and vol >= 0.15:
regime = 'volatile_trend'
elif hurst < 0.45:
regime = 'mean_reverting'
else:
regime = 'random_walk'
return {
'regime': regime,
'hurst': round(hurst, 3),
'annualized_vol': round(vol, 4),
'autocorrelation': round(autocorr, 4)
}Computes the Hurst exponent using rescaled range analysis and classifies the current market regime based on Hurst, volatility, and autocorrelation indicators.
Knowledge Check
Q1.A Hurst exponent of 0.38 suggests the market is:
Q2.What is the primary advantage of unsupervised clustering for regime detection?
Assignment
Compute the rolling 120-day Hurst exponent for three FX pairs (EUR/USD, GBP/USD, USD/JPY) over 3+ years. Identify periods where Hurst > 0.55 and Hurst < 0.45. Backtest a simple strategy: use momentum when Hurst > 0.55 and mean-reversion when Hurst < 0.45. Compare to a non-regime-conditional version.