- ONNX Support
- Format Conversion
- Automatic data type conversion
- Creating a Model
- Running a model
- Validation in the Strategy Tester
- OnnxCreate
- OnnxCreateFromBuffer
- OnnxRelease
- OnnxRun
- OnnxGetInputCount
- OnnxGetOutputCount
- OnnxGetInputName
- OnnxGetOutputName
- OnnxGetInputTypeInfo
- OnnxGetOutputTypeInfo
- OnnxSetInputShape
- OnnxSetOutputShape
- Data structures
Creating a Model
Multiple methods are available to obtain a ready model in the ONNX format. The popular ONNX Model Zoo library contains several pre-trained ONNX models for different types of tasks. The advantage of this collection is that each model's notebook contains links to the training dataset and references to the original paper that describes the model architecture.
Most machine learning frameworks use Python. To install the ONNX runtime for Python, use one of the following commands:
pip install onnxruntime # CPU build
|
To invoke the ONNX runtime in Python, use the following command
import onnxruntime
|
For model inputs and outputs, check out the relevant model's documentation. You can also use visualization tools to view the model, such as Netron or WinML Dashboard. In the ONNX runtime, you can also query a model's metadata and its inputs and outputs:
results = session.run(["output1", "output2"], {
|
You can create ONNX models directly in the MetaTrader 5 terminal or in MetaEditor using Python.
Python in MetaTrader 5
The MetaTrader 5 provides out-of-the-box support for Python scripts. To enable these operations, the terminal developers provide MetaTrader5 module for Python: https://pypi.org/project/MetaTrader5.
In the MetaEditor integrated development environment, in addition to creating applications in MQL5, you can also run Python scripts directly from the editor. To do this, specify the path to the executable in MetaEditor settings:
If Python is not installed on your computer, click Install to download the installation file.
You can create a Python script in MetaEdtior or upload it to the terminal's data folder and immediately run it using the F7 (Compile) key. This will open the MetaTrader 5 terminal with the script running on the current chart. Messages from the Python console (stdout, stderr) will be displayed under the Errors section.
Operations with models in MetaTrader 5
The MQL5 language allows you to run ONNX models directly in the MetaTrader 5 terminal. This is done in three steps:
- Train the model in a third-party platform, such as Python.
- Convert the model to ONNX.
- Include the ONNX model into an Expert Advisor using ONNX function and run in the MetaTrader 5 terminal.
Python integration in MQL5 allows running a python script and saving an ONNX model in the MetaEditor or run it directly on a chart in MetaTrader 5. You can train the model using a pre-written Python script as often as you need right in the terminal. The library includes ready-made functions for obtaining price data, which can be input into an ONNX model:
- copy_rates_from - get bars starting from the specified date
- copy_rates_from_pos - get bars starting from the specified index
- copy_rates_range - get bars for the specified date range
- copy_ticks_from - get ticks starting from the specified date
- copy_ticks_range - get ticks for the specified date range
Model example #
En example of a finished ONNX model is available in public projects. You should first activate MQL5 Storage in the Navigator by specifying you MQL5 login in MetaEditor settings (case sensitive).
After activation, find the ONNX.Price.Prediction project and join it via the context menu command.
Next, update the project from MQL5 Storage.
The project contains an ONNX model, two python scripts, an MQL5 script for project operation, and an MQL5 project file (ONNX.Price.Prediction.mqproj).
You can create the ONNX model yourself using the PricePredictionTraining.py script included in the project. To do this, you should first install the required modules from the command line.
python.exe -m pip install --upgrade pip
|
After installing the modules, open the PricePredictionTraining.py script in MetaEditor and run it with the Compile button or with the F7 key.
Before running the Python script, make sure that the MetaTrader 5 terminal is connected to a server with the EURUSD symbol available. For example, connect to the MetaQuotes-Demo server and check "Integration with Python" in terminal settings.
While training the network, MetaEditor will print messages from the Python script until the training is complete.
When the result is 100%, the ONNX model is ready and it is saved to the project folder at <terminal data directory>\MQL5\Shared Projects\ONNX.Price.Prediction\Python.
You can check the resulting model by running the second script PricePrediction.py, pressing F7.