MQL4 Help moving stop loss

 

Hi All,

 I'm coding an EA and I'd like to move the stoploss of an order up to the opening price if the Bid is a certain number of points above the price the order was opened at.

 I have some code, and it does modify the stoploss correctly, however it also generates an OrderModify Error 1 on each tick too - so from what I can gather it's trying to modify the stoploss on each tick, not just when my conditions are met.

Below is the code I'm using, any help would be greatly appreciated!

int Free_Trade = 250;
for (int i = 1; i <= OrdersTotal(); i++) {
   if (!OrderSelect(i, SELECT_BY_POS)) continue;
      if (OrderType() == OP_BUY)  
         if (Bid > (OrderOpenPrice() + (Free_Trade*Point))) {  
            if (OrderStopLoss() < OrderOpenPrice()) {
               OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0, Blue)
            }
         }
      }
   }
}
 
int Free_Trade = 250;
for (int i = 1; i <= OrdersTotal(); i++) {                     //Don't count up !!!!
   if (!OrderSelect(i, SELECT_BY_POS)) continue;
      if (OrderType() == OP_BUY)  
         if (Bid > (OrderOpenPrice() + (Free_Trade*Point))) {  
            if (OrderStopLoss() < OrderOpenPrice()) {
               OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()+Point, OrderTakeProfit(), 0, Blue)  // this correction
            }
         }
      }
   }
}

this way it will ending error 1

But also Don't count up checking trades

Read   Loops and Closing or Deleting Orders

The moment trades get closed why the EA is doing the loop it can fail

 

Thanks for the tip about counting down, rather than up - I've modified my code accordingly.

 The correction you suggested also worked - can you please explain to me why setting SL to OrderOpenPrice() causes errors but setting it to OrderOpenPrice()+Point doesn't? 

 
kiwi_trader:

Thanks for the tip about counting down, rather than up - I've modified my code accordingly.

 The correction you suggested also worked - can you please explain to me why setting SL to OrderOpenPrice() causes errors but setting it to OrderOpenPrice()+Point doesn't? 


https://www.mql5.com/en/forum/136997   Can price != price ?