CloseAllTrades
Download scriptThis tool is designed as a specialized utility for the MetaTrader 4 trading platform, specifically engineered to provide traders with an immediate way to clear their active positions and pending orders on a specific currency pair or asset.
Core Functionality
The primary purpose of this script is "market cleanup." Instead of manually closing every individual trade one by one—which can be time-consuming during fast-moving market conditions—this script automates the entire process instantly. When activated, it scans your trading account to identify all active activities related to the specific chart you have open.
How the Logic Works
The script follows a systematic three-step process to manage your trades:
- Identification: The tool begins by scanning every open trade in your account. It is programmed to be "location aware," meaning it only interacts with trades associated with the current symbol on your screen. This ensures that if you are trading multiple different currencies simultaneously, it won't interfere with your other positions.
- Execution of Active Trades:
- Buy Positions: If the script finds an active "Buy" trade, it automatically triggers a market sell to close that position at the current best price.
- Sell Positions: If it finds an active "Sell" trade, it triggers a market buy to close it immediately.
- Removal of Pending Orders: Beyond just closing trades that are already running, the script also identifies "Pending Orders" (limit and stop orders that are waiting for the price to hit a certain level). It systematically deletes these from the queue so that no new trades can be triggered automatically.
Key Features
- Precision Handling: The script includes a built-in calculation to handle "slippage." This ensures that even in highly volatile markets where prices move rapidly, the tool attempts to execute the closure at the most accurate price possible.
- Visual Feedback: While the script performs the actions in the background, it uses color designations (defaulted to Yellow) to help distinguish between different types of closing actions within the platform's logs.
In summary, this is a safety and efficiency tool. It allows a trader to "wipe the slate clean" for a specific asset instantly, ensuring that no lingering positions or pending orders remain active on that specific pair.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| CloseAllTrades.mq4 |
//| Copyright © 2005, Renato |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, rodrigokaus"
#property link "http://www.forex-tsd.com/expert-advisors-metatrader-4/703-100-pips.html"
extern color clCloseBuy = Yellow;
extern color clCloseSell = Yellow;
extern string Name_Expert = "CloseAllTrades";
void deinit() {
Comment("");
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start(){
CloseAllTrades();
return (0);
}
void CloseAllTrades() {
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol()) {
if (OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,GetSlippage(),clCloseBuy);
if (OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,GetSlippage(),clCloseSell);
if (OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT ||
OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)
OrderDelete(OrderTicket());
}
}
}
}
double GetSlippage() { return((Ask-Bid)/Point); }
Comments