Discussing the article: "Hidden Semi-Markov Models for Duration-Aware Regime Detection in MQL5"

 

Check out the new article: Hidden Semi-Markov Models for Duration-Aware Regime Detection in MQL5.

Standard HMMs assume geometric, memoryless state durations, which poorly match real market phases. This piece implements a duration‑explicit Hidden Semi‑Markov Model natively in MQL5, with per‑state sojourn distributions and a residual‑time forward filter. Parameters are fit offline via EM and loaded through a compact JSON manifest. The EA for XAUUSD M5 uses expected remaining duration to gate entries and exits, helping hold trends while avoiding late entries near regime exhaustion.

Most regime-detection write-ups use a plain Hidden Markov Model: a handful of states, a transition matrix, and a forward–backward pass to estimate the current state. This assumes a geometric sojourn time: the probability that a regime ends on the next bar is constant, regardless of how long it has lasted. Markets do not behave that way — trends and chop both have momentum in a mean-duration sense.

A Hidden Semi-Markov Model (HSMM) fixes this by pulling duration out of the transition matrix and giving each state its own explicit sojourn-time distribution. The model then carries a live estimate of how much longer the current state is expected to run, which lets an Expert Advisor hold a trend trade through the middle of a move but step aside once that estimate shrinks toward zero.

This follows the same offline-Python/live-native-MQL5 pipeline as earlier regime work in this series, but answers a different question: not just which state this is, but how much of it is probably left.


Author: Adewumi Babatunde Gbadebo