Trailing stop

 
//Trailing Tool---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  void Trail_Stop()
  {
   if(Trailing_Stop!=0)
   {
   int i=OrdersTotal()-1;
   for(i=OrdersTotal()-1;i>=0;i--)
   {
    OrderSelect(i,SELECT_BY_POS);
    if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic_Number)
    {
     if(OrderType()==OP_BUY)
     {
      if( (OrderStopLoss()<OrderOpenPrice() || OrderStopLoss()==0) && Bid-OrderOpenPrice()>=Trailing_Stop*pip*Point
         && Trailing_Stop*pip>=MarketInfo(Symbol(),MODE_STOPLEVEL)  && NormalizeDouble(Bid-Trailing_Stop*pip*Point,Digits)!=OrderStopLoss())
       OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Trailing_Stop*pip*Point,Digits),OrderTakeProfit(),0,Gold);
      
      if(OrderStopLoss()>=OrderOpenPrice() && Bid-OrderStopLoss()>=Trailing_Stop*pip*Point && NormalizeDouble(Bid-Trailing_Stop*pip*Point,Digits)-OrderStopLoss()>Point)
       OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Trailing_Stop*pip*Point,Digits),OrderTakeProfit(),0,Gold);
     }
     if(OrderType()==OP_SELL)
     {
      if( (OrderStopLoss()>OrderOpenPrice() || OrderStopLoss()==0) && OrderOpenPrice()-Ask>=Trailing_Stop*pip*Point
         && Trailing_Stop*pip>=MarketInfo(Symbol(),MODE_STOPLEVEL)  && NormalizeDouble(Ask+Trailing_Stop*pip*Point,Digits)!=OrderStopLoss())
       OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Trailing_Stop*pip*Point,Digits),OrderTakeProfit(),0,Gold);
     
      if(OrderStopLoss()<=OrderOpenPrice() && OrderStopLoss()-Ask>=Trailing_Stop*pip*Point && OrderStopLoss()-NormalizeDouble(Ask+Trailing_Stop*pip*Point,Digits)>Point)
       OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Trailing_Stop*pip*Point,Digits),OrderTakeProfit(),0,Gold);     
     }
    }
   }  
  }
  }
 //Trailing Tool------

Hi,

My ea uses this code for the trailing stop. It is the type of stop that jumps to break even when the trailing stop value is reached and then starts trailing from there.. What do I need to delete (or modify) so that it becomes the type that trails immediately from as soon as the order has been opened?

 

Thanks in advance! 

 

hello mate

i just wonder why you use in sell orders ask price and bid in buy

and about our question think this is it

  void Trail_Stop()
  {
   if(Trailing_Stop!=0)
   {
   int i=OrdersTotal()-1;
   for(i=OrdersTotal()-1;i>=0;i--)
   {
    OrderSelect(i,SELECT_BY_POS);
    if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic_Number)
    {
     if(OrderType()==OP_BUY)
     {
      if( (OrderStopLoss()<OrderOpenPrice() || OrderStopLoss()==0) && Bid-OrderOpenPrice()>=Trailing_Stop*pip*Point
         && Trailing_Stop*pip>=MarketInfo(Symbol(),MODE_STOPLEVEL) )
       OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Trailing_Stop*pip*Point,Digits),OrderTakeProfit(),0,Gold);
      
      }
     if(OrderType()==OP_SELL)
     {
      if( (OrderStopLoss()>OrderOpenPrice() || OrderStopLoss()==0) && OrderOpenPrice()-Ask>=Trailing_Stop*pip*Point
         && Trailing_Stop*pip>=MarketInfo(Symbol(),MODE_STOPLEVEL) )
       OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Trailing_Stop*pip*Point,Digits),OrderTakeProfit(),0,Gold);
       
     }
    }
   }  
  }
  }
 
Aleksei Beliakov:

hello mate

i just wonder why you use in sell orders ask price and bid in buy

Because the stop is calculated from the close price.

and about our question think this is it

Your code will not trail at all, it's a breakeven when Trailing_Stop profit is reached.

 
Adam Davies:

Hi,

My ea uses this code for the trailing stop. It is the type of stop that jumps to break even when the trailing stop value is reached and then starts trailing from there.. What do I need to delete (or modify) so that it becomes the type that trails immediately from as soon as the order has been opened?

 

Thanks in advance! 

 
Alain Verleyen:

Because the stop is calculated from the close price.

Your code will not trail at all, it's a breakeven when Trailing_Stop profit is reached.

It does trail from when it jumps to break even.. I am currently using it. I want to adapt it though for a new strategy that trails straight away. This is how I want it to work.. I will try the new code when I'm home so thanks!

Let's say the price is 1.5000 and I open a long entry with a trailing stop value of 20 pips (at 1.4980).. When the price moves to 1.5001 I want the trailing stop to move to 1.4981

So it starts trailing immediately but still stays 20 pips behind the price..
 
and just to clarify, the ea places stop orders
 
Adam Davies:
It does trail from when it jumps to break even.. I am currently using it. I want to adapt it though for a new strategy that trails straight away. This is how I want it to work.. I will try the new code when I'm home so thanks!

Let's say the price is 1.5000 and I open a long entry with a trailing stop value of 20 pips (at 1.4980).. When the price moves to 1.5001 I want the trailing stop to move to 1.4981

So it starts trailing immediately but still stays 20 pips behind the price..
My reply was to Aleksei. His code is not doing what you asked for.
 
Aleksei Beliakov:

hello mate

i just wonder why you use in sell orders ask price and bid in buy

and about our question think this is it

Thanks for your reply, I tried this and it just jumps the trailing stop to break even as soon as the price moves up (whilst long), rather than trailing it at the set distance as soon as the price moves up
 
Alain Verleyen:
My reply was to Aleksei. His code is not doing what you asked for.
Oh ok, sorry, I was confused. Yes I just found that out whilst testing it. Still stuck at the moment but I'll keep trying
 
Adam Davies:
Oh ok, sorry, I was confused. Yes I just found that out whilst testing it. Still stuck at the moment but I'll keep trying

 NOT TESTED

   void Trail_Stop()
     {
      if(Trailing_Stop!=0)
        {
         for(int i=OrdersTotal()-1;i>=0;i--)
           {
            OrderSelect(i,SELECT_BY_POS);
            if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic_Number)
              {
               if(OrderType()==OP_BUY)
                 {
                  if(NormalizeDouble(Bid-Trailing_Stop*pip*Point,Digits)-OrderStopLoss()>Point)
                     OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Trailing_Stop*pip*Point,Digits),OrderTakeProfit(),0,Gold);
                 }
               if(OrderType()==OP_SELL)
                 {
                  if(OrderStopLoss()-NormalizeDouble(Ask+Trailing_Stop*pip*Point,Digits)>Point)
                     OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Trailing_Stop*pip*Point,Digits),OrderTakeProfit(),0,Gold);
                 }
              }
           }
        }
     }
 
Alain Verleyen:

 NOT TESTED

I've only just seen this. Thank you, I will try this out in the morning!
Reason: