How To Connect Python With MT4/MT5 Using ∅MQ?

If you are a forex trader than you must be trading on MT4. There are other trading platforms available too like the Trade Station, Ninja Trader, Think and Swim but forex traders seem to love MT4 and its new version MT5. MT4/MT5 are free to download and use. Today MT4 and MT5 can also be used to trade stocks, commodities, futures, indices and even cryptocurrencies. I am not a big fan of cryptocurrencies especially the Bitcoin. If you have been reading my blog posts, I had warned about the cryptocurrencies implosion last year. This year you can see it happening with your own eyes. Algorithmic trading is the future of trading. Did you read the post on a Trend Trading High Frequency Trading Strategy. Today more than 80% of the trades placed at NYSE are by algorithmic trading systems. You should learn algorithmic trading. Python is a modern general purpose object oriented programming language.

Python is easy to learn and get started. You can use it for learning machine learning, deep learning and reinforcement learning and then use that in developing your own algorithmic trading systems. I have developed a few python courses for traders that you can take a look at if you are interested. The first is the course for beginners who have never coded in their lives. Python for Traders course is for beginners. I take you by hand and show you how to start coding in Python. Once you develop proficiency with python, you can take the second course Python Machine Learning for Traders. After taking the course on machine learning, you can learn deep learning with the third course Python Deep Learning for Traders. Last course Algorithmic Trading with Python is how to develop algorithmic trading systems using python. In the last course I go in depth how to develop your algorithmic trading system and deploy them on MT4/MT5 using ZeroMQ messaging library. Below is a schematic of how you can use ZeroMQ to connect MT4/MT5 with Python and R.

ZMQ MT4/MT5 Bridge

Coming back to the topic of this post, we were talking about MT4 and MT5 platforms developed by MetaQuotes Corporation. MT4/MT5 are good trading platforms that provide you with streaming price data, a dozen of default technical indicators like the Moving Average, MACD, RSI, CCI, TEMA, TRIX etc that you can use and build your trading strategies. You can also code your own custom indicators and expert advisors using MQL4 and MQL5 programming languages. MQL4 and MQL5 have been modeled on the popular C++ object oriented programming language. But we have a problem! MQL4 and MQL5 are basic languages that lack the libraries that we can use to build powerful machine learning and artificial intelligence trading systems and expert advisors. Building these libraries is not easy as it requires a lot of experience and hard work. Did you read the post on how to use indicators in expert advisors?

Today Python is considered to be a leader when it comes to data science, machine learning, deep learning and reinforcement learning. Python is easy to learn and master. Developing algorithmic trading strategies in Python takes much less time as compared to C++/JAVA. Developing machine learning/deep learning libraries in MQL4/MQL5 is not an easy task. The best thing that we can do is to somehow find a method to connect MT4/MT5 with Python and then use the power of Python in developing algorithmic trading strategies. We can use sockets to do that. First we will need to write a Winsock DLL which is cumbersome process and requires a lot of experience.

I had always thought of developing a DLL that connects MT4/MT5 with Python/R/Java/C++/C#. Happy news! We can do this very easily with an open source library ZeroMQ. ZeroMQ is an open source messaging library which is blazingly fast. It has support for many programming languages. So you can use it to connect all sort of platforms. It uses TCP/IP protocol and sockets. Yes you need to learn a little bit of networking programming and you will be easily able to use ZeroMQ library. In this post my focus will be on connecting Python with MT4/MT5 and developing a simple python based algorithmic trading strategy in real time on MT4/MT5. In future, I will also show you how to connect C++/C#/Java/R with MT4/MT5. So for now let’s focus on Python and MT4/MT5 only. Read this post on why it is difficult to predict currency pair prices using statistical methods.

Why You Need Python For Algorithmic Trading

Python is a very powerful modern object oriented programming language. Python is in great demand due to the availability of modules like numpy, pandas, tensorflow, pymc3, keras etc. Tensorflow is a powerful machine learning and deep learning package that has been developed by Google. You should use tensorflow in developing algorithmic trading strategies. Numpy, Pandas and now Tensorflow these three libraries have made Python very powerful. Python has now become the language of Artificial Intelligence, Machine Learning and Deep Learning. If you are into these things, you should learn Python. The good thing about Python is that it is far easier to learn as compared to the standard languages C++ and Java. You have a trading strategy in your mind, you can easily code it in Python in a few hours and backtest it. In C++ and Java, it can take days and weeks. Today, Python is being extensively used by the quants at Wall Street, big banks and hedge funds in developing their algorithmic trading systems. Did you read the post on how to predict gold prices using Kernel Ridge Regression?

So Python has emerged as the best language for developing algorithmic trading strategies. If you don’t know it you should learn it. Now when you develop an algorithmic trading strategy, you need historical data to test it. You also need live data if you want to use it in live trading. By connecting MT4/MT5 with Python using the ZeroMQ library, we can achieve our purpose. We will transfer the market price data to python where our algorithmic trading strategy will be running. If we want to trade live, we can also do that. When we have a buy/sell signal, we can tell MT4/MT5 to open a trade or close a trade using ZeroMQ library. Good thing! Most of the hard work has already been done. We have the bindings on both sides. We just need to learn how to write an EA in MT4/MT5 and a program Python and connect them using ZeroMQ.

As I said in the beginning of this post, I have developed a few courses that you can take a look at if you want to learn Python. The first course is Python for Traders. This is for beginners those traders who have never coded in their lives. I take you by hand and teach you the basics of Python with special focus on Numpy, Pandas and Tensorflow. The next course is Python Machine Learning for Traders. After taking the first course Python on Traders, you can take this Python Machine Learning for Traders in which I show you how to develop regression and classification algorithmic trading models using Neural Networks, Support Vector Machines etc. In the third course Python Reinforcement Learning for Traders I show you how to develop trading models that depend on reward/risk. Reinforcement learning is the thing now a days. It is being heavily used in autonomous driving. We can use it in trading as well. The fourth course is Python Deep Learning for Traders and the last course is Python Algorithmic Trading for Traders. You have a full package. These courses will make you a master of algorithmic trading. Watch this 58 minute documentary on UK Billionaire Traders.

Sockets

Sockets are channels for exchanging information between two programs written in different programming language. These two programs can be running on the same machine or running on different machines. Sockets provide us the means using TCP/IP protocols to communicate between two or more programs. Sockets are very important in network programming. We can always connect MT4/MT5 with other programs using sockets. Interactive Brokers is a famous online broker that uses sockets in its API. However if we want to use sockets in MT4/MT5, first we will have to write a DLL in C/C++ using Winsock library. Over the last 10 years, MT4/MT5 platforms have matured enough and you can find a number of libraries available that can implement sockets with MT4/MT5. There are many brokers that are allowing stocks, futures, indexes, commodities and cryptocurrencies in addition to currencies. So you can use MT4/MT5 for trading hundreds of stocks, futures, commodities and cryptocurrencies as well as currency pairs. However as said MQL4/MQL5 are basic languages with almost no libraries that can do artificial intelligence/machine learning/deep learning/reinforcement learning. Developing algorithmic trading strategies using MQL4/MQL5 is not possible. This requires more functionalities in the platform which is not available in shape of libraries. Socket module in available in almost all important programming libraries like C/C++, Python, JAVA, PHP, Javascript, C# etc. We can use it to connect these powerful programming languages with MT4/MT5. Take a look at this Neural Network Forex Trading System.

ZeroMQ Messaging Library

Now let’s discuss what is ZeroMQ Messaging Library. You should have some idea about network programming before you will start understanding ZeroMQ (∅MQ) Messaging Library. If you don’t have, you can take a look at my course Algorithmic Trading with Python. So let’s start with the basics of network programming and in a few paragraphs you will have sufficient idea about it. You should be familiar with the idea of sockets and TCP/IP protocol. ∅MQ is a very popular library that you should master as it will allow you to use it with all sorts of software codes. You can use ∅MQ with R, C++, Java, C#, Python etc. So in essence knowing ∅MQ is something that will help you in your coding career apart from algorithmic trading. For example if you are a game developer apart from algorithmic trader, you can use use knowledge of ∅MQ in game development as well. ∅MQ provides us with the following methods to connect MT4/MT5 with Python:

1. PUSH/PULL

2.PUB/SUB (Publish/Subscribe is just like a radio broadcast. We can use MT4/MT5 as a server that publishes the market data and use Python as a client that subscribes to that market data).

3. REQ/REP (Request/Reply pattern allows the client to request the server to provide the requested information which can be in our case Python asking MT4/MT5 for market data/how many trades are open/equity in the account etc).

4. Pipeline

 

∅MQ MQL4/MQL5 Bindings

As said above ∅MQ uses sockets and has bindings for most programming languages. Most of the work has already been done for us. Developing MQL4/MQL5 binding for ∅MQ is the first thing that you need. Happily this has already been done for us. Go to this Github code depository and download the ∅MQ MQL4/MQL5 bindings. Ding Li is the creator of these ∅MQ MQL4/MQL5 bindings. We should thank Ding Li for solving a major problem that we could have faced on developing the ∅MQ MQL/MQL5 bindings ourselves. Header files is the same for MQL4 and MQL5.