Close/Delete all open/pending orders immediately

 

I want to ask you if it is possible to close all open orders and to delete all pending orders within a unique call to a method.

Without the need to iterate over Orders MODE_TRADES.

Thanks!

 
Jose Luis Lominchar:

I want to ask you if it is possible to close all open orders and to delete all pending orders within a unique call to a method.

Without the need to iterate over Orders MODE_TRADES.

Thanks!

//+------------------------------------------------------------------+
//|                                            close-all-orders.mq4  |
//|                                  Copyright © 2005, Matias Romeo. |
//|                                       Custom Metatrader Systems. |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2005, Matias Romeo."
#property link      "mailto:matiasDOTromeoATgmail.com"

int start()
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, 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, CLR_NONE );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE );
                          break;

      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
    if(result == false)
    {
       if (GetLastError()>0) Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  
  return(0);
}




 

I havent explain myself properlly. I am trying to avoid to iterate over the trades in order to close/delete them.

I think MQL4 doesnt offer a method like deleteAllTrades();

Thank you!

 

... something that produces similar faster results as:

 
Jose Luis Lominchar:

... something that produces similar faster results as:

This is the sequence to close all orders in the fastest possible way. 

  1. Manager EA opens worker EAs on separate charts that can receive commands from the manager for asynchronous order ops.
  2. Manager EA calculates net positions and sends commands to worker EA to async hedge each net position (if more than one order exists) or close position (if only one order exists).
  3. Manager send commands to delete pending orders (async)
  4. Manager does a multiple closeby in order to reconcile zeroed out (temporarily hedged) positions.  

 
nicholi shen:

This is the sequence to close all orders in the fastest possible way. 

  1. Manager EA opens worker EAs on separate charts that can receive commands from the manager for asynchronous order ops.
  2. Manager EA calculates net positions and sends commands to worker EA to async hedge each net position (if more than one order exists) or close position (if only one order exists).
  3. Manager send commands to delete pending orders (async)
  4. Manager does a multiple closeby in order to reconcile zeroed out (temporarily hedged) positions.  

I ll try it. Thank you!
 
Mehmet Bastem #:



//+------------------------------------------------------------------+
//|                                            close-all-orders.mq4  |
//|                                  Copyright © 2005, Matias Romeo. |
//|                                       Custom Metatrader Systems. |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2005, Matias Romeo."
#property link      "mailto:matiasDOTromeoATgmail.com"

int start()
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, 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, CLR_NONE );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE );
                          break;

      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
    if(result == false)
    {
       if (GetLastError()>0) Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  
  return(0);
}



How do you tell the terminal to make sure that you close open orders first, then work on the pending orders?

 
Joshua Graham #:




How do you tell the terminal to make sure that you close open orders first, then work on the pending orders?

I suggest to use two loops. One to close the open orders, and skip the pending orders, and a second loop to close the pending orders.

 
Use MT5 and asynchronous OrderSend command.
 
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE );

MT4: You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type to get the close price.
MT5: you can use PositionGetDouble(POSITION_PRICE_CURRENT)

Reason: