Close_all_pending_orders
Download scriptThis script is a specialized utility designed for the MetaTrader 4 trading platform. Its primary purpose is to provide traders with a quick and efficient way to clear their trading workspace of all active "pending" orders simultaneously.
How it Works
When the script is executed, it performs an automated sweep of your current trading account to identify any open instructions that have not yet been triggered into actual trades.
The logic follows these specific steps:
- Comprehensive Inventory Check: The script first counts every single order currently held in your platform's active list.
- Systematic Review: It moves through this list one by one, starting from the most recent entries and working backward to ensure nothing is skipped during the process.
- Targeted Identification: The script specifically looks for "Pending Orders." In trading terms, these are orders that are waiting for the market price to hit a specific level (such as Buy Limits, Buy Stops, Sell Limits, or Sell Stops). It ignores trades that are already open and running (active positions).
- Automated Cancellation: Once a pending order is identified, the script automatically sends a command to the server to delete/cancel it immediately.
- Error Reporting & Safety: If the script encounters an issue—such as a connection problem or a restriction from the broker preventing the cancellation—it will trigger an on-screen alert notification. It also includes a brief pause (3 seconds) if an error occurs, allowing the system time to stabilize before trying to proceed with the next item in the list.
Summary for the User
Think of this script as a "Clear All" button for your pending orders. Instead of manually right-clicking and deleting every individual limit or stop order on your chart, this tool handles the entire cleanup process in a single click, ensuring your trading environment is cleared of any outstanding instructions.
/*-----------------------------+
| |
| 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"
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 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