
The combination of Python and MetaTrader 5 can be incredibly powerful for your trading bot.
Bringing together the world’s most popular programming language and one of the world’s largest retail trading platforms is a game changer. In fact, TradeOxy is built on leveraging these factors to help traders!
If you’re looking to integrate MetaTrader5 (MT5) and Python together into a trading bot, you’ll need a few functions:
How to initialize a symbol on MetaTrader5 (needed if you want to retrieve data or make orders)
In this article, I’ll show you how to cancel an open order (i.e. before it executes and becomes a position)
Join our amazing Discord community here.
I’ll assume you’ve connected to MT5.
To cancel an order on MT5, all you need is an order number. Check out my article “How to Get a List of Open Orders on MetaTrader 5 with Python” if you want to find out how to retrieve a list of order numbers.
With your order number in hand, here’s the code:
# Function to cancel an order
def cancel_order(order_number):
# Create the request
request = {
"action": MetaTrader5.TRADE_ACTION_REMOVE,
"order": order_number,
"comment": "Order Removed"
}
# Send order to MT5
order_result = MetaTrader5.order_send(request)
return order_result
No comments yet