Summary: This article provides a complete MQL4 script that automatically closes all profitable trades when a target profit level is reached. Includes parameters for money or pip target, partial close options, and push notifications.




Manual profit taking can cost traders due to slippage or delay. This MT4 auto close profits script runs as a simple utility tool, monitoring your account in real-time. Once the specified profit target in dollars, pips, or percentage is hit, it instantly closes all profitable positions or only the most profitable one.

Use Cases


  • Close all winning trades when daily profit hits $500.

  • Lock in gains on a basket of signals.

  • Combine with a trailing stop script for a complete exit strategy.


  • Complete MQL4 Source Code


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

    //--- Input Parameters
    input double TargetProfitUSD = 100.0; // Target profit in USD (0=disabled)
    input double TargetPips = 0.0; // Target profit in pips (0=disabled)
    input double TargetPercent = 0.0; // Target profit as % of balance (0=disabled)
    input bool CloseOnlyProfitable = true; // Close only profitable positions
    input bool ClosePartial = false; // Close partial lots (if true, closes 50% of each)
    input int Slippage = 30; // Allowed slippage in points
    input bool SendPushNotification = true; // Send push notification when triggered
    input bool AlertOnScreen = true; // Show alert on screen

    //+------------------------------------------------------------------+
    //| Script program start function |
    //+------------------------------------------------------------------+
    void OnStart()
    {
    double currentProfit = 0;
    double currentPipsProfit = 0;
    double currentPercentProfit = 0;
    double accountBalance = AccountBalance();

    //--- Calculate current profit in USD
    for(int i = OrdersTotal() - 1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderType() == OP_BUY || OrderType() == OP_SELL)
    {
    currentProfit += OrderProfit() + OrderSwap() + OrderCommission();
    //--- Calculate pips profit
    if(OrderType() == OP_BUY)
    currentPipsProfit += (Bid - OrderOpenPrice()) / Point;
    else if(OrderType() == OP_SELL)
    currentPipsProfit += (OrderOpenPrice() - Ask) / Point;
    }
    }
    }

    currentPercentProfit = (currentProfit / accountBalance) * 100.0;

    //--- Check if target is reached
    bool targetReached = false;
    string reason = "";

    if(TargetProfitUSD > 0 && currentProfit >= TargetProfitUSD)
    {
    targetReached = true;
    reason = StringFormat("USD target $%.2f reached", TargetProfitUSD);
    }
    else if(TargetPips > 0 && currentPipsProfit >= TargetPips)
    {
    targetReached = true;
    reason = StringFormat("Pips target %.1f pips reached", TargetPips);
    }
    else if(TargetPercent > 0 && currentPercentProfit >= TargetPercent)
    {
    targetReached = true;
    reason = StringFormat("Percent target %.2f%% of balance reached", TargetPercent);
    }

    if(!targetReached)
    {
    if(AlertOnScreen) Alert("Profit target not reached. Current profit: $", currentProfit);
    Print("Auto Close Script: Target not met. Exiting.");
    return;
    }

    //--- Send alerts
    if(AlertOnScreen)
    Alert("Auto Close: ", reason, " Closing profitable trades...");
    if(SendPushNotification)
    SendNotification("Auto Close Script: " + reason);

    //--- Close positions
    int closedCount = 0;
    for(int i = OrdersTotal() - 1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    bool isProfitable = (OrderProfit() + OrderSwap() + OrderCommission()) > 0;
    if(CloseOnlyProfitable && !isProfitable)
    continue;

    if(ClosePartial)
    {
    //--- Close half of the lot size
    double currentLots = OrderLots();
    double halfLots = NormalizeDouble(currentLots / 2.0, 2);
    if(halfLots < MarketInfo(OrderSymbol(), MODE_MINLOT))
    halfLots = currentLots; // Close whole if half is too small

    bool closed = OrderClose(OrderTicket(), halfLots, OrderClosePrice(), Slippage, clrNONE);
    if(closed) closedCount++;
    }
    else
    {
    //--- Close full position
    bool closed = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, clrNONE);
    if(closed) closedCount++;
    }
    }
    }

    Print("Auto Close Script completed. Closed ", closedCount, " positions. ", reason);
    if(AlertOnScreen)
    Alert("Auto Close Complete: Closed ", closedCount, " positions.");
    }
    //+------------------------------------------------------------------+
    ```

    How to Compile and Use


    1. Save the code as `Auto_Close_Profits_Target.mq4` in the `Scripts` folder of your MT4 data directory.
    2. Open MetaEditor (F4), compile (F7), and ensure zero errors.
    3. Drag the script onto any chart. The input window appears – set your target profit in USD, pips, or percentage.
    4. The script runs once, checks current total profit, and closes trades if the target is met.

    Parameter Explanation


  • TargetProfitUSD: Close when floating + closed profit reaches this USD amount.

  • TargetPips: Aggregate pips profit target across all open positions.

  • TargetPercent: Target as percentage of current account balance.

  • CloseOnlyProfitable: If true, only closing positions that are currently in profit.

  • ClosePartial: If true, closes 50% of each position instead of full.

  • Slippage: Maximum allowed slippage for close orders.

  • SendPushNotification: Requires MT4 mobile app and configured MetaQuotes ID.


  • This free EA tool download provides a no-nonsense profit management solution. For advanced risk management including breakeven, trailing stops, and time-based exits, consider our premium Trade Manager EA – subscribe to our channel for weekly script releases.

    Reference: AutoCompile AI - Original MQL4 implementation for utility scripts, 2025.