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: