Counting orders in the order board

Every week I tell three to five people to look at the code of any of my indicators to find the countOrders function, which allows to check the number of open orders quickly and easily – it’s high time to put this function into code repository on this blog. But first of all, I need to say why you should use my function instead of OrdersTotal().

The problem with OrdersTotal is that it doesn’t offer any order filtering, but only returns the number of orders in the orders board, what forces you to check the type and magic number of an order. countOrders solves this problem elegantly and efficiently allowing for a flexible choice of orders that you want to count.

int countOrders(int oMagic,int oType) {

   int count=0;

   for(int i=0;i<OrdersTotal();i++) {
    if(OrderSelect(i,SELECT_BY_POS)) {
     if(OrderMagicNumber()==oMagic) {
      if(OrderSymbol()==Symbol()) {
       if(OrderType()==oType || oType<0) {



The function takes two arguments – oMagic, which is a magic number of orders that we want to count and oType, which is a MQL constant for order type (OP_BUT, OP_SELL, …). To count all orders with the given magic type you need to pass negative value of oType argument.

Share with your friends:

Send us your comments

Name (required)

Email (required)


  • Facebook