close-all-orders
Download scriptThis script is designed as a utility tool for traders using the MetaTrader 4 platform to instantly manage their active trading portfolio. Its primary purpose is to provide an automated "panic button" or a comprehensive cleanup mechanism that clears out all market activity with a single execution.
How the Logic Works
When the script is activated, it performs a systematic sweep of your entire trading account to identify every active transaction. Here is the step-by-step process it follows:
1. Inventory Scan The tool first counts every open order currently existing in your account. It then begins a countdown review, starting from the most recent position and working backward to ensure that no trades are skipped during the process.
2. Identification of Order Types As it scans through your accounts, the script categorizes each individual item into one of two main groups:
- Active Positions: These are trades currently "live" in the market (where you have already entered a trade to buy or sell).
- Pending Orders: These are "waiting" instructions (limit or stop orders) that have not yet been triggered by price movement.
3. Execution of Actions Based on how the script categorizes your trades, it takes different actions:
- For Active Buys and Sells: The script immediately sends a command to the broker to close these positions at the current market price. It aims to exit the trade completely, whether you were in a profit or a loss.
- For Pending Orders: Instead of trying to "fill" them, the script simply removes these instructions from your platform entirely, canceling any future entries.
4. Error Handling and Notifications The script is designed with a safety check to ensure it completes its task successfully. If it encounters an order that it cannot close (due to a connection issue, a price gap, or other market restrictions), the tool will:
- Trigger a visual alert on your screen identifying the specific order number.
- Provide a brief pause to allow for system stability.
- Notify you of the specific error code returned by the server so you can investigate why that specific trade remained open.
Summary of Use
In short, this script serves as an emergency exit tool. It ensures that every single commitment—whether it is a trade currently running or a request to enter one later—is cleared from your screen immediately upon execution.
//+------------------------------------------------------------------+
//| 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 );
break;
//Close pending orders
case OP_BUYLIMIT :
case OP_BUYSTOP :
case OP_SELLLIMIT :
case OP_SELLSTOP : result = OrderDelete( OrderTicket() );
}
if(result == false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}
return(0);
}
Comments