Close_onepair_open_and_pending_orders

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

This script is designed as an automated management tool for the MetaTrader platform, specifically created to provide traders with a "panic button" or a quick way to clear their positions on a specific currency pair or asset. Its primary purpose is to instantly neutralize all active trading activity associated with the current chart you are viewing.

How the Logic Works

When the script is executed, it performs a comprehensive sweep of your open trading activities. Here is the step-by-step logic it follows:

1. Identification of Relevant Trades The script first scans through every open order in your trading account. It specifically filters these to ensure it only interacts with trades belonging to the current symbol (the specific currency pair or asset) displayed on your screen. This prevents the script from accidentally closing trades on other assets you might be monitoring simultaneously.

2. Categorization of Orders Once it identifies a relevant trade, the script determines what "type" of order it is. It handles two distinct categories differently:

  • Active Positions: If you are currently "in the market" (holding an open Buy or Sell position), the script identifies the current market price and sends an immediate command to close that position at the best available price.
  • Pending Orders: If you have "limit" or "stop" orders waiting to be triggered (orders that haven't opened yet), the script identifies these and cancels them entirely, removing them from the order book.

3. Execution and Error Handling The script attempts to execute these closures one by one. Because market conditions can change rapidly, it includes a built-in verification system:

  • If a trade is successfully closed or deleted, it moves to the next one.
  • If the platform fails to close a specific order (due to a connection issue, price slippage, or other technical hurdles), the script will trigger an on-screen alert notifying you of the specific order number and the error code received. It then pauses briefly before trying to continue, ensuring that one failure doesn't cause the script to crash instantly.

Summary for the User

In simple terms, this tool acts as an automated "cleanup" service. Instead of manually clicking "Close" on every individual trade or deleting every pending order one by one during a high-volatility event, this script performs that entire process automatically in a matter of seconds for the asset you are currently viewing.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
Miscellaneous
It issuies visual alerts to the screen
14 Views
0 Downloads
0 Favorites
Close_onepair_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()
{
    for( int count = OrdersTotal(); count > 0; count-- ) {
    OrderSelect(count, SELECT_BY_POS);
    if(OrderSymbol()==Symbol()){
      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