How to close multi-open-trades at one shot

 
we would like to hv somebody telling us on how to close several opened trades at one shot and not to close them individually or one by one.
 

Try this:

The attached script file should close all orders at same time. Place it in script folder and compile. Just drag to chart screen when needed. I haven't tested it. Hope it works.

Another way is to code it inside EA to CloseAllTrades when called.

if(x==?) CloseAllTrades(); // insert a call in int start() section of EA.
 
// insert this following code after 
// int start() {  
 
// } section of EA.
 
void CloseAllTrades() {
   for (int i=0; i<OrdersTotal(); i++) { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { 
         if (OrderSymbol()==Symbol()) { 
            if (OrderType()==OP_BUY) 
               OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet);
            if (OrderType()==OP_SELL) 
               OrderClose(OrderTicket(),OrderLots(),Ask,5,Violet);
            if (OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT || 
                OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT) 
               OrderDelete(OrderTicket());
         }
      }
   }
}
Files:
 
wackena:

Try this:

The attached script file should close all orders at same time. Place it in script folder and compile. Just drag to chart screen when needed. I haven't tested it. Hope it works.

Another way is to code it inside EA to CloseAllTrades when called.


Thanks! I gave that a try and it is still closing 1 by 1.
 

Unfortunately, that code is wrong. Use this

void CloseAllTrades() {
   for (int i=OrdersTotal()-1;i>=0; i--) { 
      if (OrderSelect(i, SELECT_BY_POS)) { 
         if (OrderSymbol()==Symbol()) { 
            if (OrderType()<=OP_SELL) 
               OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Violet);
            else   OrderDelete(OrderTicket());
         }
      }
   }
}
 
There's already another active thread right now asking basically the same question if you even bothered to look. https://www.mql5.com/en/forum/127997 Open multiple Meta-Traders otherwise, its NOT possible.
 
You MUST refreshRates between each server call.
 
WHRoeder:
You MUST refreshRates between each server call.

Not necessary.
 
Roger:

Unfortunately, that code is wrong. Use this

 


  Thanks, but it is the same. closing 1 by 1.
 
johnnybegoode:

  Thanks, but it is the same. closing 1 by 1. 


If you want to close all orders at all, try this

void CloseAllTrades() {
   for (int i=OrdersTotal()-1;i>=0; i--) { 
      if (OrderSelect(i, SELECT_BY_POS)) { 
            if (OrderType()<=OP_SELL) 
               OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Violet);
            else   OrderDelete(OrderTicket());
      }
   }
}
 

alternative close trade by...

may be usefull for you...

void closebuy()

{

int total=OrdersTotal(),i;

for(i=total-1; i>=0; i--)

{

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if ((OrderType()==OP_BUY) || OrderSymbol()==Symbol())

{

OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,CLR_NONE);

}

}

return(0);

}

void closesell()

{

int total=OrdersTotal(),i;

for(i=total-1; i>=0; i--)

{

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if ((OrderType()==OP_SELL) || OrderSymbol()==Symbol())

{

OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,CLR_NONE);

}

}

return(0);

}

 
Folks there is no capability either manually or programmatically to close all orders in an account. Each order must be selected and closed. CB
Reason: