Summary: Complete compilable MQL4 script to close all open orders instantly. Includes position filtering, partial close ratio, and profit/loss limits. Free download for forex traders.




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:
  • Close all open orders with one click

  • Filter by order type (buy/sell/both)

  • Partial close percentage option

  • Close only profitable or losing positions

  • Risk warning before execution


  • Parameters Explanation:
  • `CloseBuyOrders` – True to close buy orders (OP_BUY)

  • `CloseSellOrders` – True to close sell orders (OP_SELL)

  • `PartialClosePercent` – Percentage to close per order (0=100% full close)

  • `CloseOnlyProfitable` – True to close only positive profit positions

  • `CloseOnlyLoss` – True to close only negative profit positions

  • `ShowConfirmDialog` – Show confirmation message before execution


  • 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:
  • Set `PartialClosePercent=50` to close half of each position

  • Use `CloseOnlyProfitable=true` to lock in gains only

  • Disable confirmation dialog for faster execution (caution advised)


  • Reference: 自主编译 / Self-compiled utility script.

    For advanced semi-automated trading systems with GUI panels and risk management, check out our premium EA collection.