Native python support

The potential success of automated trading largely depends on the breadth of technology that is available in the implementation of the idea. As we have already seen in the previous sections, MQL5 allows you to go beyond strictly applied trading tasks and provides opportunities for integration with external services (for example, based on network functions and custom symbols), processing and storing data using relational databases, as well as connecting arbitrary libraries.

The last point allows you to ensure interaction with any software that provides API in the DLL format. Some developers use this method to connect to industrial distributed DBMSs (instead of the built-in SQLite), math packages like R or MATLAB, and other programming languages.

Python has become one of the most popular programming languages. Its feature is a compact core, which is complemented by packages which are ready-made collections of scripts for building application solutions. Traders benefit from the wide selection and functionality of the packages for fundamental market analysis (statistical calculations, data visualization) and testing of trading hypotheses, including machine learning.

Following this trend, MQ introduced Python support in MQL5 in 2019. This tighter "out-of-the-box" integration allows the complete transfer of technical analysis and trading algorithms to the Python environment.

From a technical point of view, integration is achieved by installing the "MetaTrader5" package in Python, which organizes interprocess interaction with the terminal (at the time of writing this, through the ipykernel/RPC mechanism).

Among the functions of the package, there are full analogs of the built-in MQL5 functions for obtaining information about the terminal, trading account, symbols in Market Watch, quotes, ticks, Depth of Market, orders, positions, and deals. In addition, the package allows you to switch trading accounts, send trade orders, check margin requirements, and evaluate potential profits/losses in real-time.

However, integration with Python has some limitations. In particular, it is not possible in Python to implement event handling such as OnTick, OnBookEvent, and others. Because of this, it is necessary to use an infinite loop to check new prices, much like we were forced to do in MQL5 scripts. The analysis of the execution of trade orders is just as difficult: in the absence of OnTradeTransaction, more code would be needed to know if a position was fully or partially closed. To bypass these restrictions, you can organize the interaction of the Python script and MQL5, for example, through sockets. The mql5.com site features articles with examples of the implementation of such a bridge.

Thus, it seems that it is only natural to use Python in conjunction with MetaTrader 5 for machine learning tasks that deal with quotes, ticks, or trading account history. Unfortunately, you can't get indicator readings in Python.