OPEN-SOURCE SCRIPT

Hidden Markov Model Market Regimes [LuxAlgo]

16 471
The Hidden Markov Model Market Regimes [LuxAlgo] tool provides a probabilistic framework for identifying the current state of the market by applying a Hidden Markov Model (HMM) to price action and volatility data.

🔶 USAGE

The indicator identifies four distinct market regimes. Each regime is represented by a unique color, and the oscillator values (0-100%) represent the probability of the market being in that specific state.

🔹 Regime Breakdown & Trading Implications

  • Low Volatility Trend (Gray): Characterized by steady, consistent returns with minimal price swings. Ideal for trend-following strategies and "buy-and-hold" positions.
  • High Volatility Chop (Orange): Large price swings without a clear directional trend. Suggests a "risk-off" environment or mean-reversion strategies.
  • Crash Regime (Red): Extreme negative returns coupled with a spike in volatility. Indicates high panic; traders may look for hedging opportunities.
  • Accumulation (Blue): Low volatility with neutral-to-rounding price action. Often occurs at the end of a bear cycle, signaling a potential "bottom-fishing" zone.


🔹 Visual Cues

The indicator features background highlighting that changes based on the "Dominant State" (the state with the highest current probability).

When a specific probability line crosses above the 50% "Neutral" line, the model is gaining conviction. Crosses above 80% indicate "High Confidence" in that regime. Users should watch for "probability flips," where one regime's dominance is rapidly overtaken by another, signaling a structural shift in the market.

🔶 DETAILS

The script implements a specialized version of the Forward Algorithm to estimate the likelihood of hidden states.

🔹 Heuristic vs. Trained ML

It is important to note that this indicator is accurately described as a "Heuristic" or "Static" HMM rather than a traditional Machine Learning model. Unlike modern ML models that require extensive training on historical datasets (using algorithms like Baum-Welch or Expectation-Maximization), this model uses fixed mathematical parameters and predefined heuristic-based emission profiles.

This approach ensures deterministic behavior—the model will always react to the same price patterns in the same way—and eliminates the "black box" nature of traditional ML while still providing the probabilistic benefits of a Markov chain.

🔹 Mathematical Logic

  • Log Returns: The model utilizes log returns ($TSX:LN(Close_{t} / Close_{t-1})$) rather than simple percentage changes to ensure time-additivity and a more symmetric distribution of data.
  • Emission Likelihoods: The model uses Gaussian-style probability density functions to compare normalized returns and volatility against "ideal" profiles for each state. For example, the Crash Regime likelihood increases when returns are significantly below the mean and volatility is significantly above the mean.
  • Transition Matrix (Markov Property): This defines the "memory" of the system. The matrix is weighted toward "persistence" (diagonal values), meaning the market is statistically more likely to stay in its current regime than to jump to a different one every bar.


🔶 SETTINGS

🔹 HMM Settings

  • Lookback Period: The window used to calculate the mean and standard deviation for normalizing returns and volatility.
  • Learning Rate: Controls how quickly the model updates its internal probabilities. Range: 0.01 (very slow/stable) to 1.0 (instant/reactive).


🔹 Dashboard

  • Enable Dashboard: Toggles the visibility of the on-screen information table.
  • Position: Determines where the dashboard is anchored (Top Right, Bottom Right, or Bottom Left).
  • Size: Adjusts the scale of the dashboard text and cells.

Clause de non-responsabilité

Les informations et publications ne sont pas destinées à être, et ne constituent pas, des conseils ou recommandations financiers, d'investissement, de trading ou autres fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.