Close all pending orders_v1

Download script
Author: Copyright � 2005, Matias Romeo.
Close all pending orders_v1

This script is designed as a management tool for the MetaTrader 4 platform, specifically created to quickly clear out all pending market orders with a single execution. Unlike active trades that are already open in the market, "pending" orders are instructions waiting for the price to reach a specific level before they become active. This tool ensures you can wipe those waiting instructions from your account instantly.

How the Logic Works

The script follows a systematic process to clean up your trading terminal:

1. Scanning Your Account When activated, the script first performs an inventory check of all current orders on your account. It identifies every active instruction currently listed in your "Trade" window.

2. Identifying Pending Orders The tool then examines each individual order one by one to determine its status. It specifically looks for four types of pending instructions:

  • Buy Limits and Buy Stops
  • Sell Limits and Sell Stops

If an order is a standard active trade (an already open position), the script ignores it, ensuring that your currently running trades remain unaffected.

3. Executing the Removal Once the script identifies a pending order, it sends a command to the server to delete that specific ticket. It processes these from the bottom of your list to the top to ensure that the numbering remains consistent as orders are removed one by one.

4. Error Handling and Notifications The script is built with a safety check. If for any reason the platform fails to remove an order (such as a connection issue or a server delay), the tool will:

  • Trigger a visual alert on your screen.
  • Provide the specific ticket number of the failed order.
  • Report the specific error code provided by the server.
  • Pause for three seconds before continuing, giving you time to see the notification without the system crashing or skipping ahead too quickly.
Orders Execution
Checks for the total of open orders
Miscellaneous
It issuies visual alerts to the screen
10 Views
0 Downloads
0 Favorites
Close all pending orders_v1
//+------------------------------------------------------------------+
//|                                            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 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