Counting orders – version with references

Some time ago I was writing about counting orders in a seperate article entitled “Counting orders in the order board”. I would like to return to this topic in context of references, which I described in a separate entry.

The code from the previous article is correct and works as intended. But in case we want to count the exact number of orders of each type in the order board, the use of the previous code will make our code less efficient. This problem can be solved using the following function:

int countOrdersByRef(int oMagic, int& all, int& buy, int& sell,
                                           int& buystop, int& sellstop,
                                           int& buylimit, int& selllimit) {

   all=0;
   buy=0;
   sell=0;
   buystop=0;
   sellstop=0;
   buylimit=0;
   selllimit=0;

   for(int i=0;i<OrdersTotal();i++) {
    if(OrderSelect(i,SELECT_BY_POS)) {
     if(OrderMagicNumber()==oMagic) {
      if(OrderSymbol()==Symbol()) {
       all++;
       if(OrderType()==OP_BUY) buy++;
       if(OrderType()==OP_SELL) sell++;
       if(OrderType()==OP_BUYSTOP) buystop++;
       if(OrderType()==OP_SELLSTOP) sellstop++;
       if(OrderType()==OP_BUYLIMIT) buylimit++;
       if(OrderType()==OP_SELLLIMIT) selllimit++;
      }
     }
    }
   }

}

We use it in this way:

int cAll;
int cBuy;
int cSell;
int cBuyStop;
int cSellStop;
int cBuyLimit;
int cSellLimit;

countOrdersByRef(magic,cAll,cBuy,cSell,cBuyStop,cSellStop,cBuyLimit,cSellLimit);

// after that we use these declared variables
if(cBuy>0.....) { .... }

A short explanation of the function body:

  • Zeroing is necessary, because passed variables may have non-zero values, so their incrementation would produce incorrect values, which would result in numbers that are different from the true board order state.
  • You may ask why we should use a function when we can put such loop directly into start function body. Of course, it can be done, but this way is more elegant and helps to reuse code in future projects – we just copy the entire function without playing with code fragments.

Share with your friends:

Send us your comments

Name (required)

Email (required)

Message

  • Facebook