Regime detection identifies the current market "behavior mode" (trending, ranging, volatile). Different strategies work in different regimes.
Related:
- GLOSSARY.md — Definitions of HMM, BOCPD, etc.
- FORECAST.md — Price forecasting
- SAMPLE-TRADE-ADVANCED.md — Using regimes as trade filters
Markets cycle through distinct behavioral phases:
| Regime | Characteristics | Strategy Implication |
|---|---|---|
| Low Volatility / Ranging | Price oscillates in a band | Mean reversion works; trend-following fails |
| Trending | Directional momentum | Trend-following works; mean reversion fails |
| High Volatility / Crisis | Large unpredictable swings | Reduce size or stay out |
A strategy that profits in one regime may lose in another. Regime detection helps you:
- Filter trades: Only enter when conditions match your strategy
- Adjust sizing: Reduce risk in unfavorable regimes
- Detect breakouts: Identify when the market is transitioning
1. Hidden Markov Model (HMM)
What it does: Classifies each bar into one of N hidden "states" based on return and volatility patterns.
How it works:
- Assumes the market switches between N underlying states
- Each state has characteristic mean return and volatility
- Uses observed data to estimate which state is currently active
Example:
mtdata-cli regime_detect EURUSD --timeframe H1 --method hmm --params "n_states=2"Output:
summary:
last_state: 0
state_shares:
0: 0.623
1: 0.377
state_sigma:
0: 0.000303 # Low volatility state
1: 0.000739 # High volatility state
Interpretation:
- State 0: Low volatility (σ = 0.0003) — ranging/quiet market
- State 1: High volatility (σ = 0.0007) — trending/active market
- Currently in State 0 (low volatility)
Parameters:
| Parameter | Default | Description |
|---|---|---|
n_states |
2 | Number of regimes to detect |
When to use:
- Ongoing regime classification
- Filtering strategies by market type
- Multi-day to multi-week analysis
What it does: Detects moments when the market's statistical properties changed.
How it works:
- Bayesian Online Change Point Detection
- Estimates probability that each bar marks a regime transition
- Doesn't classify regimes—just detects when changes occur
Example:
mtdata-cli regime_detect EURUSD --timeframe H1 --method bocpd --threshold 0.5 --detail summaryOutput:
summary:
last_cp_prob: 0.471
max_cp_prob: 0.471
change_points_count: 0
Interpretation:
last_cp_prob: 0.471— 47% probability that a regime change just occurred- Below threshold (0.5), so not flagged as a change point
- Higher probabilities indicate likely structural breaks
Parameters:
| Parameter | Default | Description |
|---|---|---|
threshold |
0.5 | Probability cutoff to flag a change point |
lookback |
300 | Historical bars to analyze |
When to use:
- Detecting breakouts
- Alerting on market structure changes
- Invalidating stale forecasts
What it does: Combines HMM with autoregressive modeling. Each regime has its own AR parameters.
Example:
mtdata-cli regime_detect EURUSD --timeframe H1 --method ms_ar --params "k_regimes=2 order=1"When to use:
- When regime changes affect both mean and autocorrelation structure
- Academic research contexts
What it does: Groups bars into regimes using distance-based clustering (e.g. KMeans on return/volatility features). Does not assume a hidden Markov structure.
Example:
mtdata-cli regime_detect EURUSD --timeframe H1 --method clustering --params "n_clusters=3"When to use:
- Exploratory regime discovery without a parametric model
- When HMM convergence is unstable
Only enter trend-following trades when HMM detects high-volatility state:
# Check current regime
mtdata-cli regime_detect EURUSD --timeframe H1 --method hmm --params "n_states=2"
# If state_sigma shows current state is high-volatility:
# → Enable trend-following entries
# If current state is low-volatility:
# → Disable trend-following or switch to mean reversionMonitor for regime transitions and reduce exposure when detected:
# Check for recent change points
mtdata-cli regime_detect EURUSD --timeframe H1 --method bocpd --threshold 0.6
# If last_cp_prob > 0.6:
# → Tighten stops
# → Reduce position size
# → Consider closing positionsRun barrier optimization separately for each regime:
# In low-volatility regime: tighter barriers
mtdata-cli forecast_barrier_optimize EURUSD --timeframe H1 --horizon 12 \
--tp-min 0.15 --tp-max 0.5 --sl-min 0.1 --sl-max 0.4
# In high-volatility regime: wider barriers
mtdata-cli forecast_barrier_optimize EURUSD --timeframe H1 --horizon 12 \
--tp-min 0.5 --tp-max 2.0 --sl-min 0.3 --sl-max 1.5mtdata-cli regime_detect EURUSD --timeframe H1 --method hmm --detail compactShows regime segments: start time, end time, duration, state ID.
mtdata-cli regime_detect EURUSD --timeframe H1 --method hmm --detail summaryShows aggregate statistics: current state, state distributions, volatility per state.
mtdata-cli regime_detect EURUSD --timeframe H1 --method hmm --detail fullShows per-bar state assignments and probabilities.
| Task | Command |
|---|---|
| Classify regimes (2 states) | mtdata-cli regime_detect EURUSD --method hmm --params "n_states=2" |
| Classify regimes (3 states) | mtdata-cli regime_detect EURUSD --method hmm --params "n_states=3" |
| Detect change points | mtdata-cli regime_detect EURUSD --method bocpd --threshold 0.5 |
| Markov-switching AR | mtdata-cli regime_detect EURUSD --method ms_ar --params "k_regimes=2" |
| Clustering | mtdata-cli regime_detect EURUSD --method clustering --params "n_clusters=3" |
- GLOSSARY.md — Term definitions
- FORECAST.md — Price forecasting
- VOLATILITY.md — Volatility estimation
- BARRIER_FUNCTIONS.md — TP/SL analysis