Close All Orders for MT4

Orders management is a fundamental task when working with trading tools and expert advisors. In some cases, especially when there is a trigger from some risk management rule, you might want to close all open orders. Here, we will see the MQL4 code for doing that and you can also download a free Close All Orders script for MT4 — a script that can close all open orders automatically.

MQL4 Close All Orders

When coding an expert advisor, we should all implement safety measures to protect us from big losses. Risk management is a fundamental pillar of an EA. It is necessary to set rules, so that your EA can close all orders if some condition is met.

Some examples that could trigger the closure of all orders can be:

  • A group of orders hit the take-profit.
  • The cumulative loss of open orders causes the equity or the margin to go under a specific threshold.
  • There is a scheduled report coming out that could heavily impact prices.
  • An unexpected event is causing sudden changes in exchange rates.

These are only some situations that can push you to close all open orders immediately. You achieve that with a rather simple piece of MQL4 code.

You have probably already read our guide on OrderClose function, which can close an individual order. Below we want to show you how to use it to close all orders with a custom function.

The following MQL4 Close All Orders function can close all the open orders in the same run.

void CloseOrders()
   // Update the exchange rates before closing the orders.
   // Log in the terminal the total of orders, current and past.
   // Start a loop to scan all the orders.
   // The loop starts from the last order, proceeding backwards; Otherwise it would skip some orders.
   for (int i = (OrdersTotal() - 1); i >= 0; i--)
      // If the order cannot be selected, throw and log an error.
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
         Print("ERROR - Unable to select the order - ", GetLastError());

      // Create the required variables.
      // Result variable - to check if the operation is successful or not.
      bool res = false;
      // Allowed Slippage - the difference between current price and close price.
      int Slippage = 0;
      // Bid and Ask prices for the instrument of the order.
      double BidPrice = MarketInfo(OrderSymbol(), MODE_BID);
      double AskPrice = MarketInfo(OrderSymbol(), MODE_ASK);

      // Closing the order using the correct price depending on the type of order.
      if (OrderType() == OP_BUY)
         res = OrderClose(OrderTicket(), OrderLots(), BidPrice, Slippage);
      else if (OrderType() == OP_SELL)
         res = OrderClose(OrderTicket(), OrderLots(), AskPrice, Slippage);
      // If there was an error, log it.
      if (res == false) Print("ERROR - Unable to close the order - ", OrderTicket(), " - ", GetLastError());

MQL4 Close All Orders Function Logic

The logic of the function is the following:

  1. Update the prices.
  2. Using a loop, scan all the orders.
  3. If the order is open, then get the details.
  4. Close the order using the correct price.
  5. Check if the operation was successful for each order and, in case it wasn't, return an error.

Possible Improvements

The function is not very complex and it can be improved with additional filters. For example, you might want to apply some of the following filters:

  • Close only orders in profit.
  • Close only orders in loss.
  • Close only orders for a specific pair.
  • Close only orders with a specific magic number.
  • Other filters.

The above function is used many of our expert advisors.

Close All Orders Script

If you want to close all orders in MT4, you need to do it manually, unless you use an external tool.

Unfortunately, MT4 does not allow selecting all open orders and closing them all at once.

But there is a solution! You can either code your own script using the function above as a template or you can use a ready-made tool.

The script provided below can be copied to your MT4 platform and will let you to close all orders with just a couple of clicks.

Download MT4 Close All Orders Script

➥ Download Close All Orders

MT4 Close All Orders Script Installation

To install the script, please follow the instructions below:

  1. Download the script archive file.
  2. Open the MetaTrader 4 data folder (via File→Open Data Folder).
  3. Open the MQL4 Folder.
  4. Copy all the folders from the archive directly to the MQL4 folder.
  5. Restart MetaTrader 4 or refresh the list of scripts by right-clicking the Navigator subwindow of the platform and choosing Refresh.

You can also read a more detailed instruction on how to perform the installation.

Make sure you enable Allow live trading in the Common tab when executing this script and also in the platform's menu Tools→Options→Expert Advisors. Otherwise, it won't be able to close any orders.

How to Enable Live Trading for Scripts in MT4?


The code provided above explains how an ordering closing script works using a basic example. Our free Close All Orders script for MT4 provides a ready-made tool for filtered termination of open orders as well as a more advanced example of MQL4 source code for studying, modification, upgrade, and use in other programs. With it, you can easily create your own script to close orders in MetaTrader using custom conditions and filters.

© 2005–2023

Design — Mart Studio

Forex trading bears intrinsic risks of loss. You must understand that Forex trading, while potentially profitable, can make you lose your money. Never trade with the money that you cannot afford to lose! Trading with leverage can wipe your account even faster.

CFDs are leveraged products and as such loses may be more than the initial invested capital. Trading in CFDs carry a high level of risk thus may not be appropriate for all investors.