How to control MT4 from python?

12,391

Solution 1

I think you can try MetaApi https://metaapi.cloud cloud service which provides REST API and WebSocket API access to both MetaTrader 4 and MetaTrader 5 accounts.

Official REST API documentation: https://metaapi.cloud/docs/client

SDKs: https://metaapi.cloud/sdks (javascript, python and Java SDKs are provided as per April 2021)

Python SDK: https://pypi.org/project/metaapi-cloud-sdk/

It supports reading account information, positions, orders, trade history, receiving quotes, and accessing market data.

The service also provides copy trading API https://metaapi.cloud/docs/copyfactory and API to calculate forex trading metrics on a MetaTrader account https://metaapi.cloud/docs/metastats.

Solution 2

As of to-date, I recommend https://github.com/darwinex/dwxconnect, it enables anyone to write trading strategies in any programming language and trade them via MetaTrader 4 or 5 without having to code algorithms in MQL.

The only thing I found missing in the above recommendation was ability to get custom indicator data from MT4/5 to the preferred programming language. I did find https://github.com/khramkov/MQL5-JSON-API which allowed this. However MQL5-JSON-API was a bit complex for my taste or rather required a lot more effort to implement. How this helps

Solution 3

Several options:

  • exchange with files (write data from mt4 into a file for python, another folder in opposite direction with buy/sell instructions);
  • 0MQ (or something like that) as a better option.

Solution 4

There are several ways to do that but the best is to use WebSocket. as WebSocket provides two-way communication, it will be easy to fetch data using MT4 EA and send them to the WebSocket. You can record every Tick from EA and send it to WebSocket. The WebSocket should be opened by your Python script. You will be able to command the EA from python script as well. There will be a bi-directional data stream from EA to Python. There is a WebSocket library available for MQL you can read the MT4 WebSocket Library post and for python, you can just go through the Python WebSocket from RedHat.

Share:
12,391

Related videos on Youtube

Dragon.M
Author by

Dragon.M

Updated on June 14, 2022

Comments

  • Dragon.M
    Dragon.M about 2 years

    I'm using a MetaTrader4 Terminal and I'm experienced python developer.

    Does anyone know, how can I connect MT4 and Python?
    I want to:
    - connect to MT4
    - read USD/EUR data
    - make order (buy/sell)

    Does anyone know some library, a page with instructions or a documentation or have at least idea how to do that?

    I googled first 30 page but I didn't find anything useful.