# One Click Close All Orders Script - Complete MQL4 Source Code
This utility script for MT4 allows traders to close all open orders instantly with a single click. It displays total profit and loss before execution and supports filtering by magic number or order comment.
Script Features
Complete MQL4 Code
```mql4
//+------------------------------------------------------------------+
//| CloseAll.mq4 |
//| Version 2.0 |
//| |
//+------------------------------------------------------------------+
#property copyright "Forex Tools Lab"
#property link ""
#property version "2.00"
#property strict
#property script_show_inputs
//--- Input parameters
input bool CloseAllSymbols = true; // Close orders on all symbols
input bool CloseBuys = true; // Close buy orders
input bool CloseSells = true; // Close sell orders
input int FilterMagic = 0; // Magic number filter (0=all)
input string FilterComment = ""; // Comment filter (empty=all)
input bool ShowConfirmation = true; // Show confirmation dialog
input int Slippage = 30; // Slippage in points
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
int closedCount = 0;
double totalProfit = 0;
int buyCount = 0, sellCount = 0;
double buyProfit = 0, sellProfit = 0;
// First pass: collect order information
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
continue;
// Check magic number filter
if(FilterMagic != 0 && OrderMagicNumber() != FilterMagic)
continue;
// Check comment filter
if(StringLen(FilterComment) > 0 && StringFind(OrderComment(), FilterComment) == -1)
continue;
// Check symbol filter
if(!CloseAllSymbols && OrderSymbol() != Symbol())
continue;
// Count by order type
if(OrderType() == OP_BUY)
{
buyCount++;
buyProfit += OrderProfit() + OrderSwap() + OrderCommission();
}
else if(OrderType() == OP_SELL)
{
sellCount++;
sellProfit += OrderProfit() + OrderSwap() + OrderCommission();
}
}
totalProfit = buyProfit + sellProfit;
// Show confirmation if enabled
if(ShowConfirmation)
{
string msg = "=== Order Summary ===\n";
msg += "Buy orders: " + IntegerToString(buyCount) + "\n";
msg += "Sell orders: " + IntegerToString(sellCount) + "\n";
msg += "Buy P/L: " + DoubleToStr(buyProfit, 2) + "\n";
msg += "Sell P/L: " + DoubleToStr(sellProfit, 2) + "\n";
msg += "Total P/L: " + DoubleToStr(totalProfit, 2) + "\n\n";
msg += "Close all orders?";
int response = MessageBox(msg, "Close All Orders", MB_YESNO | MB_ICONQUESTION);
if(response != IDYES)
{
Print("Operation cancelled by user");
return;
}
}
// Second pass: close orders
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
continue;
// Re-apply filters
if(FilterMagic != 0 && OrderMagicNumber() != FilterMagic)
continue;
if(StringLen(FilterComment) > 0 && StringFind(OrderComment(), FilterComment) == -1)
continue;
if(!CloseAllSymbols && OrderSymbol() != Symbol())
continue;
bool shouldClose = false;
if(OrderType() == OP_BUY && CloseBuys)
shouldClose = true;
if(OrderType() == OP_SELL && CloseSells)
shouldClose = true;
if(shouldClose)
{
double closePrice = (OrderType() == OP_BUY) ? Bid : Ask;
bool result = OrderClose(OrderTicket(), OrderLots(), closePrice, Slippage, clrRed);
if(result)
{
closedCount++;
Print("Closed order #", OrderTicket(), " | Profit: ", OrderProfit() + OrderSwap() + OrderCommission());
}
else
{
Print("Failed to close order #", OrderTicket(), " Error: ", GetLastError());
}
}
}
Print("=== Close All Script Finished ===");
Print("Total closed: ", closedCount, " orders");
Print("Total profit: ", DoubleToStr(totalProfit, 2));
MessageBox("Closed " + IntegerToString(closedCount) + " orders\nTotal P/L: " + DoubleToStr(totalProfit, 2), "Execution Complete", MB_OK);
}
//+------------------------------------------------------------------+
//| Get order type as string |
//+------------------------------------------------------------------+
string OrderTypeToString(int type)
{
switch(type)
{
case OP_BUY: return "BUY";
case OP_SELL: return "SELL";
case OP_BUYLIMIT: return "BUY LIMIT";
case OP_SELLLIMIT: return "SELL LIMIT";
case OP_BUYSTOP: return "BUY STOP";
case OP_SELLSTOP: return "SELL STOP";
default: return "UNKNOWN";
}
}
//+------------------------------------------------------------------+
```
Parameter Explanation
| Parameter | Description |
|-----------|-------------|
| CloseAllSymbols | True = close orders on all currency pairs, False = only current chart symbol |
| CloseBuys | Enable closing of buy orders (OP_BUY) |
| CloseSells | Enable closing of sell orders (OP_SELL) |
| FilterMagic | Only close orders with this magic number (0 = all magic numbers) |
| FilterComment | Only close orders containing this text in comment (empty = all) |
| ShowConfirmation | Display profit summary before closing |
| Slippage | Maximum allowed slippage in points when closing |
Installation Instructions
1. Copy the code into MetaEditor (F4 in MT4)
2. Click File > New > Script
3. Delete default code and paste the complete code above
4. Click Compile (F7) - verify no errors
5. In MT4 Navigator, find the script under "Scripts" folder
6. Drag the script onto any chart
7. Adjust input parameters in the popup window
8. Click OK to execute
Usage Examples
Example 1: Close all orders on all symbols with confirmation
Example 2: Close only sell orders on current chart
Example 3: Close only orders from specific EA (magic number 12345)
Compilation Notes
Safety Tips
Always test the script on a demo account first. The confirmation dialog provides an extra safety layer. Consider using FilterMagic to avoid closing orders from other EAs.
Reference
This script is independently compiled and tested. Based on standard MQL4 order management functions from MetaQuotes documentation.
*For more professional trading utilities including partial close tools, trailing stop managers, and automated risk management scripts, check our premium tools collection.*