Error 10025 whit a partial TP order

 
#include <Trade\Trade.mqh>

CTrade trade;

input double PartialProfitTarget = 500.0; // Target profit in account currency for partial closure (TP1)
input double FullProfitTarget = 300.0; // Target profit for full closure (TP2)
input double PartialClosePercentage = 0.5; // Percentage of the position to close partially
input double BreakevenTrigger = 400.0; // Profit before TP1 to move SL to breakeven (BE)

bool isTP1Reached = false; // Flag to track if TP1 is reached

void OnTick() {
    for (int i = 0; i < PositionsTotal(); i++) {
        ulong ticket = PositionGetTicket(i);
        if (ticket == 0) continue; // Skip invalid positions
        
        string symbol = PositionGetString(POSITION_SYMBOL);
        if (symbol != _Symbol) continue; // Skip positions for different symbols
        
        double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
        double volume = PositionGetDouble(POSITION_VOLUME);
        double partialVolume = NormalizeDouble(volume * PartialClosePercentage, 2);
        double profit = PositionGetDouble(POSITION_PROFIT);
        double spread = (double)SymbolInfoInteger(_Symbol, SYMBOL_SPREAD);
        
        // Adjust for buy or sell
        bool isBuy = PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY;
        double breakevenPrice = openPrice;

        // Check for first TP (TP1) 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 (BE) before reaching TP1
        if (!isTP1Reached && profit >= BreakevenTrigger && profit < PartialProfitTarget) {
            double currentSL = PositionGetDouble(POSITION_SL);
            if (!IsEqual(breakevenPrice, currentSL)) { // Check if the proposed breakeven SL is different from the current SL
                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 (TP2)
        if (isTP1Reached && profit >= FullProfitTarget) {
            if (!trade.PositionClose(ticket)) {
                Print("PositionClose error ", trade.ResultRetcode());
            } else {
                Print("Full position closed at TP2");
            }
        }
    }
}

// Function to compare double values with a small tolerance
bool IsEqual(const double value1, const double value2) {
    const double tolerance = 0.00001; // Adjust tolerance as needed
    return MathAbs(value1 - value2) < tolerance;
}

Hi guys,
i need your help to solve a problem with the 10025 error when i try to get a partial TP.

i am not good with mql5 so i adapted a basic script i found here with the help of chatgpt.

The goal is to get a partial TP when a gain is reached, move the SL to be and then set another tp for closing the entire position.

So i used 4 variables:
gain for TP 1,
percentage of the position to clos in TP 1
gain for TP 2
when move the SL to BE

Testing it i noticed it generate an error (10025) when try to get the TP1 and, for this reason, the position is closed at TP 2

i really appreciate if someone could help me to solve this problem

Thank you so much
r.

 

Unfortunately, as often happens, AI codes are full of problems and not consistent. First of all, if you have more than 1 trade opened, it will not work correctly for all trades.

I strongely suggest to read documentation, use CTrade classes codes (that you only declared but never used in your code), and to use AI the less possible times.

Anyway, the procedure should be:

  1. Cycles all your positions (filter by magic or symbol if you need)
  2. Ignore trades that have SL in profit or at PriceOpen 
  3. Check if PriceCurrent compared to PriceOpen of each position is >= of your "PartialTargetProfit"
  4. If 3 is true, do the partial closure and move SL to BE.
PS. "FullProfitTarget" have no sense to exists, use TP instead. If you really want to use it for any reason, set at least the Target profit for TP2 higher than target profit for TP1, otherwise the partial closue (TP1) will never happens and your trade will always fully closed by "TP2"
 
I strongly recommend consulting the documentation thoroughly and utilizing the CTrade class codes, which you've declared but not utilized in your code. Additionally, minimize reliance on AI as much as possible.

Here's a suggested approach:

Iterate through all your positions (filter by magic number or symbol if necessary).
Disregard trades that have a stop loss (SL) in profit or at the price open.
Compare the current price to the open price of each position. If the current price is greater than or equal to your "PartialTargetProfit":
a. Perform a partial closure.
b. Move the stop loss to breakeven.
P.S. The existence of "FullProfitTarget" is redundant; instead, use "Take Profit" (TP). If you insist on retaining it, ensure that the target profit for TP2 is higher than that for TP1. Otherwise, the partial closure (TP1) will never occur, and your trades will always be fully closed by "TP2".

 
h_aslani #:
I strongly recommend consulting the documentation thoroughly and utilizing the CTrade class codes, which you've declared but not utilized in your code. Additionally, minimize reliance on AI as much as possible.

Here's a suggested approach:

Iterate through all your positions (filter by magic number or symbol if necessary).
Disregard trades that have a stop loss (SL) in profit or at the price open.
Compare the current price to the open price of each position. If the current price is greater than or equal to your "PartialTargetProfit":
a. Perform a partial closure.
b. Move the stop loss to breakeven.
P.S. The existence of "FullProfitTarget" is redundant; instead, use "Take Profit" (TP). If you insist on retaining it, ensure that the target profit for TP2 is higher than that for TP1. Otherwise, the partial closure (TP1) will never occur, and your trades will always be fully closed by "TP2".

Have you copy and paste my message into an AI to write your own based on my information? Good job man
 
Fabio Cavalloni #:

Unfortunately, as often happens, AI codes are full of problems and not consistent. First of all, if you have more than 1 trade opened, it will not work correctly for all trades.

I strongely suggest to read documentation, use CTrade classes codes (that you only declared but never used in your code), and to use AI the less possible times.

Anyway, the procedure should be:

  1. Cycles all your positions (filter by magic or symbol if you need)
  2. Ignore trades that have SL in profit or at PriceOpen 
  3. Check if PriceCurrent compared to PriceOpen of each position is >= of your "PartialTargetProfit"
  4. If 3 is true, do the partial closure and move SL to BE.
PS. "FullProfitTarget" have no sense to exists, use TP instead. If you really want to use it for any reason, set at least the Target profit for TP2 higher than target profit for TP1, otherwise the partial closue (TP1) will never happens and your trade will always fully closed by "TP2"
hi fabio, and thank you so much for your help.

I know that i need the help of a professionist, but in this moment i'm just "playing" with demo accounts to test notions i'm studying.

This is the reason why i asked help to chatgpt.

i will try to improve your advices

thank you so much!
r.
 
Raffele Montillo #: This is the reason why i asked help to chatgpt.

Stop using ChatGPT.
          Help needed to debug and fix an AI EA - Trading Systems - MQL5 programming forum #2 (2023)

ChatGPT (the worst), “Bots Builder”, “EA builder”, “EA Builder Pro”, EATree, “Etasoft forex generator”, “Forex Strategy Builder”, ForexEAdvisor (aka. ForexEAdvisor STRATEGY BUILDER, and Online Forex Expert Advisor Generator), ForexRobotAcademy.com, forexsb, “FX EA Builder”, fxDreema, Forex Generator, FxPro, “LP-MOBI”, Molanis, “Octa-FX Meta Editor”, Strategy Builder FX, “Strategy Quant”, “Visual Trader Studio”, “MQL5 Wizard”, etc., are all the same. You will get something quick, but then you will spend a much longer time trying to get it right, than if you learned the language up front, and then just wrote it.

Since you haven't learned MQL4/5, therefor there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.

We are willing to HELP you when you post your attempt (using Code button) and state the nature of your problem, but we are not going to debug your hundreds of lines of code. You are essentially going to be on your own.

ChatGPT
  1. Even it says do not use it for coding. * 
  2. Mixing MT4 and MT5 code together.
  3. Creating multiple OnCalculate/OnTick functions.
  4. OnCalculate returning a double.
  5. Filling buffers with zero in OnInit (they have no size yet). Setting buffer elements to zero but not setting Empty Value to correspond.
  6. Calling undefined functions.
  7. Calling MT4 functions in MT5 code.
  8. Sometimes, not using strict (MT4 code).
  9. Code that will not compile.
  10. Creating code outside of functions. * 
  11. Creating incomplete code. * 
  12. Initialization of Global variables with non-constants. * 
  13. Assigning a MT5 handle to a double or missing the buffer and bar indexes in a MT4 call. * 
  14. Useing MT4 Trade Functions without first selecting an order. * 
  15. Uses NULL in OrderSend. * 
bot builder Creating two OnInit() functions. * 
EA builder
  1. Counting up while closing multiple orders.
  2. Not useing time in new bar detection.
  3. Not adjusting for 4/5 digit brokers, TP/SL and slippage. * 
  4. Not checking return codes.
EATree Uses objects on chart to save values — not persistent storage (files or GV+Flush.) No recovery (crash/power failure.)
ForexEAdvisor
  1. Non-updateing global variables.
  2. Compilation errors.
  3. Not checking return codes.
  4. Not reporting errors.
FX EA Builder
  1. Not checking return codes.
  2. Loosing open tickets on terminal restart. No recovery (crash/power failure.)
  3. Not adjusting stops for the spread. * 
  4. Using OrdersTotal directly.
  5. Using the old event handlers.
 
William Roeder #:

Stop using ChatGPT.
          Help needed to debug and fix an AI EA - Trading Systems - MQL5 programming forum #2 (2023)

ChatGPT (the worst), “Bots Builder”, “EA builder”, “EA Builder Pro”, EATree, “Etasoft forex generator”, “Forex Strategy Builder”, ForexEAdvisor (aka. ForexEAdvisor STRATEGY BUILDER, and Online Forex Expert Advisor Generator), ForexRobotAcademy.com, forexsb, “FX EA Builder”, fxDreema, Forex Generator, FxPro, “LP-MOBI”, Molanis, “Octa-FX Meta Editor”, Strategy Builder FX, “Strategy Quant”, “Visual Trader Studio”, “MQL5 Wizard”, etc., are all the same. You will get something quick, but then you will spend a much longer time trying to get it right, than if you learned the language up front, and then just wrote it.

Since you haven't learned MQL4/5, therefor there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.

We are willing to HELP you when you post your attempt (using Code button) and state the nature of your problem, but we are not going to debug your hundreds of lines of code. You are essentially going to be on your own.



hi William, you are right..
for me, now, it is impossible start to learn to code..
it is not matter of 2 or 3 days

i'm trying to find something that "could be good enough" untill i will study how trading works..

if i will start to operate with a real account, i will hire a professionist who will write the code i need

thank you for your advices
regards
r.

 
Raffele Montillo:

Hi guys,
i need your help to solve a problem with the 10025 error when i try to get a partial TP.

i am not good with mql5 so i adapted a basic script i found here with the help of chatgpt.

The goal is to get a partial TP when a gain is reached, move the SL to be and then set another tp for closing the entire position.

So i used 4 variables:
gain for TP 1,
percentage of the position to clos in TP 1
gain for TP 2
when move the SL to BE

Testing it i noticed it generate an error (10025) when try to get the TP1 and, for this reason, the position is closed at TP 2

i really appreciate if someone could help me to solve this problem

Thank you so much
r.

Just learn the MQL language or C++, syntaxes are not much different, best would be to get started with Python, its quite easy to catch on, Just code it yourself. Never use Chat Gpt for coding, its a mixup, it will not generate code correctly, also do not use generators. All of us here that are coders has tried all those tricks before, we were there.