Since MT5 build 3620, released in March 2023, MetaQuotes added native support for ONNX models. ONNX support has been expanded across several builds since, including Float8 and Float16 inputs in build 4230 (March 2024) and CUDA-accelerated GPU inference in build 5572 (January 2026). You can train a model in Python (or any ONNX-compatible framework) and run inference directly inside an MQL5 EA. The mechanics are straightforward; whether ML actually improves your trading is a much harder question.
ONNX (Open Neural Network Exchange) is a standard format for representing machine learning models. The idea: train a model in any framework (PyTorch, TensorFlow, scikit-learn, XGBoost), export to .onnx, and run inference in any environment that supports the format. The runtime is portable, the model artifact is a single file.
MetaQuotes added ONNX runtime support to MT5 starting with build 3620, released on March 10, 2023. This means MQL5 code can call OnnxCreate, OnnxRun, and related functions to perform inference on an ONNX model embedded in or loaded alongside an EA. Support has been actively expanded since: build 4230 (March 2024) added Float8 and Float16 input types, and build 5572 (January 2026) added CUDA-accelerated GPU inference on supported hardware.
The core functions:
| Function | Purpose |
|---|---|
OnnxCreate | Load an ONNX file and create a session handle |
OnnxCreateFromBuffer | Load an ONNX model from an in-memory byte buffer (useful for embedding the model as a resource) |
OnnxRun | Perform inference: provide input tensor(s), receive output tensor(s) |
OnnxSetInputShape / OnnxSetOutputShape | Specify tensor shapes when the model has dynamic dimensions |
OnnxRelease | Free the session and resources |
The full API is documented in the official MQL5 reference under the Onnx section. Function signatures and supported tensor types may change between MT5 builds; consult current docs before writing production code.
The pattern for using ONNX in an EA, simplified:
// In OnInit
long onnx_handle = OnnxCreate("model.onnx", ONNX_DEFAULT);
if(onnx_handle == INVALID_HANDLE)
{
Print("Failed to load model");
return(INIT_FAILED);
}
// Specify input shape if dynamic
const long input_shape[] = {1, 10}; // 1 batch, 10 features
OnnxSetInputShape(onnx_handle, 0, input_shape);
// In OnTick (or OnTimer, on bar close)
float features[10];
// ... fill features array with normalised inputs from market data
float prediction[1];
if(!OnnxRun(onnx_handle, ONNX_DEFAULT, features, prediction))
{
Print("Inference failed");
return;
}
// Act on prediction[0]
The actual ONNX file must be placed in the MQL5/Files directory of the MT5 data folder, or embedded into the EA as a resource.
Most common ML model types can be exported to ONNX:
Things that do not work well:
The ONNX file is the easy part. Feature engineering is where most ML trading projects fail.
Common features used in retail-scale ML trading:
The trap: feature engineering on historical data is a form of data snooping. Every feature you add that "works" in backtest may simply be exploiting historical noise. Walk-forward validation is non-negotiable.
Workflow for building a model to use in MT5:
torch.onnx.export, skl2onnx.convert_sklearn).ONNX inference inside MT5 is fast for typical retail models. A small tree-based model with 10-20 features runs in well under a millisecond. Even moderate neural networks (a few thousand parameters) run in single-digit milliseconds.
This is fast enough for any non-HFT use case. EAs that act on bar close (1-minute or longer) have hundreds of milliseconds of latency budget; ONNX inference is not the bottleneck. EAs that try to act on every tick may need to consider whether feature computation (not the ONNX inference itself) is fast enough.
| Pitfall | Why it goes wrong |
|---|---|
| Training on future-leaking features | Using future data to compute features that the EA cannot compute in real time. The model looks brilliant in research; useless in production. |
| Normalisation drift | Features were normalised during training using the mean and std of the training period. If you do not apply the same normalisation in the EA, inputs go to the model on different scales. |
| Target leakage through features | Including the target itself or a closely correlated quantity as a feature. Common in lagged-return setups when lag is misaligned by one bar. |
| Overfitting through hyperparameter tuning | Tuning on validation data turns validation data into training data. Hold out a true test set you only use once. |
| Ignoring trading costs | Model predictions need to exceed spread plus commission to be worth acting on. Most "profitable" predictions have edges smaller than costs. |
| Stationarity violations | Markets change. A model trained on 2018-2022 may not generalise to current regimes. Retraining schedules and regime detection are open research problems. |
An honest answer: probably not as your first or second EA. The conventional wisdom in quantitative trading is that simple, robust, well-understood strategies outperform complex ML approaches in most retail contexts. Reasons:
That said, ONNX in MT5 is a legitimate tool for traders who have already built statistical intuition with simpler strategies and want to encode more complex relationships. The mechanics are sound; the strategic application is where most attempts fail.
If you do want to experiment:
Inference uses CPU by default. GPU inference may be supported in some MT5 builds but is rarely needed for retail-scale models. Training (in Python, outside MT5) may benefit from GPU; inference does not.
No. MT5 supports ONNX inference but not training. You must train externally (typically in Python) and import the resulting model. To retrain periodically, your workflow needs an external retraining pipeline that produces updated .onnx files.
This changes between MT5 builds. Recent builds support a broad set of ONNX operators; very recent operators from latest ONNX specifications may not be supported. Check the current documentation if your model export complains about unsupported operators.
Yes. The .onnx file is portable. If your EA loads the model from MQL5/Files, distribute the .onnx alongside the EA. If the model is embedded as a resource in the EA, the .ex5 file contains it.
No. Retail traders have always used computers to assist trading; ML is just another tool. The market does not care which tools you use; it cares whether your decisions are profitable after costs.