Close_all_open_and_pending_orders_v1
Download scriptThis script is designed as an automated utility for traders who need to quickly clear their entire trading portfolio from a specific market chart. Instead of manually closing every position and canceling every pending request one by one, this tool performs those actions automatically in a single execution.
How the Logic Works
The script follows a systematic process to ensure every active instruction is handled:
1. Portfolio Scanning When the script is activated, it first performs a complete sweep of all your current active orders and pending requests. It counts how many items are currently open and begins reviewing them one by one, starting from the most recent entries.
2. Identifying Order Types As the script scans your portfolio, it categorizes each item into one of two main groups:
- Active Positions: These are trades that are currently "live" in the market (where you are either buying or selling).
- Pending Orders: These are "waiting" instructions set to trigger at a specific price in the future.
3. Executing the Closures The script then takes different actions based on those categories:
- For Active Trades: If the script detects an open "Buy" position, it immediately sends a command to close it at the current market price. If it detects an open "Sell" position, it does the same.
- For Pending Orders: If the script finds any orders that are still waiting to be triggered (such as Buy Limits or Sell Stops), it simply removes those instructions from your account entirely so they cannot be activated.
4. Error Handling and Verification To ensure reliability, the script checks the success of every single action. If a specific order fails to close—perhaps due to a momentary connection flicker or a price jump—the script will trigger an on-screen alert notifying you of the specific order number and the reason for the failure. It then pauses briefly before moving on to the next item, ensuring it doesn't skip over any remaining orders during a glitch.
Summary of Use
This tool is essentially an "Emergency Exit" button. It provides a clean slate by simultaneously flattening all active market exposure and wiping out all future price-based instructions, leaving your trading account clear of all current activity on that specific symbol.
//+------------------------------------------------------------------+
//| 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