Your opinion on this small code please

 

Hello,

Can you tell me your opinion on this piece of code please.

Its goal is to count the total number of active orders (op_buy and op_sell only, no pending) in progress on several EAs.

Thanks, Emmanuel.

   Total_Orders = OrdersTotal();
   Compteur = 0;

   for(Position_Index = Total_Orders - 1; Position_Index >= 0 ; Position_Index --)
      {
      if ( ! OrderSelect(Position_Index, SELECT_BY_POS, MODE_TRADES)) continue;
   
      if (
         ((OrderType() == OP_BUY) || (OrderType() == OP_SELL)) &&
         ((OrderMagicNumber()==Magic_Number) || (OrderMagicNumber()==Other_EA_Magic_Number_1) || (OrderMagicNumber()==Other_EA_Magic_Number_2))
         )
         {
         Compteur = Compteur + 1;
         break;
         }     
      }




 
arsouille:

Hello,

Can you tell me your opinion on this piece of code please.

Its goal is to count the total number of active orders (op_buy and op_sell only, no pending) in progress on several EAs.

If you want to count them all and have the count stored in Compteur   you will need to remove the   break;  otherwise when the first one is found the break will . . . .  " terminates the execution of the nearest nested outward switchwhile, or for operator. "  and your for loop will end.

You have additional sets of ( )  that you don't need,  but they won't hurt. 
 
arsouille:

Hello,

Can you tell me your opinion on this piece of code please.

Also you could think about to have the code block in a function and passing the magic numbers within an array to it. You could fill the array with external values (magic numbers) and you would not need to change the code block(s) if the number of controled magic numbers do change.

 

Thank you,

RaptorUK, you do not count your answers, and our ignorance does not tire, a big thank you to you

 
Your code
for(Position_Index = Total_Orders - 1; Position_Index >= 0 ; Position_Index --)
    {
    if ( ! OrderSelect(Position_Index, SELECT_BY_POS, MODE_TRADES)) continue;
   
    if (
       ((OrderType() == OP_BUY) || (OrderType() == OP_SELL)) &&
       ((OrderMagicNumber()==Magic_Number) || (OrderMagicNumber()==Other_EA_Magic_Num...)
       )
       {
       Compteur = Compteur + 1;
       break;
       }     
    }
Easier to Read
for(Position_Index = Total_Orders - 1; Position_Index >= 0 ; Position_Index --) if(
   OrderSelect(Position_Index, SELECT_BY_POS, MODE_TRADES)
&&(    OrderType() == OP_BUY
   || (OrderType() == OP_SELL   // Or use OrderType() <= OP_SELL
  )
&&(    OrderMagicNumber()==Magic_Number
   || (OrderMagicNumber()==Other_EA_Magic_Number_1
   || (OrderMagicNumber()==Other_EA_Magic_Number_2
) ){
     Compteur = Compteur + 1;
}   // for
Reason: