Is it possible to close all 100 open positions in 1 sec, in one go?

 

This is the close all order script that I use

void CloseAll()
 {
 int ctotal = OrdersTotal(); 
 for(int j=ctotal-1;j>=0;j--)
 {
 OrderSelect(j, SELECT_BY_POS);
 int type = OrderType();
 bool result = false;
 switch(type)
 {
 //Close opened long positions
 case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
 break;

 //Close opened short positions
 case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );

 //Close pending orders
 case OP_BUYLIMIT :
 case OP_BUYSTOP :
 case OP_SELLLIMIT :
 case OP_SELLSTOP : result = OrderDelete( OrderTicket() );

 }
 if(result == false)
 {
 Print("Order ", OrderTicket(), " failed to close. Error:", GetLastError() );
 Sleep(3000);
 } 
 }
 return(0);
 }

 

The  problem is that if I opened many positions say 100, it will take ages to close all the orders 1 by 1. By the time it completed doing so, the market might had turned against me.

Is there anyway to close all opened positions at once.

 

On a side note, I just  learned today that you could open more than 1 order in a go, at the same time by adding "&"

int ticket=OrderSend(symbol1,OP_SELL,LotSize,MarketInfo(symbol1,MODE_BID),3,Ask+(Point*StopLoss),0,"My EA",12345,0,Red)
& OrderSend(symbol2,OP_BUY,LotSize,MarketInfo(symbol2,MODE_ASK),3,Bid-(Point*StopLoss),0,"My EA",12345,0,Green);

but still cannot get it to work with 2 different pairs.

error 4106: unknown symbol name, even if I test it on another chart other than symbol1 and 2

 

BIG THANK YOU in advance! 

 

hi, if all your 100 positions are in the same direction and on the same symbol there is a solution,

you have to open one position in the other direction where lotsize is the total amount of all other positions, then you can close all the order with the function OrderCloseBy() without risk.


//z

 
Please, format code properly regardless of useless.
 

You already have the loop.

Just integrate IF (OrderType() == Pair) OrderClose(ctotal.......);

After assigning var Pair and using ticket# instead of ctotal as should be.

It will then close one at each loop-run.

Hope I didn´t misunderstand something.

/ McKeen

 

Would it be ok to run the loop for closing orders first and then close out your pending orders after that's done? Might speed it up a bit if you give the switch block less cases to look through...

 
zzuegg:

hi, if all your 100 positions are in the same direction and on the same symbol there is a solution,

you have to open one position in the other direction where lotsize is the total amount of all other positions, then you can close all the order with the function OrderCloseBy() without risk.


//z

thanks!

  I have open orders in different direction but on the same symbol. I just need to close everything.

 
Rosh:
Please, format code properly regardless of useless.

thanks!
  don't understand?, I'm a beginner coder
 
McKeen:

You already have the loop.

Just integrate IF (OrderType() == Pair) OrderClose(ctotal.......);

After assigning var Pair and using ticket# instead of ctotal as should be.

It will then close one at each loop-run.

Hope I didn´t misunderstand something.

/ McKeen

Thanks!

I don't want to close one at each loop-run. I want to close all at once. The problem is the loop.

I think there is a need to re-code. I recently realize that MT4 could make more than 1 order at a time. Therefore it might be able to close all order at once. Closing one each loop will take time. Let say I have 1000 open orders, that will take minutes.

 

The problem is that there is a loop 

int ctotal = OrdersTotal();
for(int j=ctotal-1;j>=0;j--)

 

and 

OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );

can only close one at a time.

 

Is there a way to maybe get all the total order, but instead of creating a loop to OrderClose 1 by 1, 

make mt4 do many

OrderClose statements?

 

e.x.

I got n number of total order, then MT4 do this 

 

OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );

OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );

OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );

OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );

OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );

OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );

..... n number etc? 

???
 

 

Hi there,

There is no way you can just close all your orders at once, but here is a script that I wrote that will close your orders twice as fast as the regular method.

Note that it will only work if all trade sizes are the same and will only work on the currency pair of the chart that it is loaded onto. You can customise it for differing order sizes but I haven't written that routine since I don't actually use this script.....I just made it for demonstration purposes.

   int
      Buy_Ticket[],
      Sell_Ticket[];
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
  int timer = GetTickCount();
//----
   int
      Buys = 0,
      Sells = 0;
   for(int i =OrdersTotal()-1;i>=0;i--){
      OrderSelect(i,SELECT_BY_POS);
      if(OrderType()==0)Buys++;
      else Sells++;
   }
   ArrayResize(Buy_Ticket,Buys);
   ArrayResize(Sell_Ticket,Buys);
   
   if (Sells == Buys){
      for(i =OrdersTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS);
         if(OrderType()==0){
            Buy_Ticket[Buys-1] = OrderTicket();
            Buys--;
         }
         else{
            Sell_Ticket[Sells-1] = OrderTicket();
            Sells--;
         }
      }
   }
   for(i=0;i<ArraySize(Buy_Ticket);i++)OrderCloseBy(Buy_Ticket[i],Sell_Ticket[i],CLR_NONE);
   timer -= GetTickCount();
   Print("Time to close = " + timer);
  
//----
   return(0);
}
 
kennyhubbard:

Hi there,

There is no way you can just close all your orders at once, but here is a script that I wrote that will close your orders twice as fast as the regular method.

Note that it will only work if all trade sizes are the same and will only work on the currency pair of the chart that it is loaded onto. You can customise it for differing order sizes but I haven't written that routine since I don't actually use this script.....I just made it for demonstration purposes.



Thanks! twice as fast as the regular method is still an improvement. Thank you very much. I will try this!

 

 

But I got Invalid ticket error for OrderCloseBy 

 

This method uses OrderCloseBy which is simply pairing up of opposing trades. When you close a trade, you are actually making a new trade in the opposite direction. This instruction tells the broker not to make the opposite direction trade, but rather to offset and close 2 opposing trades.

It is worth allowing your EA to take a couple of milliseconds to organise your trades. This would entail matching opposite trades that are the same size with an equivalent opposite trade. Once this is done, those positions are fully hedged, and speed is not the key element. Close out the unbalanced trades(using the normal method) and then use a spread filter to close opposing matched trades when spread is at a minimum.