a2 Close ALL Pstns_v1

Download script
Author: Copyright � 2006, Ronald Verwer, Forex MetaSoft
a2 Close ALL Pstns_v1

This script is an automated utility designed for traders using the MetaTrader platform to instantly clear their active trading accounts. Its primary purpose is to identify every open position and close them simultaneously at current market prices, ensuring that no manual intervention is required to exit multiple trades one by one.

How the Logic Works

The script follows a systematic process to ensure all active trades are handled efficiently:

  • Comprehensive Scanning: The tool starts by looking at your entire account. It scans through every active trade currently running across all available currency pairs or assets in your portfolio.
  • Filtering Relevant Trades: It specifically identifies "Buy" and "Sell" positions. By focusing only on these types of trades, it ensures that other non-trade orders (like pending limit orders) are not accidentally affected.
  • Reverse Order Processing: The logic processes trades starting from the most recent one and moves backward to the first. This is a standard professional practice in trading software to ensure that the list of active positions remains stable as items are removed during the process.
  • Real-Time Price Execution: For every trade identified, the script automatically fetches the most current market price (the "Bid" for sells and the "Ask" for buys). It then attempts to execute a closing command immediately at that price.
  • Error Handling and Persistence: If the market moves so quickly that a trade cannot be closed instantly (often resulting in a "Requote"), the script is designed to be persistent. Instead of giving up, it refreshes the latest data and immediately tries again until the position is successfully closed.
  • Logging: Throughout the process, the script monitors for any communication errors with the broker's server. If an issue occurs that prevents a trade from closing, it logs the specific error code so you can review what happened.

Summary of Behavior

Essentially, this tool acts as an "Emergency Exit" button. When activated, it sweeps through your account, identifies every active market position, and forces them to close at the best available current price, automatically retrying if the market is fluctuating rapidly.

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

//+------------------------------------------------------------------+
//|                                           a2 Close ALL Pstns.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Ronald Verwer, Forex MetaSoft"
#property link "www.forexmetasoft.com"
#property show_confirm
//+------------------------------------------------------------------+
//| script to close ALL 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 || type==OP_SELL)
            {
            while(true)
               {
               if(type==OP_BUY) price=MarketInfo(OrderSymbol(),MODE_BID);
               else price=MarketInfo(OrderSymbol(),MODE_ASK);
               result=OrderClose(OrderTicket(),OrderLots(),price,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