Algorithmic Trading with Python and MT5: The Only MQL You Need to Know

Sebastian Ospina Valencia
3 min readSep 14, 2021

--

By now i think you already know me and you know that i don’t like MQL# to create trading robots. But some years ago i faced an ugly truth: I have to know something about MQL# to send my orders to MT5.

My first thought when i used tools like ZeroMQ or RabbitMQ to try to connect MT4 with Python was that i had to learn not only new highly and complicated architectures and Python functions but also have some intermediate MQL4 or MQL5 language. But when i find the mt5 library i realized, luky me, that i have to know only some basic and well documented MQL code to get the data from the desired time frame, select the order type and the needed filling order type.

The only MQL you need to know to get the data

As we seen in previous posts we used to get the data the next line of code:

rates = mt5.copy_rates_from_pos(symbol, mt5.TIMEFRAME_H1, 0, 1000)

The only MQL statement is TIMEFRAME_H1 that indicates that you will download the last 1000 candles of your desired symbol in 1 hour frame. In other words, download the last 1000 hours of price information from the symbol you want.

There are several options yo can use, for example to get the information from a 5 minutes frame you will use TIMEFRAME_M5, or if you want days you could use TIMEFRAME_D1. Another example is if you want a 2 hours timeframe TIMEFRAME_H2.

Select Order Type

Before start, we have to know the basic structure of a trading request from Python to MT5. The next structure is a full MQL language piece of code embedded in a Python dictionary:

request = {
“action”: mt5.TRADE_ACTION_DEAL,
“symbol”: symbol,
“volume”: lot,
“type”: mt5.ORDER_TYPE_BUY,
“sl”: price — 1.2,
“tp”: price + 3.6,
“deviation”: deviation,
“magic”: 202003,
“comment”: “InUpBot MrRedDragon”,
“type_time”: mt5.ORDER_TIME_GTC,
“type_filling”: mt5.ORDER_FILLING_FOK}
print(‘Se ejecutó una compra por 0.2’)
mt5.order_send(request)

So in trading you have only two types of possible operations: Sell or Buy. In order to sell, you will use, in the “type” field, ORDER_TYPE_SELL in contrast, if you want to buy you will use ORDER_TYPE_BUY. Easy and simple.

Also you can also send pending order like a buy/sell stop/limit using in this field ORDER_TYPE_BUY_LIMIT, ORDER_TYPE_BUY_STOP, ORDER_TYPE_SELL_LIMIT, ORDER_TYPE_SELL_STOP.

Select Action Type

Action type reffers to the option to make a market order or a pending order. When you want a market order you will use in the “action” field of the Python dictionary TRADE_ACTION_DEAL. In contrast if you want to send a pending order, you use TRADE_ACTION_PENDING.

Filling Type

The filling type reffers to how the broker will fill your order. WARNING! the filling type you will use depends on your broker policy and you will have to test before deploy your trading bot.

The filling types are:

ORDER_FILLING_FOK

This execution policy means that an order can be executed only in the specified volume. If the necessary amount of a financial instrument is currently unavailable in the market, the order will not be executed. The desired volume can be made up of several available offers.

ORDER_FILLING_IOC

An agreement to execute a deal at the maximum volume available in the market within the volume specified in the order. If the request cannot be filled completely, an order with the available volume will be executed, and the remaining volume will be canceled.

ORDER_FILLING_RETURN

This policy is used only for market (ORDER_TYPE_BUY and ORDER_TYPE_SELL), limit and stop limit orders (ORDER_TYPE_BUY_LIMIT, ORDER_TYPE_SELL_LIMIT, ORDER_TYPE_BUY_STOP_LIMIT and ORDER_TYPE_SELL_STOP_LIMIT) and only for the symbols with Market or Exchange execution modes. If filled partially, a market or limit order with the remaining volume is not canceled, and is processed further.

Further read:

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

--

--

Sebastian Ospina Valencia

Chief Developer Officer at InUp. Data Scientist with 5 years of experience in Banking. Teacher of Algorithmic Trading at Universidad del Rosario