CloseAllOrders
Download scriptThis script is designed as a utility tool for the MetaTrader 4 platform, specifically engineered to provide traders with an automated way to clear their active positions quickly. Instead of manually closing every individual trade one by one—which can be time-consuming and prone to error during fast-moving market conditions—this script handles the entire process in a single execution.
Core Functionality
When activated, the script scans your trading account to identify all currently open orders. It specifically targets active trades (such as Buy and Sell positions) and initiates a command to close them immediately.
How the Logic Works
The script follows a systematic three-step process to ensure every trade is handled accurately:
1. Account Scanning The program starts by counting how many total orders are currently open on your account. It then works backward through this list, ensuring that it doesn't skip over any positions as they are being closed.
2. Real-Time Market Validation Before attempting to close a trade, the script performs a "health check" on the market. It gathers the most recent Ask and Bid prices for the specific currency pair or asset you are trading. This ensures that the tool is using the most up-to-date information available, preventing the script from trying to execute an order based on outdated pricing.
3. Execution and Error Reporting For every open trade identified, the script automatically submits a closing command. It calculates the necessary details (such as volume and price) automatically. If for any reason a specific trade cannot be closed (due to market connectivity issues or other platform restrictions), the script is designed to capture that specific error and display a clear message in the terminal logs. This allows you to see exactly which trade might have failed and why, rather than the script simply stopping without explanation.
Key Benefits
- Efficiency: Eliminates the need for manual interaction when you need to flatten your positions instantly.
- Accuracy: By refreshing market data automatically before each action, it minimizes the risk of "outdated price" errors.
- Transparency: Provides a feedback system that reports any issues immediately, ensuring you stay informed about your account status at all times.
//+------------------------------------------------------------------+
//| CloseAllOrders.mq4 |
//| Copyright © 2008, PRMQuotes Software Corp. |
//| Jedimedic77@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, PRMQuotes Software Corp."
#property link ""
//+------------------------------------------------------------------+
//| EX4 imports |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| global variables to program: |
//+------------------------------------------------------------------+
double Price[2];
int giSlippage;
bool CloseOpenOrders = true;
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
void start() {
int iOrders=OrdersTotal()-1, i;
if(CloseOpenOrders) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderType()<=OP_SELL) && GetMarketInfo() && !OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],giSlippage)) Print(OrderError());
}
}
}
//+------------------------------------------------------------------+
//| Function..: OrderError |
//+------------------------------------------------------------------+
string OrderError() {
int iError=GetLastError();
return(StringConcatenate("Order:",OrderTicket()," GetLastError()=",iError," ",ErrorDescription(iError)));
}
//+------------------------------------------------------------------+
//| Function..: GetMarketInfo |
//+------------------------------------------------------------------+
bool GetMarketInfo() {
RefreshRates();
Price[0]=MarketInfo(OrderSymbol(),MODE_ASK);
Price[1]=MarketInfo(OrderSymbol(),MODE_BID);
double dPoint=MarketInfo(OrderSymbol(),MODE_POINT);
if(dPoint==0) return(false);
giSlippage=(Price[0]-Price[1])/dPoint;
return(Price[0]>0.0 && Price[1]>0.0);
}
//+------------------------------------------------------------------+
Comments