Order Modify Error 130 Help Needed Please

 

For some reason when back testing with hundreds of trades over 10 years I get the occasional order modify error 130...The EA can have multiple orders open at the same time and when another order is open when the previous order is in profit it will move the previous orders stop loss to break even, which seems to work. Take profit is kept the same. Now sometimes I get the error but the stop is still moved eventually. Here is my modify order code:

void IfOrderExistsBuy3()
{
    bool exists = false;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 3)
        {
            exists = true;
        }
    }
    else
    {
        Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    }
    
    if (exists)
    {
        BuyOrderModify2();
        
    }
}

void BuyOrderModify2()
{
    double takeprofit = 0;
    double stoploss = 0;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 2 && (OrderStopLoss() < OrderOpenPrice()))
        {
            takeprofit = OrderTakeProfit();
            stoploss =  OrderOpenPrice();
            
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, 0, White);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
    }
    
}
 

I tried this but no luck:


void IfOrderExistsBuy3()
{
    bool exists = false;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 3)
        {
            exists = true;
        }
    }
    else
    {
        Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    }
    
    if (exists)
    {
        BuyOrderModify2();
        
    }
}

void BuyOrderModify2()
{

    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 2 && (OrderStopLoss() < OrderOpenPrice()))
        {
         

            
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(),  OrderOpenPrice(), OrderTakeProfit(), 0, White);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
    }
    
}
 

And this but still getting the error - any help would be greatly appreciated!

void IfOrderExistsBuy3()
{
    bool exists = false;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 3)
        {
            exists = true;
        }
    }
    else
    {
        Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    }
    
    if (exists)
    {
        BuyOrderModify2();
        
    }
}

void BuyOrderModify2()
{

    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 2 && (OrderStopLoss() < OrderOpenPrice()))
        {
           double takeprofit = OrderTakeProfit();
           double stoploss =  OrderOpenPrice();
            
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, 0, White);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
    }
    
}
 
gangsta1:

For some reason when back testing with hundreds of trades over 10 years I get the occasional order modify error 130...The EA can have multiple orders open at the same time and when another order is open when the previous order is in profit it will move the previous orders stop loss to break even, which seems to work. Take profit is kept the same. Now sometimes I get the error but the stop is still moved eventually. Here is my modify order code:

Read this and modify your code to ensure it complies . . .  Requirements and Limitations in Making Trades

Then read this,  especially the latter posts,  I believe there is a bug in the Strategy Tester:   Error 130 in the Strategy Tester

 

Thanks Raptor - I had a read through and I guess it is nothing to worry about and results are still valid.

It cannot be that the buy/sell stops are too close as I have an offset >50 and the stop loss is only moved to the open price after that offset. It is annoying to have those errors however.

 

It doesn't matter what the price is the moment you modify your trade ??

or do you know what the price is and if so how ??

it is not to see in the condition you show us 

 

The trade is modified when another trade is opened (if trade exists) and another trade is only opened after price moves away from the order that I want to modify by at least 50 pips.

SO, if buy order number 2 exists (the new trade) then we want to modify buy order number 1. We only enter trade 2 after at least 50 pips so we should be able to put the stop loss of buy number 1 to the open price of buy number 1. It will only modify order number 1 when the stop loss is less than (or greater than for a sale) the open price so technically we are just moving the stop to break even when the new trade is opened.

It only happens say 1 time in 100 trades, maybe it is an issue with the testing data, but I would be willing to do whatever it takes to get it fixed.

 
gangsta1:It only happens say 1 time in 100 trades, maybe it is an issue with the testing data, but I would be willing to do whatever it takes to get it fixed.
if (OrderType() == OP_BUY 
&& OrderSymbol() == Symbol()
&& OrderMagicNumber() == 2 && (OrderStopLoss() < OrderOpenPrice())){
   :
   double stoploss =  OrderOpenPrice();
   bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, ...
Exactly where did you:
RaptorUK: modify your code to ensure it complies . . .  Requirements and Limitations in Making Trades
? You are changing the SL to the OOP. No where are you checking to make sure market (Bid) is above OOP by StopLevel.
Reason: