Hidden Markov model (regime filter)

A hidden Markov model (HMM) is a statistical model in which the market is assumed to move between a small number of unobserved states, such as bear, range and bull, and each state emits observations such as bar returns with its own distribution; fitted to a window of bars it returns the most likely regime for each bar together with a probability.

Senzoukria · Glossary · Updated September 2026


What the model assumes

Two ingredients define an HMM. A transition matrix gives the probability of staying in a state or moving to another one on the next bar; an emission distribution gives, for each state, how the observed values are spread. In the Gaussian version each state has its own mean and variance of returns. Neither the states nor the transitions are observed directly; they are inferred from the sequence of bars.

Fitting the model means finding the transition matrix and the emission parameters that make the observed sequence most probable. Decoding means, given a fitted model, computing for each bar the probability of each state. The state with the highest probability is reported as the regime, and that probability is the model's confidence.

Two things it can observe

  • Returns: log returns of the bars. This is the classic quant regime model, and the resulting states describe price behaviour: trending down, ranging, trending up.
  • Delta: the bar's order-flow imbalance, delta divided by volume, where delta is ask volume minus bid volume. The hidden state then describes aggression rather than price. Two bars can print the same return with opposite flow, and this source separates them.
  • States are sorted by their mean, so state 0 is always the most bearish and the last state the most bullish, whichever source is used.

How to read it as a filter

A regime filter does not generate entries. It answers whether the conditions a strategy was designed for are currently present. A mean-reversion rule, for example, can be restricted to the range state and switched off in the strongly directional states. The expected remaining duration of a regime, derived from the probability of staying in it, is an estimate under the model's assumptions, not a forecast of the next bar.

  • Low confidence near a state change is normal. The model is uncertain precisely when the regime is turning.
  • The fit depends on the window. A 500-bar fit and a 2000-bar fit can label the same bar differently.
  • Refitting on every tick would make labels jump; a sound implementation refits on bar close and decodes the live bar against the frozen model.

In Senzoukria

The overlay is listed in the footprint indicators as HMM Regime (quant), described as bear / range / bull regimes fitted on the bars with the model's confidence. Its settings are States (2 to 4), Observes (returns or delta), Fit window in bars, and appearance options for the band and an optional column tint. The Scripts editor also ships a Python template, Mean reversion under regime (HMM + Hurst), which rules out the crisis regime with the HMM before a z-score entry and a Hurst confirmation; it is Python only and needs numpy, and like the other templates it runs on the simulated account or in replay unless the autopilot is armed by hand.

Common mistakes

  • Reading the regime label as a prediction rather than as the most probable description of the bars already seen.
  • Choosing four states on a short window and interpreting the extra states as insight when they mostly fit noise.
  • Tuning the fit window until the labels agree with a chart you already like, then treating the result as validated.
  • Ignoring that the delta source needs executed bid and ask volume; on a feed without side classification the model has nothing meaningful to observe.

This page in other languages

Frequently asked questions

How is an HMM regime different from a moving-average trend filter?
A moving-average filter applies a fixed rule to prices and has no notion of uncertainty. An HMM infers states from the distribution of returns or flow, allows for regime persistence through its transition probabilities, and reports how confident it is. It is more flexible and also more sensitive to the window and the number of states it is asked to find.
What does the confidence number mean?
It is the posterior probability of the reported state for that bar, given the fitted model and the observations. A value near one means the model finds the other states implausible for that bar; a value near one divided by the number of states means it cannot tell them apart. It is a statement about the model, not a probability that a trade will work.
Can the regime filter place or block orders on its own?
No. It is an overlay and a component of a scripted strategy. Any order still goes through a strategy in a simulated account, replay, or the optional autopilot, which must be armed explicitly on a compatible account with its own limits.

Keep reading