EA - Modify a pending order's take profit when another order is open

 
Hello,

I can't seem to get it right, I have an EA that put two pending orders per day (and reset everyday) on different levels (a buy and a sell).
When one of those two is opened, I would like that the remaining one update is take profit to double the initial one (this is in case first trade goes to stop loss).
But I'm getting A LOT of trouble with this update...
Here is what I've tried so far :
- A function places the two pending orders
- Another one check which OrderType() got filled and call for the update function where everything goes south.
This is the part where nothing work (cause yeah error 130) :
...
int filledTradeType = OrderType();
UpdateSecondPendingTradeTP(filledTradeType);
...

void UpdateSecondPendingTradeTP(int filledTradeType)
{
    double newTakeProfit = 2 * takeProfit * Point; // Double the take profit*
    
    if (filledTradeType == OP_BUY)
    {
        //Print("MODIFY SELL old tp:" , takeProfit, ", new tp:" ,newTakeProfit);
        ModifyTakeProfit(sellStopOrder, newTakeProfit);
    }
    else if (filledTradeType == OP_SELL)
    {
        //Print("MODIFY BUY old tp:" , takeProfit, ", new tp:" ,newTakeProfit);
        ModifyTakeProfit(buyStopOrder, newTakeProfit);
    }
}

// Function to modify the take profit of an order
void ModifyTakeProfit(int ticket, double takeProfitPrice)
{
    if (ticket > 0)
    {
        double openPrice = OrderOpenPrice();
        double stopLossPrice = OrderStopLoss();
        
        if (OrderModify(ticket, openPrice, stopLossPrice, takeProfitPrice, 0, clrNONE))
        {
            Print("Take profit modified for ticket ", ticket, " to ", takeProfitPrice);
        }
        else
        {
            int error = GetLastError();
            Print("Error modifying take profit for ticket ", ticket, ". Error code: ", error);
            
            if (error == 130)
            {
                //Print("Error 130: Trade is not allowed. Possible reasons include closed orders or incorrect price values.");
            }
        }
    }
}

If you have any idea feel free to share...
Extract profit down to the last pip
Extract profit down to the last pip
  • www.mql5.com
The article describes an attempt to combine theory with practice in the algorithmic trading field. Most of discussions concerning the creation of Trading Systems is connected with the use of historic bars and various indicators applied thereon. This is the most well covered field and thus we will not consider it. Bars represent a very artificial entity; therefore we will work with something closer to proto-data, namely the price ticks.
 
Hello!
To fix this, you should ensure that the new take profit price is within the acceptable range according to your broker's StopLevel and the symbol you are trading. Here's a revised version of your code with modifications to handle the Error 130 scenario and avoid violating the StopLevel:

// Define the maximum deviation in pips
input int maxDeviation = 5;

// Function to modify the take profit of an order
bool ModifyTakeProfit(int ticket, double takeProfitPrice)
{
    if (ticket > 0)
    {
        double openPrice = OrderOpenPrice();
        double stopLossPrice = OrderStopLoss();
        
        // Check the allowed price deviation
        double priceDiff = MathAbs(takeProfitPrice - openPrice);
        if (priceDiff < MarketInfo(Symbol(), MODE_STOPLEVEL))
        {
            // Handle too small price difference
            Print("Price difference is too small to modify. Price deviation: ", priceDiff, " pips.");
            return false;
        }
        
        if (OrderModify(ticket, openPrice, stopLossPrice, takeProfitPrice, 0, clrNONE, 0, ""))
        {
            Print("Take profit modified for ticket ", ticket, " to ", takeProfitPrice);
            return true;
        }
        else
        {
            int error = GetLastError();
            Print("Error modifying take profit for ticket ", ticket, ". Error code: ", error);
            
            if (error == 130)
            {
                Print("Error 130: Trade is not allowed. Possible reasons include closed orders or incorrect price values.");
            }
            return false;
        }
    }
    
    return false;
}

void UpdateSecondPendingTradeTP(int filledTradeType)
{
    double newTakeProfit = 2 * takeProfit * Point; // Double the take profit*
    
    if (filledTradeType == OP_BUY)
    {
        if (!ModifyTakeProfit(sellStopOrder, newTakeProfit))
        {
            // Handle modification failure if needed
        }
    }
    else if (filledTradeType == OP_SELL)
    {
        if (!ModifyTakeProfit(buyStopOrder, newTakeProfit))
        {
            // Handle modification failure if needed
        }
    }
}

In this version, I've added a check to see if the price difference between the new take profit price and the open price is less than the broker's StopLevel. If the price difference is too small, it avoids attempting to modify the order and provides a print message indicating the issue. This should help you avoid Error 130.

Also, I've added the maxDeviation input parameter to control the maximum allowed deviation when modifying the order. You can adjust this value as needed based on your broker's requirements and market conditions.

Remember to replace any missing variable declarations (like takeProfit , sellStopOrder , buyStopOrder , etc.) with the appropriate values from your code.

Hope this helps you!
 
binaryforexea #:
Hello!
To fix this, you should ensure that the new take profit price is within the acceptable range according to your broker's StopLevel and the symbol you are trading. Here's a revised version of your code with modifications to handle the Error 130 scenario and avoid violating the StopLevel:

In this version, I've added a check to see if the price difference between the new take profit price and the open price is less than the broker's StopLevel. If the price difference is too small, it avoids attempting to modify the order and provides a print message indicating the issue. This should help you avoid Error 130.

Also, I've added the maxDeviation input parameter to control the maximum allowed deviation when modifying the order. You can adjust this value as needed based on your broker's requirements and market conditions.

Remember to replace any missing variable declarations (like takeProfit , sellStopOrder , buyStopOrder , etc.) with the appropriate values from your code.

Hope this helps you!
Hello,
Thanks for the input !
Maybe I'm dumb but where do you use your maxDeviation parameter ?
Also I somehow managed to see what's going on :
When my function is called, the second pending order is already live... Because I check which order is left only when there is one in MODE_HISTORY. But that means the second one is already live.
So I'm not trying to update a pending order... I'm trying to update the TP of an order that is live ! And I wonder why it won't work...
Reason: