Close all open positions
Download scriptThis script is a utility tool designed for the MetaTrader 4 platform that provides a "panic button" or an immediate exit strategy for active trades. Its primary purpose is to systematically identify and shut down every open position in your trading account simultaneously.
How the Logic Works
When the script is executed, it follows a structured sequence to ensure all active trades are handled:
- Account Scanning: The tool begins by scanning your entire trading account to determine the total number of currently open orders.
- Reverse Iteration: It processes these orders in a "backward" loop (starting from the most recent and moving toward the oldest). This is a standard logic used in trading software to ensure that as positions are closed, the numbering of the remaining orders doesn't cause the script to skip over any specific trade.
- Trade Identification: For every open position it finds, the script checks whether you are currently "Long" (buying) or "Short" (selling).
- Execution of Closure:
- If it detects a Buy position, it automatically sends a command to close that trade at the current best available selling price.
- If it detects a Sell position, it automatically sends a command to close that trade at the current best available buying price.
- Error Handling and Notifications: The script includes a safety check for each trade. If a specific order fails to close for any reason (such as a sudden loss of internet connection or a price gap), the tool will:
- Generate an on-screen alert notifying you of the specific order number that failed.
- Display the specific error code provided by the server.
- Pause briefly before attempting to continue, ensuring the system remains stable while processing the remaining list.
Summary for the User
In simple terms, this script removes the need to manually close every individual trade one by one during high-volatility moments. You can run it once, and it will systematically work through your entire portfolio to exit all positions instantly, providing an automated way to "flat" your account or secure your current profits and losses immediately.
//+------------------------------------------------------------------+
//| close-all-orders.mq4 |
//| Copyright © 2005, Matias Romeo. |
//| Custom Metatrader Systems. |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Matias Romeo."
#property link "mailto:matiasDOTromeoATgmail.com"
int start()
{
int total = OrdersTotal();
for(int i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = false;
switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;
//Close opened short positions
case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
}
if(result == false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}
return(0);
}
Comments