Hidden Markov Model Market Regimes [LuxAlgo]The Hidden Markov Model Market Regimes indicator uses a mathematical framework to identify whether the current market environment is in a Trending or Ranging state. By analyzing log returns and volatility through a Hidden Markov Model (HMM) approach, the tool provides real-time probability estimates for each regime to help traders adapt their strategies to changing market conditions.
🔶 USAGE
The indicator is displayed in a separate pane below the price chart, showing two oscillating lines representing the probability (0% to 100%) of the market being in a "Trending" or "Ranging" regime.
Trending Regime (Blue): High values indicate a directional market with expanding volatility and persistent returns.
Ranging Regime (Gray): High values indicate a mean-reverting environment with lower volatility and compressed price action.
Neutral Zone: When both lines are near the 50% level, the model is uncertain about the current regime, suggesting a potential transition.
Traders can use these regimes to filter signals: trend-following strategies (like moving average crosses) typically perform better when the Trending probability is high, while mean-reversion strategies (like oscillators) are often more effective during high Ranging probability periods.
🔶 DETAILS
The script implements a simplified version of the HMM Forward Algorithm to estimate "hidden" market states from observable price data.
🔹 Observation Data
The model processes two main inputs:
Log Returns: Used to detect directional movement and momentum.
Volatility: Calculated via standard deviation to differentiate between high-activity trends and low-activity ranges.
🔹 The Forward Algorithm
The indicator calculates "Emission Likelihoods" based on Gaussian heuristics. It assumes that a Ranging state is characterized by low volatility and returns near zero, while a Trending state is characterized by higher volatility and significant price displacement.
The algorithm then combines these likelihoods with the "Transition Matrix" (which assumes regimes tend to persist over time) to calculate the posterior probability of each state. The Learning Rate setting determines how much weight is given to the newest bar's data versus the previous state's probability.
🔶 SETTINGS
🔹 HMM Settings
Lookback Period: Determines the window used to calculate mean returns and volatility benchmarks. A longer lookback provides more stable regime detection, while a shorter lookback is more reactive.
Learning Rate: Controls the sensitivity of the probability updates. A higher value makes the indicator switch between regimes faster, while a lower value filters out market noise.
🔹 Dashboard
Enable Dashboard: Toggles the visibility of the on-screen information table.
Position: Moves the dashboard to different corners of the chart (Top Right, Bottom Right, or Bottom Left).
Size: Adjusts the scale of the dashboard text and cells.
אינדיקטור Pine Script®






















