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

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:

The two-stage pipeline

  1. 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.
  2. Train a classifier. Take the cluster labels as ground truth and train a tree-based classifier on the same features. Export to ONNX.
  3. 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:

This is more robust than swapping logic. The base strategy still works; its parameters adapt to context.