hi guys i need your help.
i have to admit that for me the mql5 is something really hard to understand.
for this reason i found some code on this forum and i tried to adapt it to my needs with the help of chatgpt..
but it was really frustrated.
so, for this reason i'm writing now for ask your help.
So, i'd like to use this script in a way that, when it is active, can read all my active orders (so no pending orders) and can manage them in this way:
setting 2 TP for 2 different closes: first tp should be a partial close of the 50% of the volume of the open orders
second TP shloud be the totally of the order that remains
the 2 levels for tp should regard just the profit of the order, and not the price, or ticks, or pips or anything else
because i qould use the script when different orders on different pairs/commodities are active
at the same time, when TP1 is triggered also the stoploss could move to breakeven.
this is what i obtained for now (and it does not work)
i hope you could help me to develop what i need:
regards
#include <Trade\Trade.mqh> CTrade trade; input double PartialProfitTarget = 250.0; // Target profit in account currency for partial closure input double FullProfitTarget = 500.0; // Target profit for full closure input double PartialClosePercentage = 0.5; // Percentage of the position to close partially void OnTick() { for (int i = 0; i < PositionsTotal(); i++) { ulong ticket = PositionGetTicket(i); if (!PositionSelectByTicket(ticket)) continue; // Ensure we can select the position double openPrice = PositionGetDouble(POSITION_PRICE_OPEN); double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID); // For sell positions, use SYMBOL_ASK for buys double volume = PositionGetDouble(POSITION_VOLUME); double partialVolume = NormalizeDouble(volume * PartialClosePercentage, 2); double profit = PositionGetDouble(POSITION_PROFIT); // Adjust for buy or sell bool isBuy = PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY; double breakevenPrice = isBuy ? openPrice + SymbolInfoDouble(_Symbol, SYMBOL_SPREAD) * _Point : openPrice - SymbolInfoDouble(_Symbol, SYMBOL_SPREAD) * _Point; // Check for first TP and partial close if (profit >= PartialProfitTarget && volume > partialVolume) { if (!trade.PositionClosePartial(ticket, partialVolume)) { Print("PositionClosePartial error ", trade.ResultRetcode()); } else if (!trade.PositionModify(ticket, breakevenPrice, 0)) { // Move SL to breakeven Print("PositionModify error ", trade.ResultRetcode()); } } // Check for full closure at second TP if (profit >= FullProfitTarget) { if (!trade.PositionClose(ticket)) { Print("PositionClose error ", trade.ResultRetcode()); } } } }
i will test it with a demo account in the next few hours and i will give you a feedback
in any case, really thank you sooo much!
r.
#include <Trade\Trade.mqh> CTrade trade; input double PartialProfitTarget = 250.0; // Target profit in account currency for partial closure input double FullProfitTarget = 150.0; // Target profit for full closure input double PartialClosePercentage = 0.7; // Percentage of the position to close partially input double BreakevenTrigger = 200.0; // Profit before TP1 to move SL to breakeven bool isTP1Reached = false; // Flag to track if TP1 is reached void OnTick() { for (int i = 0; i < PositionsTotal(); i++) { ulong ticket = PositionGetTicket(i); if (!PositionSelectByTicket(ticket)) continue; // Ensure we can select the position double openPrice = PositionGetDouble(POSITION_PRICE_OPEN); double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID); // For sell positions, use SYMBOL_ASK for buys double volume = PositionGetDouble(POSITION_VOLUME); double partialVolume = NormalizeDouble(volume * PartialClosePercentage, 2); double profit = PositionGetDouble(POSITION_PROFIT); // Adjust for buy or sell bool isBuy = PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY; double breakevenPrice = isBuy ? openPrice + SymbolInfoInteger(_Symbol, SYMBOL_SPREAD) * _Point : openPrice - SymbolInfoInteger(_Symbol, SYMBOL_SPREAD) * _Point; // Check for first TP and partial close if (!isTP1Reached && profit >= PartialProfitTarget && volume > partialVolume) { if (!trade.PositionClosePartial(ticket, partialVolume)) { Print("PositionClosePartial error ", trade.ResultRetcode()); } else if (!trade.PositionModify(ticket, breakevenPrice, 0)) { // Move SL to breakeven Print("PositionModify error ", trade.ResultRetcode()); } else { Print("Partial close executed, Breakeven SL set"); } isTP1Reached = true; // Set TP1 reached flag to true } // Check for breakeven before reaching TP1 if (!isTP1Reached && profit >= BreakevenTrigger && profit < PartialProfitTarget) { if (!trade.PositionModify(ticket, breakevenPrice, 0)) { // Move SL to breakeven Print("PositionModify error ", trade.ResultRetcode()); } else { Print("Breakeven SL set"); } } // Check for full closure at second TP if (isTP1Reached && profit >= FullProfitTarget) { if (!trade.PositionClose(ticket)) { Print("PositionClose error ", trade.ResultRetcode()); } else { Print("Full position closed at TP2"); } } } }hi, i tested your script and i found it doesn't work properly..
but the probelm was the logic i asked:
the setting consider that afetr TP1 a part of position will be closed. For this reason the TP2 should be less then TP1.
But this means that the script will close all the position when the gain reach the TP2 level, and not the TP1.
Now i applied some edits with the help of Chatgpt and i'm testing the new version:

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
hi guys i need your help.
i have to admit that for me the mql5 is something really hard to understand.
for this reason i found some code on this forum and i tried to adapt it to my needs with the help of chatgpt..
but it was really frustrated.
so, for this reason i'm writing now for ask your help.
So, i'd like to use this script in a way that, when it is active, can read all my active orders (so no pending orders) and can manage them in this way:
setting 2 TP for 2 different closes: first tp should be a partial close of the 50% of the volume of the open orders
second TP shloud be the totally of the order that remains
the 2 levels for tp should regard just the profit of the order, and not the price, or ticks, or pips or anything else
because i qould use the script when different orders on different pairs/commodities are active
at the same time, when TP1 is triggered also the stoploss could move to breakeven.
this is what i obtained for now (and it does not work)
i hope you could help me to develop what i need:
thank you so much
regards