Quant for Free
Method pointer

Isolation Forest: execution-quality circuit breaker in pure MQL5

IsoForestExecMonitor scores each opening fill against a rolling Isolation Forest trained on the EA’s own recent execution history. On anomaly it can cool down new entries, widen open stops, or halt when the rolling anomaly rate trips. Monitor layer, not a signal — it does not predict price direction.

What the article ships

MQL5 · native IF

Attached MQL5.zip: IsoForestExecMonitor.mq5 (host EMA-cross + monitor), IsoForest.mqh (CIsoTree / CIsoForest), ExecFeatureLogger.mqh (rolling buffer + CSV), and iso_forest_crossvalidation.py for offline synthetic checks. No ONNX, ALGLIB, or external DLL.

OnTradeTransaction → feature vector → AnomalyScore → cool-down / widen / halt
5-D fill vector pure MQL5 rolling retrain circuit breaker
Open MQL5 article → Nested CV

IF monitor vs Nested CV vs oos-lab

choose
  • Use Isolation Forest execution monitor You already have (or are building) a live/demo EA and need multivariate surveillance of fill quality — slippage, latency, spread-at-fill, volume deviation — with cool-down / widen / halt. Not for choosing price-signal hyperparameters.
  • Use Nested CV You are selecting classifier hyperparameters and fitting a probability calibrator under temporal labels. Research-stack honesty before you trust an OOS return path.
  • Use oos-lab / DSR calculator You already have return series (or a Sharpe + trials) and need PSR / Deflated Sharpe / PBO–CSCV / purged splitters. Complements the fill monitor; does not replace it.

What the pipeline actually does

Documented from article 24251 only — not invented. Scope note from the author: monitoring and circuit-breaking, not alpha.

Features

Per-fill 5-D vector

Slippage (points), fill latency (ms via GetTickCount64), spread-at-fill (points), volume deviation (%), reject flag (placeholder 0.0 on confirmed fills).

Model

Rolling Isolation Forest

Native CIsoTree / CIsoForest: random splits, path-length scoring s(x,n)=2^(-E(h)/c(n)), binary persist to FILE_COMMON.

Breaker

Cool-down → widen → halt

Isolated anomaly: pause entries for N bars, optionally widen open stops. Sustained rolling anomaly rate: hard halt and investigate.

feature contract

ISO_FEATURE_COUNT = 5

OnInit three-way check: compile-time define vs logger header vs persisted forest. Mismatch → INIT_FAILED (loud, not silent).

defaults (article inputs)

100 trees · subsample 256 · thr 0.68

Training window 500 fills; retrain every 100; cool-down 10 bars; halt if anomaly rate ≥ 0.35 over trailing 30 trades; widen multiplier 1.5.

scoring hook

OnTradeTransaction DEAL_ENTRY_IN

Request price/ticks captured before Buy/Sell. Cold start: observe-only until training window fills; placeholder score 0.5.

offline companion

iso_forest_crossvalidation.py

Synthetic slippage/latency injection + sklearn IsolationForest rank sanity-check. Threshold calibrated offline — not auto-tuned in-EA.

Score formula (from the article)

Path length averaged across trees, normalized by unsuccessful BST search length c(n)=2H(n−1)−2(n−1)/n with H(i)≈ln(i)+0.5772156649. Scores near 1 flag strong anomalies; the paper’s asymptotic midpoint is 0.5. The article’s shipped default threshold 0.68 comes from offline calibration on one real log (working band cited as roughly 0.65–0.70) — not a universal constant.

Feature vector (contract)

Hard per-feature slippage caps remain useful for single extreme fills. The forest watches for jointly unusual patterns that pass every univariate guard — neither substitutes for the other.

Attributed validation notes (source only)

Synthetic check (article): take a real demo CSV, inject ~5% contamination by adding uniform slippage in [15, 40] points and latency in [800, 2500] ms (seed=42), then compare native scores’ ranking to sklearn. On a 900-fill demo log the article reports score separation supporting a threshold in the 0.65–0.70 band. Sensitivity sweep on that log: soft peak near 150 trees × subsample 256 — author guidance is to re-grid on your log, not copy the peak blindly.

Strategy Tester P&L comparison (article): XAUUSD M5 EMA host, same date range (28,098 bars / 36,520,722 ticks), breaker ON (threshold 0.68) vs OFF (threshold 2.0). Reported metrics identical: 695 trades, net −292.40, profit factor 0.98, max equity DD 19.74%, 0 anomalies / cooldowns / halts. Author’s reading: Tester fill model lacked realistic variance — a null P&L test, not a claim the breaker is inert. Of 1,585 scored fills (excluding cold-start placeholders), all real scores sat under 0.68; 58/100 trees were single-node (constant-feature pitfall).

When not to treat this as a signal

Per the article’s scope: it will not make a mediocre strategy profitable and does not forecast price. Cold-start forests overfit tiny windows — do not shrink InpTrainingWindow just to “activate” scoring. Multi-symbol / concurrent-order / async / partial-fill-split contexts are explicitly out of scope for the shipped globals. Automatic in-EA threshold calibration and news-calendar suppression are also not implemented.

Where this sits on Quant for Free

Sits next to Nested CV and oos-lab in the tools row: research honesty and return-series stats upstream; fill-quality circuit breaker at the live/demo contact surface. Pair with Module 5 — Validation Gauntlet and process memos under Quant for Life when you separate execution controls from P&L stories.

Questions

What is IsoForestExecMonitor?
A MetaTrader 5 EA from MQL5 article 24251 with a native Isolation Forest that scores opening fills on a five-feature execution-quality vector and can cool down, widen stops, or halt on rolling anomaly rate. Monitor / circuit breaker — not a price signal.
When should I use this vs Nested CV or oos-lab?
IF monitor for live/demo fill-quality surveillance. Nested CV for HP selection + calibration under temporal leakage. oos-lab / DSR calculator for return-series validation stats. They stack.
Does Strategy Tester prove the breaker helps P&L?
No — the article’s ON vs OFF Tester runs were identical with zero flags. Author attributes that to the Tester’s lack of realistic fill variance. Synthetic injection on a demo log was used to check scoring rank quality instead.
Is this investment advice?
No. Educational and methodological use only. This page points at a published monitor design; it does not recommend any security, strategy, or trade.
Educational tool, not investment advice. This page summarizes an open MQL5 article’s native Isolation Forest execution monitor and does not predict performance or recommend any trade. Strategy Tester and attributed demo-log figures are the article’s illustrations, not Quant for Free live results. Past results, real or simulated, do not guarantee future outcomes. See the disclaimer.