Closing all open positions?

 
Quick question. I am trading with MT5, and i am looking for a way to close all open positions at once. Is this function in MT5?
I have looked everywhere in the platform and cant find it. If it isnt, why not? It really seems like this otion should be there as default. 
 
No there is not there by default but you can write an EA to that for you
 
There are a lot of "close all" EAs and the scripts.
You can check the following search results here.
 
Ivarsen:
Quick question. I am trading with MT5, and i am looking for a way to close all open positions at once. Is this function in MT5?
I have looked everywhere in the platform and cant find it. If it isnt, why not? It really seems like this otion should be there as default. 

Maybe this can help you

https://www.mql5.com/en/code/25650

Close-All
Close-All
  • www.mql5.com
A script to close all market positions and/or pending orders.
 

I have created a function for that. Hope this helps

void CloseThisSymbolAll()
  {
   int positions,orders;
   ulong inpMagic = 0;
   ulong ticket = PositionGetInteger(POSITION_TICKET);
   int orderType = (int)PositionGetInteger(POSITION_TYPE) ;
   int orderPendingType = (int)OrderGetInteger(ORDER_TYPE);
   string orderSymbol = PositionGetString(POSITION_SYMBOL);
   string orderPendingSymbol = OrderGetString(ORDER_SYMBOL);
   ulong orderPendingTicket = OrderGetInteger(ORDER_TICKET);
   ulong orderMagicNumber = PositionGetInteger(POSITION_MAGIC);
   ulong orderPendingMagicNumber = OrderGetInteger(ORDER_MAGIC);

   for(orders=OrdersTotal()-1, positions=PositionsTotal()-1; positions>=0 || orders>=0;positions--,orders--)
     {
      ulong numTicket = PositionGetTicket(positions);
      ulong numOrderTicket = OrderGetTicket(orders);

         if(orderType==POSITION_TYPE_BUY)
            {
            trade.PositionClose(numTicket);
            }
         if(orderType==POSITION_TYPE_SELL)
            {
            trade.PositionClose(numTicket);
            }
         if(orderPendingType==ORDER_TYPE_BUY_LIMIT || orderPendingType==ORDER_TYPE_SELL_LIMIT ||
         orderPendingType==ORDER_TYPE_BUY_STOP||orderPendingType==ORDER_TYPE_SELL_STOP
         ||orderPendingType==ORDER_TYPE_BUY_STOP_LIMIT||orderPendingType==ORDER_TYPE_SELL_STOP_LIMIT)
            {
            trade.OrderDelete(numOrderTicket);
            }
        }

     }
 
Renz Carillo #:

I have created a function for that. Hope this helps

Thanks bro, helped a lot. 

 
Renz Carillo #: I have created a function for that. Hope this helps
  1.    ulong ticket = PositionGetInteger(POSITION_TICKET);
       int orderType = (int)PositionGetInteger(POSITION_TYPE) ;
       int orderPendingType = (int)OrderGetInteger(ORDER_TYPE);
       string orderSymbol = PositionGetString(POSITION_SYMBOL);
       string orderPendingSymbol = OrderGetString(ORDER_SYMBOL);
       ulong orderPendingTicket = OrderGetInteger(ORDER_TICKET);
       ulong orderMagicNumber = PositionGetInteger(POSITION_MAGIC);
       ulong orderPendingMagicNumber = OrderGetInteger(ORDER_MAGIC);
    What position did you select before trying to read those?
  2. Where are you using those variables?
  3.    for(orders=OrdersTotal()-1, positions=PositionsTotal()-1; positions>=0 || orders>=0;positions--,orders--)
         {
          ulong numTicket = PositionGetTicket(positions);
          ulong numOrderTicket = OrderGetTicket(orders);
    What happens when positions or orders goes negative? Separate and simplify.
Reason: