This article provides a complete, compilable MQL4 script source code for one-click trade management. The script instantly closes all open orders or specific positions based on user-defined filters. Ideal for manual traders who need rapid exit during high volatility or before news events.
Tool Functions:
Parameters Explanation:
MQL4 Source Code:
```mql4
//+------------------------------------------------------------------+
//| OneClickCloseAllOrders.mq4 |
//| |
//| 自主编译 / Self-compiled |
//+------------------------------------------------------------------+
#property copyright "Forex Trading Tools"
#property link ""
#property version "1.00"
#property strict
//--- input parameters
input bool CloseBuyOrders = true;
input bool CloseSellOrders = true;
input int PartialClosePercent = 0; // 0=100% close, 1-99=partial %
input bool CloseOnlyProfitable = false;
input bool CloseOnlyLoss = false;
input bool ShowConfirmDialog = true;
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
// Confirmation dialog
if(ShowConfirmDialog)
{
int confirm = MessageBox("Are you sure you want to close selected orders?",
"One Click Close - Confirmation",
MB_YESNO | MB_ICONWARNING);
if(confirm != IDYES)
{
Print("Operation cancelled by user");
return;
}
}
int closedCount = 0;
int errorCount = 0;
double totalProfit = 0;
// Loop through all orders (reverse order for safe modification)
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
// Filter by order type
if(OrderType() == OP_BUY && !CloseBuyOrders) continue;
if(OrderType() == OP_SELL && !CloseSellOrders) continue;
// Filter by profit/loss
double orderProfit = OrderProfit() + OrderSwap() + OrderCommission();
if(CloseOnlyProfitable && orderProfit <= 0) continue;
if(CloseOnlyLoss && orderProfit >= 0) continue;
// Execute close or partial close
bool result = false;
if(PartialClosePercent > 0 && PartialClosePercent < 100)
{
result = PartialCloseOrder(OrderTicket(), PartialClosePercent);
}
else
{
result = CloseOrder(OrderTicket(), OrderLots());
}
if(result)
{
closedCount++;
totalProfit += orderProfit;
Print("Closed order #", OrderTicket(), " Profit: ", orderProfit);
}
else
{
errorCount++;
Print("Failed to close order #", OrderTicket(), " Error: ", GetLastError());
}
}
}
// Summary report
Print("========== Close Summary ==========");
Print("Total closed: ", closedCount);
Print("Failed: ", errorCount);
Print("Total profit: ", totalProfit);
Print("===================================");
if(closedCount > 0)
{
MessageBox("Closed " + string(closedCount) + " orders.\nTotal profit: " +
DoubleToString(totalProfit, 2) + "\nFailed: " + string(errorCount),
"Execution Complete", MB_OK | MB_ICONINFORMATION);
}
}
//+------------------------------------------------------------------+
//| Close full order function |
//+------------------------------------------------------------------+
bool CloseOrder(int ticket, double lots)
{
if(OrderSelect(ticket, SELECT_BY_TICKET))
{
int cmd = OrderType();
double price = (cmd == OP_BUY) ? Bid : Ask;
bool result = OrderClose(ticket, lots, price, 3, clrRed);
return result;
}
return false;
}
//+------------------------------------------------------------------+
//| Partial close order function |
//+------------------------------------------------------------------+
bool PartialCloseOrder(int ticket, int percent)
{
if(percent <= 0 || percent >= 100) return false;
if(OrderSelect(ticket, SELECT_BY_TICKET))
{
double currentLots = OrderLots();
double closeLots = NormalizeDouble(currentLots * percent / 100.0, 2);
if(closeLots < 0.01) closeLots = 0.01; // Minimum lot size
double remainingLots = NormalizeDouble(currentLots - closeLots, 2);
if(remainingLots < 0.01)
{
return CloseOrder(ticket, currentLots);
}
int cmd = OrderType();
double price = (cmd == OP_BUY) ? Bid : Ask;
bool result = OrderClose(ticket, closeLots, price, 3, clrBlue);
if(result)
{
Print("Partial closed ", closeLots, " / ", currentLots, " lots on order #", ticket);
}
return result;
}
return false;
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
```
Installation & Usage Guide:
1. Save as `OneClickCloseAllOrders.mq4` in `MQL4/Scripts/` folder
2. Press F5 in MetaEditor to compile
3. Drag script from Navigator onto any chart
4. Adjust parameters in the popup dialog
5. Confirm execution
Parameter Customization Tips:
Reference: 自主编译 / Self-compiled utility script.
For advanced semi-automated trading systems with GUI panels and risk management, check out our premium EA collection.