Close_All
Download scriptThis automated script is designed as a comprehensive "emergency exit" tool for your trading account. Its primary function is to instantly clear out all active market positions and pending orders, ensuring that no trades remain open on your platform simultaneously.
How the Logic Works
The script follows a systematic three-step process to ensure every trade is accounted for:
1. Identification and Scanning Upon execution, the script scans your entire trading account to identify every single active order. It distinguishes between two types of actions:
- Pending Orders: These are "waiting" trades that have not yet been triggered (limit or stop orders).
- Active Market Positions: These are trades currently running in the market (either Buy or Sell positions).
2. Systematic Execution The script then moves through your list of trades and performs specific actions based on what it finds:
- Cancellation: It automatically deletes all pending orders from the system so that no new positions can be opened unexpectedly.
- Liquidation: It immediately sends market orders to close out all open Buy and Sell positions at the current prevailing market prices.
3. Verification Loop To account for the high speed of electronic trading, the script includes a "retry" mechanism. It repeats the scanning and closing process multiple times in rapid succession. This ensures that if a trade was in the middle of closing or if new data refreshed during the process, it captures and closes those as well.
Final Confirmation
Once the script completes its cycle, it checks the account one last time:
- Success: If no trades are left open, it provides an on-screen alert notifying you that the job is complete.
- Error Handling: If there are still trades remaining (due to connection issues or extreme market volatility), it will provide a warning alert instructing you to run the script again to ensure everything is cleared.
//+------------------------------------------------------------------+
//| Close_All.mq4 |
//| (c) 2007 Investatech Inc., www.investatech.com |
//| http://www.investatech.com |
//+------------------------------------------------------------------+
#property copyright "(c) 2007 Investatech Inc., www.investatech.com"
#property link "http://www.investatech.com"
#property show_inputs
#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
//----
for (int i=0;i<5;i++)
{
int total_trades=OrdersTotal();
if (total_trades==0)
{
Alert("Job Done! Visit http://www.pipboxer.com");
return(0);
}
for(int j=0;j<total_trades;j++)
{
if(OrderSelect(j,SELECT_BY_POS,MODE_TRADES))
{
int order_type=OrderType();
double order_openprice=OrderOpenPrice();
int order_ticket=OrderTicket();
double order_volume=OrderLots();
string order_symbol=OrderSymbol();
//close pending orders
if (order_type>=OP_BUYLIMIT) OrderDelete(order_ticket,CLR_NONE);
//close sell orders
if (order_type==OP_SELL) OrderClose(order_ticket,order_volume,MarketInfo(order_symbol,MODE_ASK),3,CLR_NONE);
//close buy orders
if (order_type==OP_BUY) OrderClose(order_ticket,order_volume,MarketInfo(order_symbol,MODE_BID),3,CLR_NONE);
}
}
}
total_trades=OrdersTotal();
if (total_trades>0)
Alert("Something went wrong! Repeat the script.");
//----
return(0);
}
//+------------------------------------------------------------------+
Comments