a3 Close ALL LONG Pstns

Download script
Author: Copyright � 2006, Ronald Verwer, Forex MetaSoft
a3 Close ALL LONG Pstns

This script is designed as a utility tool for traders using the MetaTrader platform to manage their active trading portfolio quickly. Its primary purpose is to perform a bulk action: identifying and closing every open "Buy" position simultaneously at current market prices.

Core Functionality

When activated, the script scans your entire trading account to identify all active transactions. It specifically looks for "Long" positions (trades where you are betting on the price increasing). Once identified, it executes a command to close those specific positions immediately. Any "Sell" positions or other types of pending orders remain completely untouched by this script.

How the Logic Works

  • Comprehensive Scanning: The script works backward through your list of active trades. By scanning from the most recent to the oldest, it ensures that every single open position is accounted for without skipping any during the closing process.
  • Targeted Filtering: It distinguishes between different types of trades. It ignores "Sell" orders and only triggers actions on "Buy" orders, allowing the user to clear out one side of their portfolio while leaving others intact.
  • Execution Reliability: The script includes a robust "retry" mechanism. In fast-moving markets, a trade might fail to close instantly due to price fluctuations. If the platform reports a specific type of execution error related to outdated pricing, the script automatically refreshes the market data and immediately attempts to close the position again until it is successfully finished.
  • Error Reporting: If the script encounters an issue—such as a failure to select a trade or a persistent error during the closing process—it records the specific error code in the system log. This allows the user to see exactly what went wrong if a trade fails to close.

User Experience

From a user perspective, this tool serves as an "emergency exit" or a "portfolio cleanup" button. Instead of manually right-clicking and closing dozens of individual trades one by one—which can be time-consuming and risky in volatile markets—this script automates the entire process in a single click, ensuring all long positions are exited as quickly as the market allows.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
12 Views
0 Downloads
0 Favorites
a3 Close ALL LONG Pstns
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                      a3 Close ALL LONG Pstns.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Ronald Verwer, Forex MetaSoft"
#property link "www.forexmetasoft.com"
#property show_confirm
//+------------------------------------------------------------------+
//| script to close ALL LONG positions at market                     |
//+------------------------------------------------------------------+
int start()
  {
   int    i,type,err,Slippage=3;
   double price;
   bool   result;
//----
   for(i=OrdersTotal()-1;i>=0;i--)
      {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         {
         type=OrderType();
         if(type==OP_BUY)
            {
            while(true)
               {
               result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,CLR_NONE);
               if(result!=true) {err=GetLastError(); Print("LastError = ",err);}
               else err=0;
               if(err==135) RefreshRates();
               else break;
         }  }  }
      else Print( "When selecting a trade, error ",GetLastError()," occurred");
      }
   return(0);
   }
//+------------------------------------------------------------------+

Comments