How can I count the orders already on the account at start ?

 

Im a newbie and Im really stuck, I had tried many ways and combined with my programming inexperience, I just cant find a solution to this problem.

I want the EA to count how many open and pending orders of each type are on the account, and store the result for each type in a variable. I have this code

really I am asking how to do it with SWITCH. I can achieve it with lots of nested "if" but if SWITCH could be cleaner code, that would be great :-)

thankyou

int total = OrdersTotal();
  for(int x=total-1; x>=0; x-- )
  {
    OrderSelect(x, SELECT_BY_POS);
      int type   = OrderType();
      int Op_Buy = 0;
      int Op_Sell = 0;
      bool result = false;
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = Op_Buy = Op_Buy + 1 ;      break;
      
      //Close opened short positions
      case OP_SELL      : result = Op_Sell = Op_Sell + 1 ;      break;

      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
    if(result == false)
    {
      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
 
MickGlancy:
I want the EA to count how many open and pending orders of each type
#define OP_COUNT OP_SELLSTOP+1
int count[OP_COUNT]; ArrayInitialize(count,0);
    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol() ){              // and my pair.
        count[OrderType()]++;
    }
Print("I have ",count[OP_BUY]," buys");
 
WHRoeder:


thank you very much WHRoeder

what does [ #define OP_COUNT OP_SELLSTOP+1 ] mean ?

Reason: