No description
  • Jupyter Notebook 84.6%
  • MQL5 15.4%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
JohnHlomohang 7e840e1247
2026-07-16 21:49:07 +02:00
AutoML Pipeline.mq5 2026-07-16 21:49:07 +02:00
Part10_AutoML_Pipeline.ipynb 2026-07-16 21:49:07 +02:00
README.md Add README.md 2026-07-16 19:29:58 +00:00

Article-23177-AutoML-Pipelines-for-Strategy-Testing

This repository is an article-derived reference project based on the original MQL5 article. It does not claim to reproduce the full original source code unless files are explicitly attached.

Overview

This project documents a complete end-to-end AutoML deployment pipeline for MetaTrader 5. Instead of manually tuning strategy parameters, the system trains a machine learning model on the historical outcomes of every EMA crossover signal and uses it to filter trades at runtime.

The pipeline automatically:

  • Fetches historical XAUUSD H1 data directly from MetaTrader 5
  • Engineers nine contract-locked market context features
  • Simulates historical trades to generate honest profit-or-loss labels
  • Runs FLAML AutoML to select and tune the best model automatically
  • Exports the trained model to ONNX format with MT5-compatible settings
  • Validates numerical parity between Python and ONNX inference
  • Embeds the model inside an MQL5 Expert Advisor as a compiled resource
  • Gates every EMA crossover signal through a configurable confidence threshold
  • Manages active positions with an ATR trailing stop and opposite-crossover exit
  • Exposes the confidence threshold as an optimizable Strategy Tester input

The project demonstrates how Python AutoML pipelines can be adapted to any rule-based trading strategy by swapping the signal logic and feature set while keeping the entire framework intact.

Original Article

Repository Purpose

This repository should be treated as a reference or reconstruction project derived from the article content.

Its purpose is to:

  • Study practical applications of AutoML in algorithmic trading
  • Explore FLAML model selection and hyperparameter tuning on financial data
  • Demonstrate ONNX export and native MQL5 model inference
  • Provide a reusable labeling and training framework for rule-based strategies
  • Serve as a foundation for confidence-gated Expert Advisor development

Key Concepts

Feature Contract

The model is trained on nine features computed from the signal bar. The EA must fill its input array in exactly this order:

Index Feature Description
0 ema_fast_rel EMA(12) / close - 1.0
1 ema_slow_rel EMA(26) / close - 1.0
2 ema_distance (EMA fast - EMA slow) / close
3 rsi Wilder RSI(14) on the signal bar
4 rsi_momentum RSI[bar 1] - RSI[bar 2]
5 atr_rel ATR(14) / close
6 volatility_ratio StdDev(close, 20) / StdDev(close, 100)
7 close_range_pct (close - low) / (high - low) of the signal bar
8 signal_direction +1.0 buy crossover, -1.0 sell crossover

Label Generation

Every EMA crossover in the historical data is simulated as a trade:

  • Entry — open of the bar following the crossover
  • Exit — close of the bar where the next opposite crossover appears, or after MAX_HOLD_BARS, whichever comes first
  • Label — 1 if the trade closed in profit, 0 if it closed in loss

AutoML Training

FLAML searches across LightGBM, XGBoost, and Random Forest using a chronological train/test split. The most recent 20% of signals form the hold-out set. The best model is selected by ROC-AUC with no manual hyperparameter decisions.

ONNX Export

Three settings prevent the three failures that break most first attempts:

Setting Problem prevented
target_opset={"": 12, "ai.onnx.ml": 2} Unsupported operator — both domains must be capped, not just the default
Post-export opset stamp to 12 MT5 rejecting the model — tree graphs stamp the default domain at opset 1, which runtimes reject
zipmap=False Output shape error — without this, probabilities are emitted as a map type MT5 cannot read

Confidence Gate

The EA evaluates every crossover signal through the model before placing a trade. The confidence threshold is exposed as an optimizable input (InpConfidence, range 0.50–0.75, step 0.05), allowing the Strategy Tester to find the value that best balances trade frequency against win rate.

Algorithm / Architecture Summary

1. Data Fetch (Python)

Historical OHLCV data is pulled from MetaTrader 5 using copy_rates_range and saved as a CSV file.

2. Feature Engineering (Python)

Nine features are computed using indicator formulas that numerically match their MT5 counterparts. Wilder smoothing is used for RSI and ATR. Standard deviation uses the sample formula (ddof=1) to match pandas rolling().std().

3. Label Generation (Python)

EMA crossovers are detected on the historical data. Each crossover is simulated as a trade. The outcome of each trade becomes the binary training label.

4. AutoML Training (FLAML)

FLAML runs a time-aware search over the training set. The estimator list is restricted to models that convert cleanly to ONNX opset 12.

5. ONNX Export (Python)

The best model is converted using skl2onnx and onnxmltools. Both opset domains are capped and the default domain is force-stamped after conversion. The output is validated against the original model before deployment.

6. Validation (Python)

onnxruntime loads the exported file and runs inference on the test set. The maximum probability difference between ONNX and sklearn must be below 1e-3.

7. EA Deployment (MQL5)

The ONNX file is embedded as a compiled resource. On every new bar, the EA detects crossovers, computes features, queries the model, and executes only when confidence exceeds the threshold.

8. Position Management (MQL5)

Active trades are managed through an ATR trailing stop on every tick and an opposite-crossover exit on every new bar, mirroring the exit logic used during label generation.

Signal Generation

Buy signal — fast EMA crosses above slow EMA on the closed bar, model confidence exceeds threshold

Sell signal — fast EMA crosses below slow EMA on the closed bar, model confidence exceeds threshold

Risk Management

Stop loss distance is calculated as:

SL = ATR(14) * InpSLxATR

Trailing stop distance is calculated as:

Trail = ATR(14) * InpTrailxATR

The trailing stop only activates once the position moves into profit and only moves in the direction of the trade.

A/B Testing

Setting InpUseModelGate = false disables the confidence filter entirely. The EA then trades every EMA crossover without restriction, acting as the raw baseline. Running both configurations over the same tester period isolates the model's contribution to performance.

AutoML in Trading: Concept Mapping

AutoML Term Trading Interpretation
Training data Simulated trade outcomes from historical crossovers
Feature Market context indicator computed at signal bar
Label 1 = trade closed in profit, 0 = trade closed in loss
Model selection FLAML choosing between LightGBM, XGBoost, Random Forest
Hyperparameter tuning FLAML internal search, no manual decisions
Inference EA querying the model on every new crossover signal
Confidence threshold Minimum P(profit) required to execute the trade
ONNX Cross-platform model format loaded natively by MT5

Mentioned or Attached Files

Primary Components

  • Part10_AutoML_Pipeline.ipynb
  • EMA_RSI_AutoML_EA.mq5
  • README.md

Generated Artifacts

  • XAUUSD_H1.csv — historical data fetched from MT5
  • ema_rsi_model.onnx — trained model exported to MT5 Files folder

Statistics

Pipeline Components

  • Data Fetch Module: 1
  • Feature Engineering Module: 1
  • Label Generation Module: 1
  • AutoML Training Module: 1
  • ONNX Export Module: 1
  • Validation Module: 1

EA Components

  • Feature Computation Engine: 1
  • ONNX Inference Engine: 1
  • Signal Detection Module: 1
  • Trade Execution Module: 1
  • Position Management Module: 1
  • Dashboard Module: 1

Model Features

  • Total features: 9
  • Feature types: EMA-based (3), RSI-based (2), ATR-based (1), Volatility (1), Price action (1), Direction (1)

Tags

MQL5 MetaTrader5 AutoML FLAML ONNX EMA-Crossover RSI Machine-Learning Expert-Advisor Algorithmic-Trading Python LightGBM XGBoost Feature-Engineering Strategy-Testing

Difficulty

Moderate — requires familiarity with:

  • MQL5 Expert Advisor development
  • Python data science toolchain (pandas, numpy, scikit-learn)
  • EMA and RSI indicator mechanics
  • FLAML AutoML configuration
  • ONNX model export and runtime validation
  • Trade simulation and label generation concepts

Limitations

  • The model is trained on crossover signals only — it does not trade non-crossover setups.
  • Label quality depends on the exit rule; real-world slippage and spread are not modeled.
  • The trailing stop and protective SL in the EA are not present in the label simulation — live exits may differ from what the model expects.
  • Model performance depends on the stationarity of the feature distributions across the training and deployment periods.
  • A single ONNX model handles all market regimes; regime-specific models may improve accuracy.

Future Improvements

Potential enhancements include:

  • Regime-specific model training (uptrend, downtrend, ranging)
  • Tick-data feature engineering for higher-resolution labels
  • Online learning with periodic model retraining
  • Multi-symbol training for cross-instrument generalization
  • Order block and fair value gap features
  • Reinforcement learning for exit management
  • Multi-timeframe feature stacking
  • Confidence-based position sizing

Reference

Original MQL5 article: https://www.mql5.com/en/articles/23177