Hidden Markov Regime Engine MT5
- Indicatori
-
Anthony Cooke
Quantitative Systems Architect and C++, Python, MQL5, MQL4, and Pine Script developer specializing in non-repainting mathematical indicators, Expert Advisors, and statistical strategy audits. I combine industrial quality-control engineering and Applied Mathematics with multi-physics market modeling - Versione: 1.0
- Attivazioni: 5
OVERVIEW AND SCIENTIFIC ORIGIN
The Hidden Markov Regime Engine is a quantitative market-state indicator engineered from Probabilistic Machine Learning and Quantitative Econometrics (3-State Gaussian Hidden Markov Model using the Hamilton-Kim Online Forward Filter). Used by systematic asset managers to identify hidden market regimes, a forward-only Hidden Markov Model estimates the live probability of each market state strictly from past and current bars without backward Viterbi smoothing and without repainting.
Across Forex pairs, Gold (XAUUSD), Silver (XAGUSD), and US Indices, this indicator classifies live price action into three hidden states: State 0 (Bull Trend), State 1 (Range / Neutral Chop), and State 2 (Bear Trend).
MATHEMATICAL ENGINE
On every closed bar across your lookback window (InpPeriod = 55), the filter constructs a scale-invariant 2D observation vector [z_r, z_v] combining causal EMA-smoothed log-return drift (InpDriftEMA = 5) and short-term versus baseline True Range volatility expansion. It propagates prior state probabilities through a 3x3 Markov Transition Matrix controlled by diagonal state persistence (InpPersistence = 0.92), evaluates stabilized Gaussian emission log-likelihoods scaled by InpSensitivity (1.00), and outputs normalized Bayesian posterior probabilities summing to 100%.
SUBWINDOW VISUAL GUIDE
Subwindow Scale (-105.0 to +105.0, dashed confirmation levels at +60.0, 0.0, and -60.0):
- MediumSeaGreen Histogram (HMM_Bull_Regime): Confirmed Bull Trend (Bull probability dominant and signed conviction at or above +60%).
- OrangeRed Histogram (HMM_Bear_Regime): Confirmed Bear Trend (Bear probability dominant and signed conviction at or below -60%).
- DimGray Histogram (HMM_Range_Regime): Range / Neutral Chop or transitional uncertainty (helps avoid chasing breakouts during sideways consolidation).
- Gold Line (HMM_Conviction_Curve): Smoothed signed posterior conviction calculated from 100 * (P_Bull - P_Bear).
- Lime Dot (Bull_State_Switch) and Magenta Dot (Bear_State_Switch): Reversal-locked regime-switch dots printed on the Gold curve strictly on confirmed directional flips beyond +/-60% (InpConfirmPct).
DARK TELEMETRY HUD BOX GUIDE
Anchored in the upper-right subwindow inside a dark slate panel with a DodgerBlue border:
- Line 1 (HMM REGIME): Displays the active discrete regime (BULL TREND, BEAR TREND, or RANGE / NEUTRAL) and live signed conviction percentage.
- Line 2 (PROBS): Displays live Bayesian posterior probabilities across all three states (BULL %, RNG %, BEAR %, summing to 100%).
- Line 3 (ENTROPY and PERS): Normalized 3-state Shannon regime entropy measuring probabilistic certainty (Aqua below 0.45 = HIGH CLARITY, Gold 0.45 to 0.75 = TRANSITION, Silver at or above 0.75 = HIGH UNCERTAINTY), paired with configured Markov persistence %.
- Line 4 (DIRECTIVE): Live execution posture matching the confirmed state (TREND FOLLOW LONG, TREND FOLLOW SHORT, or MEAN-REVERT / STAND ASIDE).
PRACTICAL USAGE
Directional Trend Entries:
Look for MediumSeaGreen bars above +60% with a Lime Bull_State_Switch dot (or OrangeRed bars below -60% with a Magenta Bear_State_Switch dot), backed by low Shannon Entropy (HIGH CLARITY) on HUD Line 3.
Sideways Chop Filter:
When bars turn DimGray, the Gold curve flattens near 0.0, and the HUD reads RANGE / NEUTRAL with MEAN-REVERT / STAND ASIDE, pause trend-following entries until a clear probabilistic state switch occurs.
TECHNICAL SPECIFICATIONS AND INPUT PARAMETERS
Calculation Engine:
100% causal online forward filter with zero repainting, zero backpainting, and zero external DLL calls. Short name: HMM_Regime(55).
Core Inputs:
- InpPeriod (Default: 55): Lookback window for standardizing return drift and volatility emissions.
- InpDriftEMA (Default: 5): Causal EMA bars smoothing log-return drift.
- InpPersistence (Default: 0.92, Range 0.60 to 0.98): Markov diagonal persistence controlling how sticky each regime stays before switching.
- InpSensitivity (Default: 1.00, Range 0.40 to 2.50): Scales Gaussian emission log-likelihood responsiveness to new price evidence.
- InpConfirmPct (Default: 60.0): Signed conviction threshold (%) for directional histogram color and reversal-locked dots.
- InpShowHUD (Default: true): Toggles the 4-line dark telemetry panel (bypassed automatically during optimization).
Expert Advisor Integration (iCustom Buffers):
Buffer 0: HMM_Bull_Regime | Buffer 1: HMM_Bear_Regime | Buffer 2: HMM_Range_Regime | Buffer 3: HMM_Conviction_Curve | Buffer 4: Bull_State_Switch | Buffer 5: Bear_State_Switch | Buffer 6: HMM_State_ID (Hidden: +1.0 Bull, 0.0 Range, -1.0 Bear) | Buffer 7: HMM_Max_Prob (Hidden: dominant state probability 0% to 100%).
TIMEFRAME AND VOLATILITY CALIBRATION
Default settings (55 period, 5 drift EMA, 0.92 persistence, 1.00 sensitivity, 60% confirmation) are calibrated as a balanced M15 to H1 baseline for XAUUSD, Major Forex pairs, and US Indices:
- Faster Intraday Regimes (M15 to M30): Lower InpPersistence to 0.85 to 0.88 and raise InpSensitivity to 1.25 to 1.50 for earlier regime flips, or reduce InpPeriod to 34.
- Higher-Timeframe Macro Trends (H4 to D1): Keep InpPersistence at 0.92 to 0.95 with InpPeriod 55 to 89 and InpConfirmPct at 65% to 75% to filter short counter-trend pullbacks.
- Volatile Instruments (XAUUSD, US30, NAS100): Increase InpDriftEMA to 7 or 8 and keep InpConfirmPct at 65% to keep single-bar news whipsaws inside the DimGray range filter.
------Trading on MetaTrader 4 instead of MetaTrader 5? The identically engineered MetaTrader 4 edition of Hidden Markov Regime Engine is published on my MQL5 Seller Profile with the exact same thumbnail, 3-state forward Bayesian filter, dark telemetry HUD, and parameter set. Visit my seller profile to access the MT4 version.
