Discussing the article: "A Categorical Hidden Markov Model in MQL5"

Check out the new article: A Categorical Hidden Markov Model in MQL5.

The article provides a detailed examination of the theoretical foundations and practical implementation of the hidden Markov model with categorical emissions (Categorical HMM) in the MQL5 language. Specific examples are used to demonstrate the processes of inference, iterative parameter learning, and online filtering, as well as the methodology for selecting the optimal model architecture based on the AIC/BIC information criteria.

The subject of this article is the categorical hidden Markov model (HMM) — a classic example of the SSM class with discrete latent states and a categorical distribution of observed data. Transitions between states in the system are described using a transition probability matrix.

For example, the model allows you to identify hidden market regimes based on a sequence of market events (white or black candles). In one regime (let's call it an uptrend), white candles appear much more frequently; in another (a downtrend), black candles are more common; and in a third (a sideways market), their probabilities are equal. Of course, we do not observe the regimes themselves; they are hidden. The model's goal is to infer the current market state by analyzing only the available data (candlestick colors).

To test such hypotheses, the CCategoricalHMM class was implemented in MQL5. Using simple and clear examples, we will explore the class's main features:
  • calculation of posterior probabilities of hidden states (Inference),
  • learning the model parameters using the EM algorithm (Fit),
  • synthetic data generation (Sample),
  • selecting the optimal number of latent states using the AIC and BIC criteria (Model Selection),
  • real-time online filtering of hidden states (Filter).


Author: Evgeniy Chernish

From an educational perspective, you write some very interesting articles. As these are very advanced topics, it would be more helpful if you could demonstrate how they can be applied to trading using a few examples. For example, how can one use the ‘Hidden States’ filter online in real time?Is this a substitute for traditional moving averages? How can one benefit from the EM algorithm (Fit), etc.?
cemal moving averages? How can one benefit from the EM algorithm (Fit), etc.?

Thank you for your feedback.

You see, filtering and a moving average are completely different things. A moving average is simply an average calculated over a sliding window. Filtering is the process of calculating the probability distribution of hidden states. It is a set of probabilities. In other words, it is an assessment of the state we are in at the current moment in time. If the probability is higher for an uptrend, it means the market is most likely in that state, and you can then open a position in line with the trend. If the probability of being in a state of noise (a 50/50 chance of a white candle) is higher, it means the market is currently random and opening trades is not recommended. That is the main idea. Therefore, if you use moving averages to determine an entry point, the HMM acts as a safeguard that will prevent you from opening a trade if it determines that there is no trend at the moment.

But before implementing the trading algorithm, we need to detect these hidden states. How can we do this if we only have a dataset? Using a learning algorithm (EM). It’s like finding parameters in an MLP via gradient descent. By analysing the parameters obtained, we can conclude whether any hidden states are present in the data at all, or whether the market is in a state of chaos and there is only one mode (equal emission probabilities for each state).

Train the model for the coin-toss scenario (white/black candlestick) for two states (fair/biased); you will then be able to better understand the model’s capabilities.