Summary: This article provides a complete, compilable MQL4 script source code for instantly closing all orders on MT4. It includes options to filter by order type (buy/sell/pending) and magic number, with full parameter explanations.




This tutorial provides a production-ready MT4 script source code that closes all open positions and pending orders with a single click. Manual traders and EA users alike need this utility to manage risk instantly during high volatility or before news events.

Why a One-Click Close Script?


MT4's default interface requires multiple clicks to close each order. This script reduces that to one drag-and-drop action, closing all or selected orders in milliseconds. It's an essential free EA download for every trader's toolkit.

Complete MQL4 Source Code


```cpp
//+------------------------------------------------------------------+
//| CloseAllOrders_V1.mq4 |
//| Generated by AutoCompile AI |
//| |
//+------------------------------------------------------------------+
#property copyright "AutoCompile AI"
#property link ""
#property version "1.00"
#property strict
#property script_show_inputs

//--- Input parameters
input bool CloseMarketBuys = true; // Close Market Buy Positions
input bool CloseMarketSells = true; // Close Market Sell Positions
input bool ClosePendingBuys = true; // Close Buy Limit/Stop Orders
input bool ClosePendingSells = true; // Close Sell Limit/Stop Orders
input int MagicFilter = -1; // Magic Number Filter (-1 = all)
input bool ShowConfirmDialog = true; // Show confirmation before closing

//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
int totalOrders = OrdersTotal();
if(totalOrders == 0)
{
Print("No orders found to close.");
return;
}

string confirmMsg = "Close ";
int count = 0;
for(int i=0; i {
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(MagicFilter != -1 && OrderMagicNumber() != MagicFilter)
continue;

int type = OrderType();
if(type == OP_BUY && CloseMarketBuys) count++;
else if(type == OP_SELL && CloseMarketSells) count++;
else if((type == OP_BUYLIMIT || type == OP_BUYSTOP) && ClosePendingBuys) count++;
else if((type == OP_SELLLIMIT || type == OP_SELLSTOP) && ClosePendingSells) count++;
}
}

if(count == 0)
{
Print("No orders match the selected filters.");
return;
}

if(ShowConfirmDialog && MessageBox("Close " + IntegerToString(count) + " order(s)?",
"Confirm Close", MB_YESNO | MB_ICONQUESTION) != IDYES)
{
Print("Close operation cancelled by user.");
return;
}

int closed = 0;
int errors = 0;

for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(MagicFilter != -1 && OrderMagicNumber() != MagicFilter)
continue;

int type = OrderType();
bool shouldClose = false;

if(type == OP_BUY && CloseMarketBuys)
shouldClose = true;
else if(type == OP_SELL && CloseMarketSells)
shouldClose = true;
else if((type == OP_BUYLIMIT || type == OP_BUYSTOP) && ClosePendingBuys)
shouldClose = true;
else if((type == OP_SELLLIMIT || type == OP_SELLSTOP) && ClosePendingSells)
shouldClose = true;

if(shouldClose)
{
bool result = OrderClose(OrderTicket(), OrderLots(),
OrderClosePrice(), 100, clrRed);
if(result)
closed++;
else
{
errors++;
Print("Failed to close order #", OrderTicket(),
" Error: ", GetLastError());
}
}
}
}

Print("Close completed: ", closed, " closed, ", errors, " errors.");
}
//+------------------------------------------------------------------+
```

How to Compile and Use


1. Save the code as `CloseAllOrders_V1.mq4` in the `Scripts` folder of your MT4 data directory.
2. Open MetaEditor, compile (F7), and ensure no errors.
3. Drag the script from the Navigator panel onto any chart.
4. A confirmation dialog (if enabled) will show the number of orders to close.

Parameter Explanation


  • CloseMarketBuys/CloseMarketSells: Close existing market buy/sell positions.

  • ClosePendingBuys/ClosePendingSells: Close Buy Limit, Buy Stop, Sell Limit, Sell Stop pending orders.

  • MagicFilter: Only close orders with a specific EA magic number. Use -1 for all orders.

  • ShowConfirmDialog: Prevents accidental execution with a popup confirmation.


  • Usage Tips


  • This MT4 script source code works instantly and does not need to remain on the chart.

  • For partial closing, adjust the boolean inputs to filter specific order types.

  • Combine with a hotkey manager for one-key emergency closing.


  • For traders needing automated position management with advanced logic (trailing stops, break-evens, partial closes), our premium EA Management Suite offers complete solutions. Subscribe for more free tools and updates.

    Reference: AutoCompile AI - Original MQL4 implementation, 2025.