How To Write a Close All Script in MQL4? (1 of 4)

If you're just wanting to get a ready-made Metatrader script that closes all trades per chart or for the whole trading account, then follow the links and download the MQ4 script file of your choice:

There are many ways to write a script that closes all orders in MQL4, just as there are many ways to accomplish virtually anything via code. However, if you've taken the time to look at the code for any of the various Close All routines that can be found on the various sites on the WWW, you will notice some striking similarities. Before you can close all orders, you must first learn how to iterate through all the open orders and learn something about how MT4 (Metatrader 4) handles orders.

MQL4 has a simple way to iterate through all open orders for a given account by using two simple functions. The OrdersTotal() function help file states that it returns, "market and pending orders count." By orders, the creators of MT4 really mean orders such as stop orders, or trades such as buy or sell trades. The other little tidbit that the MT4 help file leaves out is that all orders and trades for the current account will be checked with this function. A simple loop to iterate through all orders and trades might look like the following:

for (int i = OrdersTotal(); i >=0; i--) {
    // Do something here
}

In the code above the (int)eger i is declared (int i = OrdersTotal();) or in other words i is set to the total open order count.. The line termination character in MQL4 is the semicolon. The loop continues while i >= 0 or in other words until no orders are left. When  OrdersTotal()  returns -1 there are no orders open. (This is an undocumented return value that you may want to make note of.) The integer i increments down by 1 each time (i--). This is the basics of a "for" loop. The variable names can change (i was used in this example) but "for" loops follow this basic pattern every time. Curly brackets start and end a block of code, in this case a for loop.

Next you need to select the orders by using the OrderSelect() function. This can be done as follows:

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

In the MQL4 code above, orders are selected one at a time, using i as the reference number. Orders are selected by position. The two ways to select an order are SELECT_BY_POS which will give the index of total open orders, starting at 1, and SELECT_BY_TICKET which will return the actual order ticket number. When SELECT_BY_POS is used, a optional pool index may be entered with the default being MODE_TRADES which returns order index numbers of open orders and trades, while MODE_HISTORY will return the index numbers of closed and cancelled orders.

If you wish to close all open orders, you should use MODE_TRADES, which will select trades by index number so SELECT_BY_POS is used for this example. Putting the code together, you have the following:

for (int i = OrdersTotal(); i >=0; i--) {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
}

You will learn how to close an order using OrderClose(), and how to use the OrderType() function to differentiate between buy and sell orders in MQL4.
For updates Follow patrickmwhite on Twitter
Comments