Manage take profit and trailing

Dmitrij1

Trader
Oct 8, 2025
2
0
6
27
Hello, I'm creating my own EA but I've encountered a problem, maybe you know how to solve it. I want to have 3 Take Profits with 1 open position, so far I've only set 2 perfectly, I have 3 TP function but it doesn't work because it automatically closes TP2 for the rest of the position, I have a script you can check out, maybe you have an idea:
MQL5:
void ManageTakeProfitsAndTrailing()
{
    int totalPositions = PositionsTotal();
 
    for(int i = 0; i < totalPositions; i++)
    {
        ulong ticket = PositionGetTicket(i);
        if(ticket == 0 || !PositionSelectByTicket(ticket))
            continue;
 
        if(PositionGetString(POSITION_SYMBOL) != _Symbol)
            continue;
 
        string comment   = PositionGetString(POSITION_COMMENT);
        double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
        double sl        = PositionGetDouble(POSITION_SL);
        double tp        = PositionGetDouble(POSITION_TP);
        double vol       = PositionGetDouble(POSITION_VOLUME);
        double originalVol = vol; // Store the original volume
        double bid       = SymbolInfoDouble(_Symbol, SYMBOL_BID);
        double ask       = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
 
        double risk = (comment == "Buy") ? openPrice - sl : sl - openPrice;
        if(risk <= 0) continue;
 
        // Define TP levels
        double tp1 = (comment == "Buy") ? openPrice + risk * TP1_RR : openPrice - risk * TP1_RR;
        double tp2 = (comment == "Buy") ? openPrice + risk * TP2_RR : openPrice - risk * TP2_RR;
        double tp3 = (comment == "Buy") ? openPrice + risk * TP3_RR : openPrice - risk * TP3_RR;
 
        // Create unique magic comment key for this trade
        string key = comment + "_" + IntegerToString((int)openPrice);
 
        double closedVol = 0; // Store the closed volume
 
        // --- BUY CASE ---
        if(comment == "Buy")
        {
            if(bid >= tp1 && GlobalVariableCheck(key + "_tp1") == false)
            {
                GlobalVariableSet(key + "_tp1", 1);
                double lot_tp1 = originalVol * (TP1_Percent / 100.0);
                trade.PositionClosePartial(ticket, lot_tp1);
                closedVol += lot_tp1;
                Print("TP1 reached, closed ", lot_tp1, " lots at price ", bid);
                if(EnableSLtoBE && BE_Trigger <= 1)
                    trade.PositionModify(ticket, openPrice + SLtoBEPips * _Point, tp);
            }
            if(bid >= tp2 && GlobalVariableCheck(key + "_tp2") == false)
            {
                GlobalVariableSet(key + "_tp2", 1);
                double lot_tp2 = originalVol * (TP2_Percent / 100.0);
                double remainingVol = PositionGetDouble( POSITION_VOLUME);
                if(lot_tp2 > remainingVol)
                {
                    lot_tp2 = remainingVol;
                    Print("Adjusting lot_tp2 to remaining volume: ", remainingVol);
                }
                trade.PositionClosePartial(ticket, lot_tp2);
                closedVol += lot_tp2;
                Print("TP2 reached, closed ", lot_tp2, " lots at price ", bid);
                if(EnableSLtoBE && BE_Trigger <= 2)
                    trade.PositionModify(ticket, openPrice + SLtoBEPips * _Point, tp);
            }
            if(bid >= tp3 && GlobalVariableCheck(key + "_tp3") == false)
            {
                Print("TP3 logic reached. bid: ", bid, " tp3: ", tp3);
                GlobalVariableSet(key + "_tp3", 1);
                double remainingVol = PositionGetDouble(POSITION_VOLUME);
                Print("Closing remaining volume at TP3: ", remainingVol);
                trade.PositionClose(ticket);
                Print("TP3 reached, closed remaining lots at price ", bid);
            }
        }
 
        // --- SELL CASE ---
        if(comment == "Sell")
        {
            if(ask <= tp1 && GlobalVariableCheck(key + "_tp1") == false)
            {
                GlobalVariableSet(key + "_tp1", 1);
                double lot_tp1 = originalVol * (TP1_Percent / 100.0);
                trade.PositionClosePartial(ticket, lot_tp1);
                closedVol += lot_tp1;
                Print("TP1 reached, closed ", lot_tp1, " lots at price ", ask);
                if(EnableSLtoBE && BE_Trigger <= 1)
                    trade.PositionModify(ticket, openPrice - SLtoBEPips * _Point, tp);
            }
            if(ask <= tp2 && GlobalVariableCheck(key + "_tp2") == false)
            {
                GlobalVariableSet(key + "_tp2", 1);
                double lot_tp2 = originalVol * (TP2_Percent / 100.0);
                double remainingVol = PositionGetDouble(POSITION_VOLUME);
                if(lot_tp2 > remainingVol)
                {
                    lot_tp2 = remainingVol;
                    Print("Adjusting lot_tp2 to remaining volume: ", remainingVol);
                }
                trade.PositionClosePartial(ticket, lot_tp2);
                closedVol += lot_tp2;
                Print("TP2 reached, closed ", lot_tp2, " lots at price ", ask);
                if(EnableSLtoBE && BE_Trigger <= 2)
                    trade.PositionModify(ticket, openPrice - SLtoBEPips * _Point, tp);
            }
            if(ask <= tp3 && GlobalVariableCheck(key + "_tp3") == false)
            {
                Print("TP3 logic reached. ask: ", ask, " tp3: ", tp3);
                GlobalVariableSet(key + "_tp3", 1);
                double remainingVol = PositionGetDouble(POSITION_VOLUME);
                Print("Closing remaining volume at TP3: ", remainingVol);
                trade.PositionClose(ticket);
                Print("TP3 reached, closed remaining lots at price ", ask);
            }
        }
    }
}
 
Last edited by a moderator:
What was the output in the Experts and Journal tabs when that happened?
What are the values for TP1_Percent, TP2_Percent, TP1_RR, TP2_RR, TP3_RR, BE_Trigger variables?
You do realize that a script in MT5 is a program that runs only once when you launch it and then it gets detached from the chart?
 
What was the output in the Experts and Journal tabs when that happened?
What are the values for TP1_Percent, TP2_Percent, TP1_RR, TP2_RR, TP3_RR, BE_Trigger variables?
You do realize that a script in MT5 is a program that runs only once when you launch it and then it gets detached from the chart?



Output was the EA closing at TP1-50% at TP2-50%. But my idea to get TP1-50% TP2-30% TP3-20%,
Yes, know then it runs only once. AndTP1_RR, TP2_RR, TP3_RR : 0.5, 1, 1.5 RR