Download MetaTrader 5
To add comments, please log in or register
Add our channel to friends and follow the news!
mar
1143
mar 2014.06.07 19:21 

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);
               }
            }
         }
      }
   }
  }
Drazen Penic
2406
Drazen Penic 2014.06.07 22:26  

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 

mar
1143
mar 2014.06.07 22:50  
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..
Ian Venner
2583
Ian Venner 2014.06.08 00:54  

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

OrderOpenPrice() - ((OrderStopLoss() - OrderOpenPrice())*3)
OrderOpenPrice() + ((OrderOpenPrice() - OrderStopLoss())*3)
Bambang Sugianto
9831
Bambang Sugianto 2014.06.08 05:25  
must filter by Order Type  OP_BUY  or OP_SELL
mar
1143
mar 2014.06.08 07:18  

@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..

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.06.08 07:29  
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.
mar
1143
mar 2014.06.08 07:40  

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?

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.06.08 08:06  
OrderOpenTime() always reflect to its open time. When pending orders becomes market orders, OrderOpenTime() will take the time of it filled.
mar
1143
mar 2014.06.08 08:09  
OK, good to know. Thanks! That is poorly described in the docs.
whroeder1
13627
whroeder1 2014.06.09 12:56  
  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
/
To add comments, please log in or register