Close Orders with MQL4 Using OrderClose

OrderClose() is a function in the MQL4 language used to Close Orders, in this article you can see how it works.

In previous articles we saw how to open new market orders and how to scan the account for open orders, to close the cycle we also need to learn how to close orders with the function OrderClose() .

At this point the order could be a winner, loser or breakeven but we only want to see how to close the order using MQL4 code.

MQL4 OrderClose() Function

OrderClose()  is the MQL4 function that allows you to close open market orders entirely or partially. This function requires a few parameters, we can see them in the following specification.

The parameters expected are (bold the required):

  • ticket to specify the ticket number, to identify which order to close
  • lots is the size in lots to close, example if order 1234 is a BUY order or 1 lot of EURUSD you can close the entire position specifying 1 lot or only close it partially, for example 0.6 lots, the remaining 0.4 will remain open
  • price is the close price, must be the current price or very close to it
  • slippage is the allowed difference between the requested close price and the one actually on the broker’s side
  • arrow_color is the color of the arrow in the chart

Close Orders Using MQL4 OrderClose

We can apply what we learned in how to Open Orders and Scan open Orders to close orders.

Usually orders are closed manually or during the process of Order Management of an EA.

In our example we will scan for order and close all of them, with some more advanced coding you will be able to become more selective and choose what orders to close but for the moment we will keep it simple.

Most of the information necessary to close an order can be retrieved easily selecting the order, this is why the scan of the orders is so useful.

When you use the OrderSelect()  function many details about the order can be retrieved with native MQL4 functions, in particular:

  • OrderTicket()  returns the ticket number of the order
  • OrderLots()  returns the position size of the order

The above are the strictly necessary however there are more functions to retrieve even more details about an order, we will see them in some other article.

The function CloseOrder()  is of type boolean and returns true if the order is successfully closed, false if the order is not closed for some error.

Close Orders Using MQL4 OrderClose Example

We can see an example of script that closes all the open market orders

If we run the script in any chart it will close all the orders, not limited to the currency pair in the chart


Applying more filters we could restrict the selection of the orders and close only specific ones.

With this article you learned how to close an order with MQL4 language, do some experiment and test it yourself.

I remember when I first started to learn MQL coding, although I had some knowledge of coding many concept were still unclear.

To speed up your learning process I would recommend to follow a quick MQL course that will give you the fundamentals you need to create functional Trading Robot.

You can check out the following two course, Udemy often has sales on to make the courses even more affordable.

MQL4 Programming for Traders: Build Robust Trading Robots!

Black Algo Trading: Build Your Trading Robot

Please leave a comment if you have any feedback or doubt and remember to like us on Social Media!

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!

Comments are closed.

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