Markets cycle through distinct regimes — low-vol drift, high-vol trend, mean-reversion, crisis. Different strategies win in different regimes. A "regime detector" labels the current state and lets the EA swap strategies (or parameters) accordingly. Where the market-structure classifier picks up/down/range from a fixed lookback, a regime detector aims at longer-lived states that persist for weeks rather than hours.
Approaches that work
- Unsupervised clustering (k-means, GMM) on feature vectors aggregating multiple weeks of data.
- Hidden Markov Models (HMMs) — the classical approach; models regime transitions explicitly.
- Supervised classifier with synthetic labels generated by a clustering pass — then export the supervised model to ONNX.
HMMs are elegant but harder to ship through ONNX (no native HMM op — you'd export only the emission distribution). For an ONNX-friendly pipeline, train a clustering model in Python, label the historical data, then train an MLP or LightGBM classifier on those labels. The classifier exports cleanly.
Feature aggregation over weeks
The regime detector's input isn't the last 100 bars — it's statistics over the last several weeks. Typical features:
- 20-day realized volatility.
- 20-day return.
- 20-day max drawdown.
- Skew and kurtosis of returns.
- Spread of high-low ranges normalized.
- Average true range as a fraction of price.
The two-stage pipeline
- Cluster offline. Fit GMM with 4 components on aggregated feature vectors. Inspect each cluster's properties — "this one is low-vol drift," "this one is trending," etc.
- Train a classifier. Take the cluster labels as ground truth and train a tree-based classifier on the same features. Export to ONNX.
- Run in MQL5. On each new bar, compute the aggregated features, run the classifier, get the current regime. Switch EA behavior accordingly.
EA behavior switching
Don't try to switch entire strategies. Switch parameters of the same strategy:
- Low-vol regime: small lots, tight SL, distant TP.
- High-vol trending: medium lots, wider SL, generous TP.
- Crisis regime: pause trading.
This is more robust than swapping logic. The base strategy still works; its parameters adapt to context.