I created this library because the development of an Expert Advisor in MQL5 can be complicated,while, in python, the same task flows better.
I believe that the main advantage of using python, instead of MQL5, to develop an expert advisor is the possibility to implement machine learning, deep learning, and reinforcement learning to your code in a faster way. However, I still don´t cover these.
I find that using the “MetaTrader module for integration with Python” is straightforward; yet, there is space for improvement.
Thus, I created this library, aiming to transform the experience of Expert Advisor Creation. Python is a versatile language; it counts with a wide range of useful libraries, which, in turn, allows the implementation of diverse ideas, easily.
pip install MetaTrader5
pip install MetaTrader5
There is an expert advisor example for each different technology in the Expert Advisor folder.
It uses just the MetaTrader5 library in the implementation.
The “MetaTrader module for integration with Python” enables a wide range of possibilities; still, there is space for improvement. For instance, it lacks a connection to indicators.
That problem can be solved:
- Using Python indicators libraries that already exist through the internet.
- Re-creating all the indicators
- Creating a connection with MetaTrader5, asking for some indicators.
This example uses a socket connection to simplify the usage of indicators and it shows how easy it is to implement. You can find the indicator connection file here.
To be able to use socket connection you need the client-side and the server-side to allow the communication.
The python code, that is used in this example, is the server-side that waits for the client-side to send the indicator result asked for.
The python code that is used in this example is the server-side that waits for the client-side to send the indicator result asked for. In order to use the client-side, it is possible to code it in MQL-language or to use the ones that I already created:
- here - Free - 5 Indicators
- here - Not Free - 38 Indicators (5 from free indicators plus 33 new ones). The iCustom is not implemented.
To use the indicator in the expert advisor, you can call it providing the symbol name, all the other variables have pre-configured values. It always uses a time_frame for 1 minute.
from Include.indicator_connector import Indicator
indicator = Indicator()
moving_average = indicator.moving_average(symbol='PETR4') # Brazilian Stock
You can check this file to find some tips when calling an indicator, for example, on how to change the method or the applied price.
def moving_average(self,
symbol,
time_frame=1,
period=20,
start_position=0, # Change it if you want past values, zero is the most recent.
# method:
# 0 - MODE_SMA
# 1 - MODE_EMA
# 2 - MODE_SMMA
# 3 - MODE_LWMA
method=0,
# applied_price:
# 0 - PRICE_CLOSE
# 1 - PRICE_OPEN
# 2 - PRICE_HIGH
# 3 - PRICE_LOW
# 4 - PRICE_MEDIAN
# 5 - PRICE_TYPICAL
# 6 - PRICE_WEIGHTED
applied_price=0):
When using the indicators, please use try and except; sometimes the result can return "None."
from Include.indicator_connector import Indicator
indicator = Indicator()
while True:
try:
moving_average = indicator.moving_average(symbol='PETR4') # Brazilian Stock
except TypeError:
pass
The indicator result is a dictionary.
print(moving_average)
{'symbol': 'PETR4', 'time_frame': 1, 'period': 50, 'start_position': 0, 'method': 0, 'applied_price': 0, 'moving_average_result': 23.103}
print(moving_average['moving_average_result'])
23.103