PositionsTotal() not OrdersTotal() - page 2

 
Konstantin Nikitin:
In MQL4 only OrdersTotal
And check in search

I remember when MT used to evaluate all statements in the expression regardless of truthiness (pre build 600) and you had to use continue, however, that's no longer the case. Now, the expression stops being evaluated after the first false condition so if you want to flatten your code block you can do it like this, omitting the continue statements. 

for(int i=OrdersTotal()-1; i>=0; i--)
{
    if(OrderSelect(i,SELECT_BY_POS)
       && (OrderType() == OP_BUY || OrderType() == OP_SELL){
           /* work */
    }
}

Since OP_BUY == 0 and OP_SELL == 1 you can achieve a slightly more perfomant loop by simply comparing order type < 2

for(int i=OrdersTotal()-1; i>=0; i--)
    if(OrderSelect(i,SELECT_BY_POS) && OrderType()<2)

... and it should go without saying that if you want to count the positions then you'd make your own function. 

int PositionsTotal()
{
    int count = 0;
    for(int i=OrdersTotal()-1; i>=0; i--)
        if(OrderSelect(i,SELECT_BY_POS) && OrderType()<2)
            count++;
    return count;
}
 
  1. nicholi shen: , however, that's no longer the case.
    That is correct. Build 600 and Higher 17.02.2014

  2. if(OrderSelect(i,SELECT_BY_POS) && OrderType()<2)
    Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles