close all function problem

 

i have an hedging and grid EA, i have a problem when closing order.

let say if i have 7 sell order and 3 buy order position, when close all function running sometimes it won't close all order.

so it will leave 4 orders open, 2 sell and 2 buy order position. the question is why it happened and why its not always happen ( sometimes ).

any body want to help me ???

check the code below

int closeall()
  {
    int tutup=0;
    for(int hitung=OrdersTotal()-1; hitung>=0; hitung--)
    {
        if(!OrderSelect(hitung, SELECT_BY_POS, MODE_TRADES)) break;
        if(OrderSymbol()!=Symbol()) continue;
        if(OrderMagicNumber()==123456)
        {
           if (ScanOpenTrades()>0) 
           if(OrderType()==OP_BUY)  
           {
              if(OrderClose(OrderTicket(), OrderLots(), Bid, 10, Green)) tutup++;
           }
           if(OrderType()==OP_SELL) 
           {
              if(OrderClose(OrderTicket(), OrderLots(), Ask, 10, Yellow)) tutup++;
           }
        }
    }
    return(tutup);
  }
 
if(OrderSymbol() != Symbol()) continue;
 
 for(int hitung=OrdersTotal()-1; hitung>=0; hitung--) if(
    OrderSelect(hitung, SELECT_BY_POS, MODE_TRADES)
&&  OrderSymbol()     == Symbol()
&&  OrderMagicNumber()== 123456){