Discussing the article: "Building an Object-Oriented ONNX Inference Engine in MQL5"

 

Check out the new article: Building an Object-Oriented ONNX Inference Engine in MQL5.

This article shows how to run Python-trained models natively in MetaTrader 5 via the terminal's ONNX functions. We build an MQL5 class that encapsulates session creation, fixes input/output tensor shapes, applies min-max feature normalization to mirror training, and executes OnnxRun once per bar to protect the CPU, the result is a reliable, maintainable inference path for live charts and the Strategy Tester without sockets or DLLs.

To understand the implementation mechanics, one must analyze how machine learning models are structurally saved and exported. The Open Neural Network Exchange is a universal open format built to represent machine learning models uniformly across different programming environments. When a neural network is exported from Python using libraries like TensorFlow or Scikit-Learn to ONNX, it is serialized into a static mathematical graph consisting of nodes and edges. Each node represents a specific mathematical operation, such as matrix multiplication or a non-linear activation function, while the edges represent the multidimensional arrays, known as tensors, flowing between these operations.

When the MetaTrader 5 terminal loads an ONNX file, it utilizes its native internal engine to reconstruct this exact mathematical graph in the system memory. The terminal requires precise instructions regarding the dimensionality of the data it will receive and the dimensionality of the data it must output. If a neural network was trained in Python to accept five technical indicators as inputs, the input tensor shape is strictly defined as an array of five elements. Attempting to feed raw market closing prices directly into the ONNX engine without matching this predefined tensor shape will result in an immediate terminal error and the failure of the inference session. Managing these input and output shapes programmatically is the absolute foundation of a stable integration.

Author: Amanda Vitoria De Paula Pereira