For OrderSelect Loop and Error Correcting

 

I have a question regarding the below code.  It is meant to search the open orders and count how many open buy and sells their are for the correct symbol.  In this case there should only be 1 buy order open.  However, is it possible that if the OrderSelect fails to select the order at say i=0 it will then continue the "for" loop for the next iteration, i=1, having not counted the order at i=0?  Therefore it is possible that the wrong number of trades will be counted.  I guess you could insert an additional while (true) loop with Fun_Error function, but just wondering if that is unnecessary?

         Buys_Open = 0;                                                                   // Reset amount of buy orders
         Sells_Open = 0;                                                                  // Reset amount of sell orders
         
         for(int i=0; i <= OrdersTotal(); i++)                                            // Loop through orders
            {
            if(OrderSelect(i,SELECT_BY_POS) == true)                                      // If there is the next one
               {                                                                          // Analysing orders
               if(OrderSymbol() != Symbol())
                  continue;
               if(OrderType() > 1)                                                        // Pending order found 
                  {
                  Alert("Pending order detected. EA doesn't work.");
                  return;                                                                 // Exit start
                  }
               if(OrderType() == 0)                                                       // Buy order found
                  {
                  Buys_Open++;                                                            // Counter of buy orders
                  if (Buys_Open>1)                                                        // Check number of buy orders open
                     {
                     Alert("More than 1 Buy Order open.");
                     return;                                                              // Exit start
                     }
                  }
               if(OrderType() == 1)                                                       // Sell order found
                  (
                  Sells_Open++;                                                           // Counter of sell orders
                  if(Sells_Open>0)                                                        // Check number of sell orders
                     {
                     Alert("Sell Order detected.")
                     return;                                                              // Exit start
                     }
                  }
               }
            }
 
 
  1. for(int i=0; i <= OrdersTotal(); i++){
       if(OrderSelect(i,SELECT_BY_POS) == true) 
    If there are five orders, their position is zero to four, it will fail on position five.
  2. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool).
  3. In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
  4. if(OrderSymbol() != Symbol()) continue;
    No Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including manual trading.) Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
  5. if(OrderType() > 1)  
    Don't hard code numbers. Use the Order Properties - Trade Constants - Standard Constants, Enumerations and Structures - MQL4 Reference
  6. LifeHack:   However, is it possible that if the OrderSelect fails to select the order at say i=0 it will then continue the "for" loop for the next iteration, i=1, having not counted the order at i=0?  Therefore it is possible that the wrong number of trades will be counted.  I guess you could insert an additional while (true) loop with Fun_Error function, but just wondering if that is unnecessary?
    It will fail at position zero only if you have no open orders. You are over thinking it.
Reason: