Close all open positions_v1
Download scriptThis script is designed as a high-level management tool for MetaTrader 4 users who need to quickly exit all active market positions simultaneously. Instead of manually closing each trade one by one, this tool automates the process to ensure a clean slate on your trading terminal.
How It Works
The logic follows a systematic "sweep" of your active account:
- Comprehensive Scanning: When triggered, the script identifies every single open order currently active in your account. It systematically moves through these orders one by one to evaluate their status.
- Smart Recognition: The tool distinguishes between different types of market positions—specifically identifying "Buy" and "Sell" orders.
- Execution Logic:
- For every Buy position identified, the script automatically executes a closing command at the current best available market price.
- For every Sell position identified, it performs the same action, ensuring that both long and short positions are liquidated.
- Error Handling & Safety: The script includes a built-in verification step. If any specific trade fails to close due to a connection issue or market fluctuation, the system will provide an immediate on-screen alert notification. It then pauses briefly before attempting to continue, ensuring the process remains stable and informative.
Summary for the Trader
Think of this as a "Panic Button" or a "Finalize Position" tool. It is designed for situations where you need to move out of all current markets instantly—whether that is due to a significant change in market conditions, a desire to lock in profits across multiple trades, or a need to clear your dashboard for a new strategy. It handles the tedious work of price checking and individual order execution, providing a streamlined way to manage your portfolio.
/*-----------------------------+
| |
| 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 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)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}
return(0);
}
Comments