MQL4 Order Counting Functions

It is useful to discover how many orders our EA has open and what type these open orders are, such as a buy or sell. It is thus useful to create a order counting function that can count the current number of open orders based on the order type. Learn how in this MQL4 Order Counting Function article.

MT4 Snippet

int OrdersTotalMagicOpen() {
int OrderCount = 0;
for (int l_pos_4 = OrdersTotal() – 1; l_pos_4 >= 0; l_pos_4–) {
OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
if (OrderType() == OP_SELL || OrderType() == OP_BUY) OrderCount++;
}
return (OrderCount);
}

Explanation

We have named our order counting function OrdersTotalMagicOpen(). It will return an integer value of how many orders are currently opened on the specified chart symbol matching the magic number that we have passed as a function argument.

We start by declaring the OrderCount variable, the intial value of which is 0. We use the for operator to loop through the block of code, and the OrderSelect() function to examine the pool of currently open positions, checking for ones that match our order symbol and magic number.

If OrderMagicNumber() matches our MagicNumber, we can be confident that this order was placed by our EA. Note: uncertainty about EA identification would only arise if the user is running two EA’s on the same currency symbol with the same magic number.

If OrderMagicNumber() does not match, we use the != sign, which means “not equals”, the program can continue, that is, it discards unaffiliated trades and moves on to trades that match up.

We next see if the order matches the order types OP_BUY or (||) OP_SELL. OP_BUY is a constant that indicates a buy market order. To count other types, simply replace OP_BUY or OP_SELL with the appropriate order type constant.

If the order matches both our magic number and chart symbol and order type, the value of the OrderCount will be incremented by one. After we have looped through all the orders in the order pool, we return the value of OrderCount to the calling function.

MT4 Usage

if (OrdersTotalMagicOpen() > 0 && CloseOrders == true){ // Close all orders }

If there are orders opened by this EA, and the value of CloseOrders is true, then the code inside the braces will run, which will close all the open orders.

Counting only Buy or Sell Orders

Sometimes it is necessary to just count the open buy orders or open sell orders, in which case we would have to split out the above function into two.

Counting Buy Orders

int BuyTotalMagicOpen() {
int OrderCount = 0;
for (int l_pos_4 = OrdersTotal() – 1; l_pos_4 >= 0; l_pos_4–) {
OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
if (OrderType() == OP_BUY) OrderCount++;
}
return (OrderCount);
}

 

Counting Sell Orders

int SellTotalMagicOpen() {
int OrderCount = 0;
for (int l_pos_4 = OrdersTotal() – 1; l_pos_4 >= 0; l_pos_4–) {
OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
if (OrderType() == OP_SELL) OrderCount++;
}
return (OrderCount);
}

You can see that the above two pieces of code are almost identical to what we had before, except that we have replaced the condition that checks for the order type being a OP_BUY or (||) OP_SELL with the the condition that checks for just the one type.

It is pretty easy to create a order counting function for every order type, replacing the constant OP_BUY for OP_BUYLIMIT or OP_BUYSTOP, for instance, and renaming the functions to differentiate the order types.