Trailing Stop following up and down, not just in trade direction.

To add comments, please log in or register
IsWired
10
IsWired  

Hi,

I want to use a trailing stop but the stoploss simply follows the price at the trailing stop distance up and down and for the life of me I cannot understand why. Thanks for any input in advance.

PS. I understand the code looks very sloppy currently, I plan to clean it up once I get it working. 

Also the actual trailing stop part of the function is in the second and fourth "else if" statements.

void TrailingStop()
  {
   if(type==OP_BUY && Close[0]>tp && firstchange==0)
     {
      if(OrderSelect(ticket1,SELECT_BY_TICKET))
        {
         if(OrderModify(ticket1,OrderOpenPrice(),OrderOpenPrice(),tp2+AvgTR,Magenta))
           {
            firstchange=1;

           }
        }
      NewSl=OrderOpenPrice();
      NewDiff=Close[0]-NewSl;
      NewTp=tp2+range;
      mySL=Ask-NewDiff; // new SL

     }
   else if(type==OP_BUY && firstchange==1)
     {
      mySL=Ask-NewDiff;
      NewTp=NewTp+range;
      if(OrderStopLoss()<mySL)
        {
         if(!OrderModify(ticket1,OrderOpenPrice(),mySL,NewTp,Magenta))
            Comment("Error");
        }
     }
   else if(type==OP_SELL && Close[0]<tp && firstchange==0)
     {
      if(OrderSelect(ticket1,SELECT_BY_TICKET))
        {
         if(OrderModify(ticket1,OrderOpenPrice(),OrderOpenPrice(),tp2-AvgTR,Magenta))
           {
            firstchange=1;
           }

        }
      NewSl=OrderOpenPrice();
      NewTp=tp2-range;
      NewDiff=NewSl-Close[0];
      mySL=Bid+NewDiff;
     }
   else if(type==OP_SELL && firstchange==1)
      mySL=Bid+NewDiff;
   NewTp=NewTp-range;
   if(OrderStopLoss()>mySL)
     {
      if(!OrderModify(ticket1,OrderOpenPrice(),mySL,NewTp,Magenta))
         Comment("Error");
     }
  }
Seng Joo Thio
1211
Seng Joo Thio  
IsWired:

I want to use a trailing stop but the stoploss simply follows the price at the trailing stop distance up and down and for the life of me I cannot understand why. 

First of all, you need to combine and move OrderSelect() up, before the first 'if', otherwise your 2nd and 4th 'else if's may have unpredictable results.

Second, your 4th 'else if' has a missing pair of {}.

Third, without showing us how you set the values of the variables, nobody can go further than visually checking the codes - which may or may not reveal the true cause of your problem. To get the speediest help, best is if you can include the entire code... otherwise it'll be hit-and-miss kind of thing as we'll all be telling you what we 'suspect' without actually trying things out...

To add comments, please log in or register