Order closing sequence is completing properly

 

Hi guys. I'm having a problem closing all of my orders at one time. I am opening up a position that is either long EU and short GU or short EU and long GU when there is a pip difference at different level. I am opening up to 12 order total, 6 each. When the pais converge I want all of these orders to close. This only happens sometimes. Other times I am left with 2 orders of GU still open. The if(result = 0)..Alert("problem closing orders: ",GetLastError()); does not come up however. I don't understand why it only works sometimes.

int start() {
//--- order closing script 
   int result = 0;                     
   if(total >= 1)
   {
      int orderIndicator = -1;    // denotes position in order pool of mainPair
      do
      {
         orderIndicator++;
         OrderSelect(orderIndicator,SELECT_BY_POS);
      }while(OrderSymbol() != mainPair);
      OrderSelect(orderIndicator,SELECT_BY_POS);
      if(diffLevel <= 0 && OrderSymbol() == Symbol() && OrderType() == OP_BUY)
      {
         
         for(int i = 0; i < OrdersTotal(); i++)
         {
            OrderSelect(i, SELECT_BY_POS);
            if(OrderSymbol() == subSymbol)
               result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(subSymbol,MODE_ASK),3,White);
            else if(OrderSymbol() == mainPair)
               result = OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
            if(result == 0)
               Alert("problem closing orders: ", GetLastError());
         }
      }
      else if(diffLevel >= 0 && OrderSymbol() == Symbol() && OrderType() == OP_SELL)
      {
        for(int i = 0; i < OrdersTotal(); i++)
         {
            OrderSelect(i, SELECT_BY_POS);
            if(OrderSymbol() == subSymbol)
               result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(subSymbol,MODE_BID),3,White);
            else if(OrderSymbol() == mainPair)
               result = OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
            if(result == 0)
               Alert("problem closing orders: ", GetLastError());
         }
      }
   }
   return;
}
 
1468714724: I'm having a problem closing all of my orders at one time.
  1. You must count down Loops and Closing or Deleting Orders - MQL4 forum
  2. You must check OrderSelect What are Function return values ? How do I use them ? - MQL4 forum
Reason: