Download MetaTrader 5
To add comments, please log in or register
Unable to find the answer to your question? Use the search!
haoxiang xie
996
haoxiang xie 2014.08.14 01:56 

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~ 

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.08.14 02:08  
Actually closing orders are based on ticket numbers. It will be handled one by one by the broker.
Keith Watford
Moderator
7888
Keith Watford 2014.08.14 02:56  

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

haoxiang xie
996
haoxiang xie 2014.08.14 03:33  
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.
Hongliang Liu
1195
Hongliang Liu 2014.08.14 06:00  
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++;
             }
         }
    }
}
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.08.14 06:07  

@ 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).

raven.chrono
324
raven.chrono 2014.08.14 06:20  
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");
      }
   }
} 
Hongliang Liu
1195
Hongliang Liu 2014.08.15 01:07  
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 

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.08.15 01:27  
Already answered by someone else.
/
To add comments, please log in or register