Introduction:
Trading indices is a dynamic challenge requiring precision, adaptability, and robust strategies. With OverSeer, we’ve redefined how trading works by integrating machine learning models into the very core of the Expert Advisor. This post will walk you through the unique approach we’ve taken, how machine learning drives our strategy, and how it’s seamlessly integrated into MQL5.
Python Implementation: Harnessing the Power of Hidden Markov Models (HMMs)
The first step in OverSeer’s machine learning-driven strategy begins with the Python implementation. At its core, we use a Hidden Markov Model (HMM) to classify market states for each instrument. This statistical tool allows us to analyze patterns in price movements and categorize them into distinct states, forming the foundation of our trading decisions.
Why Use HMM for Market State Detection?
Hidden Markov Models are well-suited for this task because they excel at uncovering underlying structures in time-series data. Here’s how it works:
- Observation: The model observes price-based features (e.g., price changes, volatility, etc.) from historical data.
- State Identification: It assigns each observation to one of several hidden states, which represent distinct market conditions (e.g., trending, consolidating, volatile).
- Probability Mapping: Each state comes with probabilities, allowing us to understand the likelihood of transitioning from one state to another.
HMM is ideal for identifying patterns because it doesn’t rely on explicit labels—it learns directly from the data, making it robust and adaptive across diverse market conditions.
Visualizing HMM States
Here’s an example of how the HMM model identifies states in the data:
The chart above displays the defined market states of the HMM model on a segment of training data, where each color corresponds to a distinct market state. Our focus is on identifying states that represent relatively good buying opportunities—not perfect entries, but favorable ones compared to neighboring prices. In this chart, the market states represented by white and red align with this focus.
To simplify our analysis, we consolidate the states into two categories: red for the market states of interest (good buying opportunities) and blue for all other states. The image below illustrates this simplified categorization on the same training data slice.
The chart below applies the same color-coded market states to a segment of testing data, using the trained HMM model. Here, red represents the states identified as “good buying opportunities,” while blue indicates states not of interest. This visualization demonstrates the model’s ability to generalize its understanding of favorable market conditions to unseen data, maintaining consistency in identifying opportunities aligned with our focus.
A Balanced Approach to Trading Opportunities
It’s important to remember that the goal is not to identify perfect trade entries but rather to focus on overall good buying moments that provide a relative advantage compared to neighboring prices. This pragmatic approach, combined with exposure to multiple markets—each with its own model trained specifically for its unique behavior—is what makes OverSeer truly interesting and effective.
In the same way that we use models to determine good buying opportunities, we also train separate models to reflect on open positions. These models focus on identifying optimal moments for position management, supported by additional algorithms to make dynamic, data-driven decisions for safeguarding profits and managing risks. OverSeer ensures that both entry and management decisions are grounded in actionable insights tailored for each market.
Machine Learning Integration Directly in MT5
Integrating machine learning into MT5 posed a unique challenge, as Hidden Markov Models (HMMs)—used to define market states—do not support direct conversion to the ONNX format required for seamless integration. To overcome this, we introduced an intermediary step:
-
Training a Classifier Model:
Using the market states defined by the HMM model, we train a Random Forest Classifier. This model learns to replicate the state classifications provided by the HMM, effectively bridging the gap between Python’s powerful statistical tools and MT5’s real-time trading logic. -
ONNX Conversion:
Once trained, the Random Forest Classifier is converted into the ONNX format. This standardized format ensures compatibility with MT5, enabling the model to be directly imported into the Expert Advisor’s source code. -
Real-Time Application in MT5:
Within OverSeer, the imported ONNX models operate in real-time, evaluating features calculated from incoming data and generating decisions for both trade entries and position management. This integration ensures that the insights derived from machine learning are seamlessly applied to live trading scenarios.
This solution highlights OverSeer’s ability to leverage advanced machine learning models while maintaining the practicality and efficiency required for real-time trading in MT5.
Link to OverSeer: https://www.mql5.com/en/market/product/120625