How to close all position at the same time? in mt4.

 

Hello ,I want to find a way to close several opening positions at the same time, as soon as possible .I have some code below...

 

for(int i=0;i<total;i++)
        {
         if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
            continue;

         if(OrderSymbol()==Symbol()
            //&& OrderMagicNumber()==InpMagicNumber 
            && OrderType()<=OP_SELL)
           {
            if(OrderType()==OP_BUY)
              {
               bool r=OrderClose(OrderTicket(),OrderLots(),Bid,3,clrWhiteSmoke);
               if(!r)
                 {
                  Print("OrderClose ERROR!:",GetLastError());
                 }
              }
            if(OrderType()==OP_SELL)
              {
               bool r=OrderClose(OrderTicket(),OrderLots(),Ask,3,clrWhiteSmoke);
               if(!r)
                 {
                  Print("OrderClose ERROR!:",GetLastError());
                 }
              }
           }
        }

but ... In Strategy Tester ,The Orders do not close at the same time. They even close in different bars.

Is there something like asynchronous mode ?There is OrderSendAsync() in mql5 ,but not in mql4.

somebody help me please~ 

 
Actually closing orders are based on ticket numbers. It will be handled one by one by the broker.
 

You should count down in your loop when closing orders.

When you close one order eg. position 1, the order at position 2 now moves to position 1, so the next cycle checks order 2 which was order3 and misses the order that was at position 2

 
GumRai:

You should count down in your loop when closing orders.

When you close one order eg. position 1, the order at position 2 now moves to position 1, so the next cycle checks order 2 which was order3 and misses the order that was at position 2


Ok , Let me try.
 
xhxiang:

Hello ,I want to find a way to close several opening positions at the same time, as soon as possible .I have some code below...

 

but ... In Strategy Tester ,The Orders do not close at the same time. They even close in different bars.

Is there something like asynchronous mode ?There is OrderSendAsync() in mql5 ,but not in mql4.

somebody help me please~ 

as GumRai says,the for clause will cause serious issue.

potential issue one:the EA don't have the chance to close the closing failed orders again

potential issue two:the EA don't handle the pending orders

 the following is part of my EA,FYI

//+------------------------------------------------------------------+
//| Close all orders                                                 |
//+------------------------------------------------------------------+
void CloseAll()
{
    int total = OrdersTotal();
    bool result = false;
    int errno;
    int closednum = 0;
    while(OrdersTotal() > 0)
    {
        if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)==false) break;
        if(OrderSymbol() == Symbol())
         {
             if(OP_BUY == OrderType())
             {
                 result = OrderClose(OrderTicket(), OrderLots(), Bid, slippage, CLR_NONE);
             }else if(OP_SELL == OrderType())
             {
                 result = OrderClose(OrderTicket(), OrderLots(), Ask, slippage, CLR_NONE);
             }else//pending order...
             {
                 result = OrderDelete(OrderTicket());
             }
             if(true != result)
             {
                 errno = GetLastError();
                 Print("Close err:",errno);
                 Sleep(10);
             }else
             {
                 closednum++;
             }
         }
    }
}
 

@ Moggy


Your EA is a bit outdated because there are better ways to close orders even more faster. Code it right and it can close 3 orders per second (subject to broker speed as well).

 
for(int x=OrdersTotal();x>=0;x--)
{
   if(OrderSelect(x,SELECT_BY_POS)==true)
   {
      if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrNONE))
      {
         Print("Close Order");
      }
      else
      {
         Print("Error on closing");
      }
   }
} 
 
deysmacro:

@ Moggy


Your EA is a bit outdated because there are better ways to close orders even more faster. Code it right and it can close 3 orders per second (subject to broker speed as well).


thanks for your information.

 it's exciting function.would you please give some examples?

thanks again 

 
Already answered by someone else.