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
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
Save the code as Auto_Close_Profits_Target.mq4 in the Scripts` folder of your MT4 data directory.Parameter Explanation
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.