Always error #130 when OrderModify()

 

Hey guys,

I want to do a simple thing.. Just move my stop to breakeven when the trade is the same amount of pips in profit. That's all. And whatever I try, I always get error 130. I don't understand why this is an invalid SL? Can anyone help?

void OnTick()
  {
//---
   for (int i = OrdersTotal()-1; i >= 0; i--) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == Symbol()) {
            if (OrderOpenTime() < TimeCurrent()) {
               if (OrderOpenPrice() > OrderStopLoss() && (Bid - OrderOpenPrice() >= OrderOpenPrice() - OrderStopLoss())) {
                  SLtoBE = OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice(),
                           OrderOpenPrice() + ((OrderOpenPrice() - OrderStopLoss())*3), 0, clrNONE);
               }  
               else if (OrderOpenPrice() < OrderStopLoss() && Ask < OrderOpenPrice() && (OrderOpenPrice() - Ask) >= OrderStopLoss() - OrderOpenPrice()) {
                  SLtoBE = OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice(),
                           OrderOpenPrice() - ((OrderStopLoss() - OrderOpenPrice())*3), 0, clrNONE);
               }
            }
         }
      }
   }
  }
 

Your stops are to close to the current price. You need to find out minimum stops required by your broker.

See this answer for details and example: https://www.mql5.com/en/forum/151846 

 
Unfortunately that is not the problem. I use a test order buying at 1.60500 and the stop is 1.60200. When Bid > 1.60800 (=OrderOpenPrice + (OrderOpenPrice - OrderStopLoss)) I move the stop to 1.60500. So with 30 pips the stop-level is not the problem in this case..
 

You need to NormaliseDouble() to Digits(), these caclulations:

OrderOpenPrice() - ((OrderStopLoss() - OrderOpenPrice())*3)
OrderOpenPrice() + ((OrderOpenPrice() - OrderStopLoss())*3)
 
must filter by Order Type  OP_BUY  or OP_SELL
 

@SDC: Doesn't work.. :-(

I also changed OrderOpenPrice(), OrderStopLoss() and TakeProfit to absolute values to test it. And that doesn't work, too.

How can that be so complicated? It is a simple break-to-even-stop. But I don't understand why the stop is always invalid..

 
Just do a simple debugging, use Print() to see the result of OrderModify() command. I bet you would straightly got the answer to your problems.
 

It works! You know what the problem was? For testing purposes I had to use a buy limit order and this order was never executed because of the very large weekend-spread. I have not paid any attention to that. But on the other hand I use OrderOpenTime() and TimeCurrent() as a filter if there is a running order. Could it be that OrderOpenTime() is the time where the order was created instead of executed? I didn't find anything about that in the MQL4-docs.


So how do I know if there is an already executed order or if the order is still pending? OrderProfit() could be a possibility, or is there any other?

 
OrderOpenTime() always reflect to its open time. When pending orders becomes market orders, OrderOpenTime() will take the time of it filled.
 
OK, good to know. Thanks! That is poorly described in the docs.
 
  1. mar: move my stop to breakeven when the trade is the same amount of pips in profit. That's all. And whatever I try, I always get error 130. I don't understand why this is an invalid SL?
    if (OrderOpenPrice() > OrderStopLoss() && (Bid - OrderOpenPrice() >= OrderOpenPrice() - OrderStopLoss())) {
       SLtoBE = OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice(),
                OrderOpenPrice() + ((OrderOpenPrice() - OrderStopLoss())*3), 0, clrNONE);
    
    First problem is once you make the modification, your OrderOpenPrice() vs OrderStopLoss() now fails, either it succeeds when it shouldn't or the opposite direction IF succeeds. You want to test the order type, so do it if (OrderType() == OP_BUY && ...
  2. Second, is you are also modifying the TP. Once you modify the order, the next time you will be trying to set the TP equal to OOP and get 130. You don't want to move the TP, so don't do it.
    if (OrderType() == OP_BUY && (Bid - OrderOpenPrice() >= OrderOpenPrice() - OrderStopLoss())) {
       SLtoBE = OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice(), 
                OrderTakeProfit()                                          , 0, clrNONE);
  3. Third, once you move, to break even, your test will still succeed (Bid - OOP >= 0) and you will start getting Error 1 . Test and avoid.
    if (OrderType() == OP_BUY 
    && OrderOpenPrice() - OrderStopLoss() > _Point / 2.
    && (Bid - OrderOpenPrice() >= OrderOpenPrice() - OrderStopLoss()
    ) {
       SLtoBE = OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() + _Point, 
                OrderTakeProfit()                                          , 0, clrNONE);
  4. finally, test your return code. What are Function return values ? How do I use them ? - MQL4 forum

  5. SDC: You need to NormaliseDouble() to Digits(), these caclulations:
    Do not use NormalizeDouble ever. The == operand. - MQL4 forum