Close all open and pending orders_v1
Download scriptThis script is designed as a utility tool for MetaTrader users to provide immediate and comprehensive control over their active trading positions and pending orders. Its primary function is to act as an "emergency exit" or a "clean slate" mechanism, allowing a trader to clear all market activity with a single execution.
Core Functionality
The logic follows a systematic process to scan the entire account's current activity and perform the following actions:
- Active Position Management: The script identifies all currently open "Buy" and "Sell" positions. For every active trade it finds, it automatically executes a closing command at the current market price to exit the position immediately.
- Pending Order Removal: Beyond just closing active trades, the script also identifies "pending" orders (orders waiting for a specific price to be hit, such as Buy Limits, Sell Stops, etc.). It systematically deletes these from the system so that no new positions can be opened automatically in the future.
- Automated Error Handling: To ensure reliability, the script includes a safety check. If it encounters an issue while trying to close a specific order—such as a connection glitch or a price movement—it will trigger a visual alert on your screen and pause briefly before attempting to move to the next item in the list.
How It Works for the User
From a practical standpoint, you do not need to manually click on individual trades or navigate through various currency pairs to close them one by one. When this script runs:
- It looks at every single open order across your entire account.
- It differentiates between what is "active" (needs to be sold/bought back) and what is "pending" (needs to be canceled).
- It performs the cleanup automatically, providing you with a clear notification if any specific order fails to close.
This tool is essentially a "Master Off Switch," useful for risk management, switching strategies quickly, or securing profits across multiple assets simultaneously.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| close-all-orders.mq4 |
//| Copyright © 2005, Matias Romeo. |
//| Custom Metatrader Systems. |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Matias Romeo."
#property link "mailto:matiasDOTromeoATgmail.com"
extern int ProfitTarget = 10; // Profit target in dollars
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