Algorithmic Trading with Python and MT5: Working with Pending Orders
A lot of trading strategies don’t require instant execution by the market. Sometimes buy or sell an asset requires calculate a price level and then put the pending orther in the market .
For example, imagine a simple trading strategy that calculates the maximum and minimum of an asset of the last three trading days and then put buy signal on the minimum and sell signal on the maximum.
To do that we sould first get the last trhee day information of the asset. If you have read my previous post you should be able to do this easily. If you don’t here is the code.
Import pandas as pd
Import numpy as np
Import Metatrader5 as mt5
nombre = 187652387
clave = "mypassword"
path = r'C:\Program Files\MetaTrader 5\terminal64.exe'
mt5.initialize(login = nombre, password = clave, server = servidor, path = path)
rates = mt5.copy_rates_from_pos('EURUSD', mt5.TIMEFRAME_M5, 0, 864)
rates_frame = pd.DataFrame(rates)
rates_frame['time']=pd.to_datetime(rates_frame['time'], unit='s')
After get the data, we will then calculate the maximum and the minimum price of the last 864 five minutes candles (or last 3 day information) to calculate these two values.
max_value = rates_frame['high'].max() #The highest value reached by the price
min_value = rates_frame['low'].min() #The minimum value reached by the prices
With theese to values calculated, we can then send the pending orders. To do this, we will use the basic structure of an operation ( Read this post for more details) and will modify a little bit. First we will change the “action” element of the dictionary for mt5.TRADE_ACTION_PENDING, then we will change the type element with the with the desired operation. But before to show the code remember this graph:
After recall the types of pending orders we can translate them into Python:
- buy limit= mt5.ORDER_TYPE_BUY_LIMIT
- sell limit= mt5.ORDER_TYPE_SELL_LIMIT
- buy stop = mt5.ORDER_TYPE_BUY_STOP
- sell stop = mt5.ORDER_TYPE_SELL_STOP
With this small traduction, we can define a little mean reversion strategy (i will talk about this kind of strategy on future posts) that follows the rule we talk at the begining of the post.
request = {
"action":mt5.TRADE_ACTION_PENDING,
"symbol":'EURUSD',
"type" : mt5.ORDER_TYPE_BUY_LIMIT,
"price": min_value,
"volume":1.0,
"comment":'Test_code',
"type_filling":mt5.ORDER_FILLING_IOC
}
mt5.order_send(request)
request = {
"action":mt5.TRADE_ACTION_PENDING,
"symbol":'EURUSD',
"type" : mt5.ORDER_TYPE_SELL_LIMIT,
"price": max_value,
"volume":1.0,
"comment":'Test_code',
"type_filling":mt5.ORDER_FILLING_IOC
}
mt5.order_send(request)
Note that we replace the “price” element of the dictionary together with the min value, to create the buy pending order, and with the max value to create the the sell pending orther.
The full code:
Import pandas as pd
Import numpy as np
Import Metatrader5 as mt5
nombre = 187652387
clave = "mypassword"
path = r'C:\Program Files\MetaTrader 5\terminal64.exe'
mt5.initialize(login = nombre, password = clave, server = servidor, path = path)
rates = mt5.copy_rates_from_pos('EURUSD', mt5.TIMEFRAME_M5, 0, 864)
rates_frame = pd.DataFrame(rates)
rates_frame['time']=pd.to_datetime(rates_frame['time'], unit='s')
request = {
"action":mt5.TRADE_ACTION_PENDING,
"symbol":'EURUSD',
"type" : mt5.ORDER_TYPE_BUY_LIMIT,
"price": min_value,
"volume":1.0,
"comment":'Test_code',
"type_filling":mt5.ORDER_FILLING_IOC
}
mt5.order_send(request)
request = {
"action":mt5.TRADE_ACTION_PENDING,
"symbol":'EURUSD',
"type" : mt5.ORDER_TYPE_SELL_LIMIT,
"price": max_value,
"volume":1.0,
"comment":'Test_code',
"type_filling":mt5.ORDER_FILLING_IOC
}
mt5.order_send(request)
We have a lot to discuss about pending orders. But for now you can practice and adapt this simple code to your strategy!
Do you have a strategy and want to automatize it while learning how to become an algorithmic trader? Contact me on Instagram @elospi18