Close_all_open_and_pending_orders

Download script
Author: Copyright � 2005, Matias Romeo.
Close_all_open_and_pending_orders

This automated script is designed for traders using the MetaTrader platform to provide a rapid "emergency exit" or "clean slate" functionality. Its primary purpose is to immediately clear your trading environment of all active and pending activities with a single execution.

How it Works

When the script is activated, it performs an exhaustive scan of your current trading account. It looks at every open position and every pending order simultaneously, processing them from the most recent to the oldest to ensure no items are skipped during the operation.

The script categorizes your orders into two main groups:

  • Active Positions (Buy and Sell): For trades that are already running in the market, the script identifies whether you are "Long" (buying) or "Short" (selling). It then automatically executes a closing command at the current market price to exit those positions immediately.
  • Pending Orders: For orders that have been set up but have not yet been triggered (such as limit or stop orders), the script simply removes them from the system entirely, cancelling them before they have a chance to open into active trades.

Safety and Feedback

To ensure the user is aware of the script's progress, it includes a built-in notification system. If the platform encounters any issues—such as a connection glitch or a price movement that prevents an order from closing instantly—the script will trigger a visual alert on your screen. It then pauses briefly to allow for stability before attempting to move to the next item, ensuring that you are informed of any errors in real-time.

Summary of Use

This tool is ideal for situations where a trader needs to liquidate their entire portfolio instantly due to news events, account management requirements, or a sudden change in market strategy. It removes the need to manually click through every individual trade, significantly reducing the risk of human error during high-pressure moments.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
Miscellaneous
It issuies visual alerts to the screen
15 Views
0 Downloads
0 Favorites
Close_all_open_and_pending_orders
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                            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, Red );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
                          break;

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




Comments