Metatrader 5 with Python

Hello Everybody!



How can use a python code with Metrader 5? Is it possible?

Answer updated on 17 Nov 2023



You can follow the below detailed & step by step guide to connect MetaTrader5 (MT5) with Python code



https://quantra.quantinsti.com/glossary/Automated-Trading-using-MT5-and-Python



-----------------------------------------------------------------------------



You can connect MT4/MT5 with Python using the ZeroMQ. 

  1. The API link: https://pyzmq.readthedocs.io/en/latest/api/index.html
  2. Blog link: https://blog.darwinex.com/zeromq-interface-python-r-metatrader4/
  3. Example code: https://github.com/darwinex/DarwinexLabs/blob/master/tools/Python/ZeroMQ_MT4_Python_Template.py
Thanks!

Morning, I’ll research about all the tecniques that’s you suggest, I have limited platafform in Brazil for trading operanting, for this propose I think MT5 is the best, but now a days I’m trading with profitchart, I’ll try to implement ZeroMQ_MT4_Python_Template in MT5. Thanks in advance.

Julio Cesar, were you able to achieve that? I'm also trying to use MT5 from Brazil.

Unfortunately, MT5 uses a domain specific language. Python code cannot be run directly from there. You need to create your own bridge but that will be quite involved. If it is just passing input/ output, probably you can use files to communicate between them. Other option is to use the python C API (or some tools like cython) to create a peice of c code that can import and run a pure python code (this usage is quite strange by the way, the primary aim of the C API is to run fast c functions from within Python, not the reverse). Then this c code can be compiled and linked (using a DLL) to MT5. Also, this will be specific to a python function as you need to take care of matching arguments and data types. In case you mostly use MT5 and plan to use only one (or two) python functions ever, this approach can be viable.



A better solution will be to use platforms that offer generic programmic interfaces directly (Quantra Blueshift is one, although right now it is a research platform, we plan to integrate live trading very soon!). Other alternative is to use the exposed broker API directly (like interactive broker). Many brokers will support python API calls.

MetaTrader 5 package for Python: https://www.mql5.com/en/forum/306742



pip install MetaTrader5



 

from datetime import datetime
from MetaTrader5 import *

MT5Initialize()
MT5WaitForTerminal()

print(MT5TerminalInfo())
print(MT5Version())

ticks1 = MT5CopyTicksFrom( "EURAUD" , datetime( 2019 , 1 , 28 , 13 ), 10000 , MT5_COPY_TICKS_ALL)
ticks2 = MT5CopyTicksRange( "AUDUSD" , datetime( 2019 , 1 , 27 , 13 ), datetime( 2019 , 1 , 28 , 13 , 1 ), MT5_COPY_TICKS_ALL)

rates1 = MT5CopyRatesFrom( "EURUSD" , MT5_TIMEFRAME_M1, datetime( 2019 , 1 , 28 , 13 ), 1000 )
rates2 = MT5CopyRatesFromPos( "EURRUB" , MT5_TIMEFRAME_M1, 0 , 1000 )
rates3 = MT5CopyRatesRange( "EURPLN" , MT5_TIMEFRAME_M1, datetime( 2019 , 1 , 27 , 13 ), datetime( 2019 , 1 , 28 , 13 ))

MT5Shutdown()

#DATA
print( 'ticks1(' , len(ticks1), ')' )
for val in ticks1[: 10 ]: print(val)
print( 'ticks2(' , len(ticks2), ')' )
for val in ticks2[: 10 ]: print(val)
print( 'rates1(' , len(rates1), ')' )
for val in rates1[: 10 ]: print(val)
print( 'rates2(' , len(rates2), ')' )
for val in rates2[: 10 ]: print(val)
print( 'rates3(' , len(rates3), ')' )
for val in rates3[: 10 ]: print(val)

#PLOTTING
x_time = [x.time for x in rates2]
y_open = [y.open for y in rates2]
y_close = [y.close for y in rates2]

import matplotlib.pyplot as plt

plt.plot(x_time, y_open, 'g-' )
plt.plot(x_time, y_close, 'r-' )

plt.show()




 

I can't install this package in mac OS , What is the solution? 

 

I can't install this package in Ubuntu Devian either…

Yes their is a module in python



pip install MetaTrader5





 

how to get a call order or put an indicator in meta trader 5 with python code?

Thank you

Hello Rully,



https://www.mql5.com/en/docs/integration/python_metatrader5/mt5ordersend_py



You can use the above to place an order and to calculate indicator you can use using normal python libraries like TA-lib. You can generate a signal and then place order using this.