a1 Close THIS SYMBOL Pstns
Download scriptThis script is designed as an automated management tool for MetaTrader 4 users who need to quickly exit their market exposure. Its primary function is to identify and instantly close every active trade associated with the specific financial instrument shown on your current chart.
Core Functionality
The tool performs a comprehensive scan of your open trading portfolio. Instead of closing every trade you might have across different currencies or assets, it specifically filters for positions that match the symbol of the chart where the script is executed. For example, if you run this on a EURUSD chart, it will only target your EURUSD positions, leaving all other unrelated trades untouched.
Operational Logic
The script follows a systematic process to ensure every position is cleared:
- Portfolio Scanning: It starts at the bottom of your open trade list and works backward to the top. This "reverse" scanning method is a standard reliability practice to ensure that no trades are skipped as others are removed from the list.
- Trade Verification: For every open position found, the script checks two conditions:
- Is it an active market trade (a Buy or a Sell)?
- Does it belong to the current chart's symbol?
- Execution and Price Matching: Once a valid trade is identified, the script automatically determines the correct current market price (the "Bid" for selling or the "Ask" for buying) to execute the closure at the best available market rate.
- Error Handling & Persistence: The script includes a built-in safety loop. If a trade fails to close due to a temporary connection glitch or a rapid price fluctuation, the tool will automatically refresh the data and try again immediately until the position is successfully closed.
User Experience
When you activate this script, it provides a confirmation prompt to ensure the action was intentional. Once confirmed, the tool handles the heavy lifting of navigating your trade history, checking symbols, and processing the exits simultaneously, providing a one-click solution for clearing out a specific asset's market position.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| a1 Cls THIS SYMBOL Pstns.mq4 |
//| Close ALL positions with this chart's SYMBOL |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Ronald Verwer, Forex MetaSoft"
#property link "www.forexmetasoft.com"
#property show_confirm
//+------------------------------------------------------------------+
//| script to close all positions with this chart's SYMBOL 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) && OrderSymbol()==Symbol())
{
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