HMM Regime
Automatically detect market regimes and adjust strategy allocation
Risk
Medium
Holding Period
Varies by underlying strategy
Best For
Reducing drawdowns by scaling down in crisis periods
How it works
Automatically detect market regimes and adjust strategy allocation
Mathematical Foundation
P(regime_t | observations_{1..t}) via Viterbi algorithm on Gaussian HMMSignal Generation Logic
- 1Fit a 3-state Gaussian Hidden Markov Model to the daily returns and volatility of the asset
- 2The three states are labelled: Bull (low vol, positive drift), Neutral (medium vol, near-zero drift), Crisis (high vol, negative drift)
- 3Run the Viterbi algorithm to decode the most likely state sequence given observed returns
- 4Map the current regime to a signal: Bull → long, Crisis → short or flat, Neutral → depends on underlying sub-strategy
- 5Use the wrapped strategy (TSMOM by default) to generate the base signal, then scale or filter by HMM regime
- 6Re-fit the HMM periodically (every 63 bars) to incorporate new market data
Parameters Explained
n_statesNumber of hidden states in the HMM. 3 states (bull/neutral/crisis) is a common and interpretable choice. More states allow finer granularity but risk overfitting.
Default
3vol_windowShort-term volatility window used as an observation feature for the HMM. 20 days (~1 month) captures recent volatility regime.
Default
20When It Works
When markets exhibit clear regime transitions that are predictable from volatility and return patterns. Particularly effective at avoiding large drawdowns during crisis regimes by reducing or reversing positions before a downtrend fully develops.
When It Fails
When market regime transitions are sudden and not preceded by gradual volatility changes (flash crashes, sudden macro surprises). The HMM is a lagging indicator — by the time it detects a crisis, significant losses may have already occurred.
Risks & Limitations
- State labelling is subjective — the model learns statistical patterns, not economic regimes
- HMM assumes Gaussian returns, which underestimates tail risks in practice
- Frequent regime switching increases transaction costs from position adjustments
- Look-ahead bias risk if the HMM is re-fitted with future data during backtesting
- The model can get 'stuck' in the wrong regime during rapid market changes
- Parameter instability: HMM parameters can change dramatically when re-fitted on new data
Implementation
Uses hmmlearn's GaussianHMM with covariance_type='full'. The model is fitted on daily log-returns and EWMA volatility as a 2D observation. Regime decoding uses the Viterbi algorithm. The strategy wraps a base strategy (TSMOM by default) and scales its signals based on detected regime.
Model parameters
Number of States
Bull / mean-reverting / crisis regimes
Vol Window
Short-term vol input for the HMM
Academic background
Academic Basis
Based on Hamilton (1989), 'A New Approach to the Economic Analysis of Nonstationary Time Series and the Business Cycle', Econometrica
Backtest this strategy
Run the exact model on your selected assets and date range. See trade-by-trade performance.
Backtest This →