Download MetaTrader 5
To add comments, please log in or register
Having difficulties in understanding the Trading Signals service? Read the article!
spillstuff
98
spillstuff 2014.06.16 19:38 

I am using OrderModify for trailing stops.   The problem is when the stop is => the OrderOpenPrice the order closes at the stop.  How do I get the order to stay open past the order open price?

          if(OrderType()==OP_BUY)

         {

               if(Bid>OrderOpenPrice())

                 {

                    tsbuy=Bid-OrderOpenPrice();

                    buystop=OrderStopLoss()+tsbuy;

                    if(buystop>OrderStopLoss())                  

                    {

                     //--- modify order and exit

                     ticket=OrderModify(OrderTicket(),OrderOpenPrice,buystop,OrderTakeProfit(),0,Green);

                     return;

                    }

 

Any critique/suggestions appreciated.

Rudy 

  

Keith Watford
Moderator
7897
Keith Watford 2014.06.17 02:54  
   if(OrderType()==OP_BUY)
     {
      if(Bid>OrderOpenPrice())
        {
        tsbuy=Bid-OrderOpenPrice();
        buystop=OrderStopLoss()+tsbuy;
        if(buystop>OrderStopLoss())
          {
          //--- modify order and exit
          ticket=OrderModify(OrderTicket(),OrderOpenPrice,buystop,OrderTakeProfit(),0,Green);

          return;

          }

You will need to rethink your code.

Imagine a situation where you have a 10 pip SL and Bid reaches OrderOpenPrice() + 1 pip.

        tsbuy=Bid-OrderOpenPrice();    //EQUALS 1 PIP
        buystop=OrderStopLoss()+tsbuy; //EQUALS ORDEROPENPRICE -9 PIPS
        if(buystop>OrderStopLoss())    //THIS WILL BE TRUE, SO ORDER IS MODIFIED TO -9 PIPS

//      NEXT TICK----

        tsbuy=Bid-OrderOpenPrice();    //EQUALS 1 PIP
        buystop=OrderStopLoss()+tsbuy; //EQUALS ORDEROPENPRICE -8 PIPS
        if(buystop>OrderStopLoss())    //THIS WILL BE TRUE, SO ORDER IS MODIFIED TO -8 PIPS

//      NEXT TICK----

        tsbuy=Bid-OrderOpenPrice();    //EQUALS 1 PIP
        buystop=OrderStopLoss()+tsbuy; //EQUALS ORDEROPENPRICE -7 PIPS
        if(buystop>OrderStopLoss())    //THIS WILL BE TRUE, SO ORDER IS MODIFIED TO -7 PIPS

//      SL WILL BE MODIFIED EVERY TICK UNTIL IT IS VERY CLOSE TO THE CURRENT PRICE
spillstuff
98
spillstuff 2014.06.17 04:16  
If Bid reaches OrderOpenPrice + 10, why does the order close because SL is near OrderOpenPrice?  I feel I'm missing something basic here.
whroeder1
13602
whroeder1 2014.06.17 11:55  
spillstuff: I am using OrderModify for trailing stops. I feel I'm missing something basic here.
Yes you are. A trailing stop is relative to the OrderClosePrice (Bid). It's calculation does not involve the current SL value.
  1.         tsbuy=Bid-OrderOpenPrice();
            buystop=OrderStopLoss()+tsbuy;
            if(buystop>OrderStopLoss())
    
    Rename tsbuy as profit. First tick you move stop up by profit., next tick you move (new) stop up by profit again, ....
  2. You want a trailing stop. So you code it that way.
    //      tsbuy=Bid-OrderOpenPrice();              
    //      buystop=OrderStopLoss()+tsbuy;           
            buystop = Bid - FixedStopLoss * pips2dbl;
            if(buystop>OrderStopLoss() +_Point) // The == operand. - MQL4 forum
    
Ian Venner
2583
Ian Venner 2014.06.17 16:13  

Trailing stops can use a lot of ordermodifys, it might be more efficient to trail the price internally and use OrderClose when it gets there.

whroeder1
13602
whroeder1 2014.06.17 16:49  
SDC: Trailing stops can use a lot of ordermodifys, it might be more efficient to trail the price internally and use OrderClose when it gets there.
And if you loose connection?
Ian Venner
2583
Ian Venner 2014.06.18 00:09  

WHRoeder:

 And if you loose connection?

 Well i said it might be more efficient. If your EA is using an unreliable internet connection it might not be.
spillstuff
98
spillstuff 2014.06.18 10:01  
Thanks for the response(s).  I am rethinking the code as suggested.
/
To add comments, please log in or register