This script is a specialized utility designed for the MetaTrader 4 platform to provide a quick, one-click solution for managing active trading accounts. Its primary purpose is to automatically identify and close every open trade simultaneously at current market prices.
How it Works
The logic follows a structured process to ensure that every position is cleared systematically:
- Comprehensive Scan: The script starts by performing a complete sweep of your account's open positions. It looks through your entire portfolio to identify every active trade you currently have running.
- Trade Identification: During this scan, the script distinguishes between different types of orders. It specifically targets "Buy" and "Sell" market positions, ensuring it only interacts with active trades rather than pending orders or other non-executable data.
- Dynamic Price Execution: For each trade identified, the script automatically determines the best current market price to exit the position. Depending on whether you are closing a "Buy" or a "Sell," it pulls the most up-to-date pricing information from the live market.
- Smart Error Handling: The script includes an intelligent loop designed to handle "busy" markets. If a trade fails to close instantly due to rapid price fluctuations (a common occurrence in volatile markets), the script automatically refreshes the data and retries the closure until the trade is successfully finalized.
- Sequential Processing: By working backward through your list of trades, the script ensures it doesn't skip any positions, providing a reliable "emergency exit" or portfolio cleanup tool.
Key Benefits for Users
Instead of manually right-clicking and closing each individual position one by one—which can be time-consuming and risky during fast-moving market conditions—this script automates that entire sequence in seconds. It acts as an automated "Close All" button, providing a fail-safe way to flatten your positions at the current market rate.
//+------------------------------------------------------------------+
//| a2 Close ALL Pstns.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Ronald Verwer, ROVERCOM"
#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