MQL4 Close All Orders In 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. You might want to close all the open orders. In this article we will see the code MQL4 Close All Orders. A script that can close all the open orders automatically.

MQL4 Close All Orders

When programming 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 the 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 the exchange rates

These are only some situations that can push you to close all the open orders immediately. You can do that with some code if you want.

We already saw in another article how to use the OrderClose function, that can close an individual order. In this article we want to see how to use it to Close all the orders with a custom function.

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

MQL4 Close All Orders Function Logic

The logic of the function is:

  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

MQL4 Close All Orders Possible Improvements

The function is not very complex and it can even be improved with further 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

I used the above function in many of my Expert Advisors…

Delete Opposite Pending Orders Script

In this article we only managed open orders.

If you need help deleting opposite pending orders with a script please visit the page about Multiple and Opposite Pending Orders in MT4.

MQL4 Close All Orders

Close All Orders in MT4

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

MT4 does not allow to select all open orders and close them at once, unfortunately.

But there are some solutions. You can either code your own script or use a ready made tool.

The script shared below can be copied on your MT4 platform and will allow you to close all the orders with just a couple of clicks.

MQL4 Close All Orders Conclusion

With the help of the above code hopefully you can create your own script to close all the orders with your desired settings. MQL4 Close All Orders is surely useful in many situation to protect your investment.

If you want to just download the script, here we go

If you want to save hours of research and coding and you want to see some professional code check this out! You can use it to build your own EA!

We use cookies to ensure that we give you the best experience on our website.