Close ALL
Download scriptThis script is designed as an automated utility for the MetaTrader 4 platform, specifically built to provide traders with a "panic button" or a quick way to liquidate all active positions simultaneously. Instead of manually closing every open trade one by one—which can be time-consuming and risky in fast-moving markets—this tool handles the entire process instantly.
How the Logic Works:
1. Scanning Your Portfolio When the script is activated, it first performs a comprehensive scan of your account to identify every active order currently held on the market. It counts the total number of open positions to ensure nothing is overlooked.
2. Systematic Review The tool then works backward through your list of open trades. By starting from the most recent and moving toward the oldest, it ensures that the script maintains a stable connection to each individual trade as it processes them, preventing any errors that might occur if it skipped around the list.
3. Intelligent Identification For every position it finds, the script automatically detects what type of trade it is—specifically identifying whether you are currently in a "Buy" (Long) position or a "Sell" (Short) position.
4. Execution of the Close Command Once a trade type is identified, the script takes the necessary action to exit the market:
- For Buy Positions: It identifies the current selling price and sends an immediate instruction to close the trade.
- For Sell Positions: It identifies the current buying price and sends an immediate instruction to close the trade.
By automating these specific actions, the script ensures that every open trade is closed at the best available market price at that exact micro-second, providing a clean exit from all market exposure in a single execution.
//+------------------------------------------------------------------+
//| 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 );
}
if(result == false)
{
}
}
return(0);
}
Comments