Summary: Complete MT4 script that closes all open orders with one click. Shows real-time profit calculation, allows partial closing by magic number or comment filter. Safe and easy to use.




# 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



  • Close all market orders (buy and sell) with one click

  • Display total P/L before confirmation

  • Filter by magic number or custom comment

  • Option to close only buy orders or only sell orders

  • Works on any symbol or current symbol only


  • 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
  • Set CloseAllSymbols = true

  • Set ShowConfirmation = true

  • Drag script to any chart


  • Example 2: Close only sell orders on current chart
  • Set CloseAllSymbols = false

  • Set CloseBuys = false

  • Set CloseSells = true


  • Example 3: Close only orders from specific EA (magic number 12345)
  • Set FilterMagic = 12345

  • Set FilterComment = "" (empty)


  • Compilation Notes



  • Requires MT4 build 600 or higher

  • The script uses standard OrderClose function

  • For ECN brokers, increase Slippage parameter to 50-100

  • Script executes immediately and does not remain on chart


  • 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.*