This is the third article from my series of articles on developing neural network-based pattern recognition indicators for MetaTrader5. Here, you can find an introduction to the series. In the first article, I describe the process of collecting data for neural network model training using the MT5 script. Here, you can find the second article describing training neural networks in Google Colab using collected data. In the following article you can find instructions on installing and configuring DT-Box-Inference and the necessary MT5 files. Finally, here you find the source code of DT-Box-Inference.
Running Your Model in Prediction Mode
Once your model is trained and saved, you can feed it new, unseen data to obtain classification results. This operation is known as prediction mode, where the neural network utilizes previously learned parameters to process new pattern data and perform classification. Binary classification models, like ours, produce results in the form of a float number ranging from 0 to 1 (from 0% to 100%), where 100% indicates that the model is ‘absolutely sure’ that the dataset matches the pattern it was trained to recognize, and 0% indicates a non-pattern. Essentially, this value represents the probability that the data corresponds to the pattern.
DT-ML indicator
I have developed an MT5 indicator that sends bars data to our trained model and visualizes its predictions as a curve oscillating between 0 and 1. In our example, after each bar close, we will collect the last 42 bars and send them to our trained model for prediction. For each dataset sent, the model will produce a probability indicating whether the dataset represents the pattern. Visually, this will look as follows:
The indicator you see in the picture displays the probability that each set of 42 bars, counting backwards from each bar, represents the pattern we are searching for. In our example, if the model is ‘100% sure’ that the dataset reflects the pattern, this can be used as a signal to sell.
Here is another screenshot: the model predicted a 97% probability that the highlighted 42 bars represent our pattern, prompting us to send a sell order:
Strictly speaking, a probability above 50% is considered indicative of the pattern (i.e., the last 42 bars represent the pattern we seek). However, we can set a higher threshold—such as 95% or even 100%—to ensure actions are taken only when the model demonstrates a high degree of confidence.
Running the model in DT-Box-Inference
Popular trading platforms like MetaTrader 5, NinjaTrader, cTrader, TradeStation, and TradingView are not designed to directly execute machine learning models. Therefore, if you’ve trained your model using the Keras framework with TensorFlow as the backend, you will need to establish an intermediary process or environment that can interface with both MT5 and the machine learning model.
A straightforward method is to use the Keras or TensorFlow libraries within a Python environment. This setup involves running a separate Python process that handles the model’s execution, allowing your trading platform to make requests to the model and receive predictions in real-time. Alternatively, TensorFlow Serving can be used (with Docker on Windows), or ONNX (using model conversion tool). Another option is to encapsulate your model into a DLL using TensorFlow libraries (for Windows you will have to build TensorFlow libraries from the source code).
For DT-Box-Inference, I’ve chosen the approach of running a separate Python process that handles TensorFlow/Keras models. This setup manages requests from the MetaTrader5 indicator and sends back predictions through a socket. It also allows for running several models simultaneously and accepts connections from multiple indicators to a single model. To minimize latency for every connected indicator, DT-Box-Inference maintains its own context of previously sent data and predictions, enabling the DT-MLIndicator to send only a single bar per prediction request.
It also manages the loading of models into the TensorFlow execution environment, pattern data normalization, latency monitoring, and many other tasks. With DT-Box-Inference, you can use your trained pattern recognition indicators just like standard MT5 built-in indicators.
Configuring DT-MLIndicator to Display Predictions from Your Model
Once you’ve selected and downloaded your best model, the next step is to run the model in DT-Box-Inference. Click the ‘Add Model’ button in the app and choose the Keras model.
Once added, DT-Box-Inference will load the model into the TensorFlow execution environment and open a socket on an allocated port. The model will be listed in the table of running models, where the ‘Status’ column displays the current model state and the ‘Port’ column shows the allocated port for data exchange through the socket:
Now it’s time to connect the MT5 indicator. In your MT5 terminal, navigate to Indicators -> pccom -> DL -> DT-MLIndicator (which will appear there after installing DT-Box-Inference or through manual installation if using source code) and drag it onto a chart. In the dialog that appears, you will see two parameters: port and the number of recalculated bars:
Set the port value to match the corresponding model’s port. The ‘PrecalculateBars’ parameter determines how many bars back from the current moment will be processed, in other words, how many bars back you want the indicator to run the prediction.
If you use the default value of 10,000, this means that during initialization, the indicator will send 10,000 bars to DT-Box-Inference, and the model will perform 10,000 predictions, each based on a sliding window of 42 bars (as per our example). The process normally takes a few seconds, depending on your hardware. After initialization is completed, with each new bar close, the indicator will send only the current bar.
Click ‘OK,’ and after initialization, your indicator will appear on the chart:
As I mentioned before, the indicator’s value for each bar represents the probability that the last 42 bars (in our example) correspond to the pattern we are looking for.
Several models and multiple indicator instances
With DT-Box-Inference, you can use your model just like a regular MT5 indicator. This includes adding the indicator to multiple charts, switching between timeframes, and placing several instances on the same chart. In the examples provided, I use one model for the M5 timeframe and another for the M30 timeframe:
It is possible to connect any number of indicators to a single model. DT-Box-Inference will display the number of connected indicators in its status line.”
Backtesting and Optimization
Using the DT-MLIndicator is not limited by real-time charts, you can also use your model as a custom indicator in your MT5 strategy:
This allows you to integrate it as a standard indicator, similar to CCI or RSI, while programming your strategies and let DT-Box-Inference handle the complex tasks of data preprocessing and model execution..
Once you added DT-MLIndicator to your strategy, you can run your it in backtesting or optimization modes. Here’s an example of DT-MLIndicator running in visual backtesting mode:
Note that for every prediction, DT-Box-Inference logs the calculation time. While this can be useful in real-time, it may flood your log during backtesting. You can use a checkbox ‘Log single prediction time’ to enable/disable the logging of calculation times.
Bringing It All Together
That’s basically it—we’ve found and marked our patterns on MetaTrader5 charts, and prepared training and validation datasets. We designed the architecture of a neural network and trained it on the data collected in Google Colab. Then we used trained neural network as MetaTrader5 indicator using DT-Box-Inference app. Moreover, all of this was accomplished without writing a single line of code or manually manipulating the data.
Now, you are fully equipped with the knowledge and tools to elevate your manual, grey-box, or fully automated trading strategies using advanced ML techniques, such as neural networks. If you are interested in enhancing or customizing the DT-Box-Inference application to fit your specific needs, the source code is available on my GitHub.
If you like my series of articles on using neural network as MT5 indicators, please take 5 minutes and let me know you thoughts, I will be grad to hear from you – pavel@pavelchigirev.com