Close all open and pending orders

Download script
Author: Copyright � 2005, Matias Romeo.
Close all open and pending orders

This script is designed as an automated utility for MetaTrader 4 users who need to quickly clear their trading workspace. Its primary function is to systematically identify every active transaction and pending request associated with your trading account and shut them down simultaneously.

The logic operates through the following steps:

Comprehensive Market Scanning When executed, the script first performs a complete scan of your account's open positions. It looks at every single active trade currently live in the market, as well as any "pending" orders (orders waiting for the price to hit a specific level before activating).

Identification and Categorization The tool evaluates each item it finds to determine what kind of order it is. It distinguishes between three main categories:

  • Active Buy Positions: These are trades where you currently hold a "long" position (buying at a lower price to sell higher).
  • Active Sell Positions: These are trades where you currently hold a "short" position (selling at a higher price to buy back lower).
  • Pending Orders: These are "limit" or "stop" orders that have not been triggered yet but are sitting on the chart waiting for market movement.

Execution Actions Once an order is identified, the script takes the appropriate action based on its category:

  1. For Live Positions (Buys and Sells): The script automatically executes a closing command at the current prevailing market price. It ensures that buy positions are closed using the "Bid" price and sell positions are closed using the "Ask" price to ensure the transaction is completed successfully.
  2. For Pending Orders: Instead of trying to "close" them (since they aren't live yet), the script simply deletes them from your active watch list.

Error Handling and Safety The logic includes a verification step for every action it takes. If the script attempts to close a position or delete an order but the platform denies the request (due to a connection issue, lack of liquidity, or other market restrictions), the script will trigger an on-screen alert notification. This informs you exactly which order failed and provides an error code so you can investigate the specific reason for the failure.

Workflow Order The script processes these orders in reverse order (from the most recent to the oldest). This is a standard safety practice in trading software to ensure that removing one item from a list doesn't cause the system to skip over the next one during the scanning process.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
Miscellaneous
It issuies visual alerts to the screen
18 Views
0 Downloads
0 Favorites
Close all open and pending orders
//+------------------------------------------------------------------+
//|                                            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