This script is designed as an automated safety utility for the MetaTrader trading platform. Its primary purpose is to act as a "panic button" or a total liquidation tool, ensuring that all active market positions are cleared immediately upon the script's execution.
Here is a breakdown of how the logic functions:
Core Functionality
The script is engineered to scan your open trading account and identify any active orders. Once it detects an open position, it systematically executes a command to close those trades. It handles both "Buy" and "Sell" types of orders independently to ensure that every transaction—regardless of its direction—is terminated.
Operational Workflow
- Immediate Execution: The script is programmed to trigger the closing process as soon as it is loaded into the platform or started manually. It does not wait for specific price targets, time intervals, or technical indicators; its sole objective is immediate action.
- Recursive Scanning: The logic utilizes a "looping" approach. This means that once the script closes one trade, it immediately looks for the next available open position. It will continue this cycle until every single open order has been successfully closed.
- Dynamic Pricing: To ensure the trades are closed at the most accurate current market rates, the script automatically references the "Bid" price (for buying positions) and the "Ask" price (for selling positions) at the exact moment of execution.
Summary for Users
In simple terms, this tool serves as an automated "Close All" command. If you need to exit all current market exposure instantly without manually closing each trade one by one, this script automates that process to ensure your account is flattened in a matter of seconds.
//=====================================================================================================================================================
int NONE=0;
//=====================================================================================================================================================
int init()
{
CloseAll();
return(0);
}
int deinit()
{
return(0);
}
int start()
{
CloseAll();
return(0);
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------
double CloseAll()
{
if(OrderSelect(OrderTicket(),SELECT_BY_TICKET)==true)
{
if(OrdersTotal()>NONE)
{
if(OrderType()==OP_BUY)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
CloseAll();
}
if(OrderType()==OP_SELL)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Green);
CloseAll();
}
}
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------
Comments