Close/Delete all open/pending orders immediately

To add comments, please log in or register
Jose Luis Lominchar
563
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!

Mehmet Bastem
844
Mehmet Bastem  
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);
}




Jose Luis Lominchar
563
Jose Luis Lominchar  

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!

Jose Luis Lominchar
563
Jose Luis Lominchar  

... something that produces similar faster results as:

nicholi shen
2393
nicholi shen  
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.  

Jose Luis Lominchar
563
Jose Luis Lominchar  
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!
To add comments, please log in or register