3 min read

How to Create a Stop Order on MetaTrader 5 with Python

Learn how to create a BUY STOP or SELL STOP order on MetaTrader 5 using Python
Cool little clockwork robot, using Sepia colors. Holds a red trading arrow.
Python Trading Bot

Python Trading Bot

For many years, algorithmic trading (which could lead to automated trading bots) faced high barriers to entry.

MetaTrader 4 used a C++ programming language derivative. Compute power was scarce. Access to advanced analysis was challenging — especially when the desire was to integrate multi-source signals (i.e. sentiment analysis).

All these factors meant automated trading bots were the purview of advanced programmers or corporations.

MetaTrader 5 (MT5) started to change this with their MetaTrader5 Python Library. Suddenly, a powerful Python 3 library was available for use. Combined with the power and reach of one of the world’s largest retail trading platforms, it represented a step change in access.


In this series, I’ve been sharing some of the basic functions you need to leverage this powerful platform. So far we’ve covered:

  1. How to connect to MetaTrader5
  2. How to initialize a symbol on MetaTrader5 (needed if you want to retrieve data or make orders)
  3. How to retrieve 50,000 candles

In this article, I’ll show you how to place a STOP order. This will include a BUY-STOP and SELL_STOP order.

P.S. To learn how to build a full MetaTrader 5 Python Trading Bot, check out my series “How to build a MetaTrader 5 Python Trading Bot” and Python Trading Bot source code on GitHub.
How to Build a MetaTrader 5 Python Trading Bot Expert Advisor
Learn how to build your Expert Advisor in Python using MetaTrader 5 in this series. You’ll get a step by step guide with incredible explanations to help you every step of the way.

Placing An Order

I’ve assumed for this article that you’ve already connected to MT5 and initialized the symbol you’d like to trade. The variables you’ll need are:

  1. Order Type (BUY_STOP or SELL_STOP)
  2. Symbol
  3. Volume (how much you’d like to trade)
  4. Price (how much you’ll pay for the volume you specify)
  5. Stop_Loss (the stop loss on the order)
  6. Take_Profit (the take profit on the order)
  7. Comment (any comments you’d like to make on the order)

Note. If the order is not successful, I’ve included a non-breaking error notification. This is to make your trading bot more resilient.

Here’s the code:

# Function to place a trade on MT5
def place_order(order_type, symbol, volume, price, stop_loss, take_profit, comment):
    # If order type SELL_STOP
    if order_type == "SELL_STOP":
        order_type = MetaTrader5.ORDER_TYPE_SELL_STOP
    elif order_type == "BUY_STOP":
        order_type = MetaTrader5.ORDER_TYPE_BUY_STOP
    # Create the request
    request = {
        "action": MetaTrader5.TRADE_ACTION_PENDING,
        "symbol": symbol,
        "volume": volume,
        "type": order_type,
        "price": round(price, 3),
        "sl": round(stop_loss, 3),
        "tp": round(take_profit, 3),
        "type_filling": MetaTrader5.ORDER_FILLING_RETURN,
        "type_time": MetaTrader5.ORDER_TIME_GTC,
        "comment": comment
    }
    # Send the order to MT5
    order_result = MetaTrader5.order_send(request)
    # Notify based on return outcomes
    if order_result[0] == 10009:
        print(f"Order for {symbol} successful")
    else:
        print(f"Error placing order. ErrorCode {order_result[0]}, Error Details: {order_result}")
    return order_result

Note how the price, stop_loss, and take_profit have all been rounded to 3 decimal places. This prevents invalid price errors due to decimal places.

Say Hi!

I love hearing from my readers, so feel free to reach out. It means a ton to me when you clap for my articles or drop a friendly comment — it helps me know that my content is helping.