How to code a dynamic trailingstop that decrements as profit rise?

 

Hi guys

Most of the EA's that I've seen operate on a fixed pip trailingstop.  For every pip the price go in your trade's direction, it adjusts the trailing stop by 1 pip.  So, if your trailing stop gets triggered at say 10 pips and the stoploss trails behind by 200 pips, then you will basically break even once the price went up by 200 pips and then turns against you.  I want to lock this profit in by having the stoploss decrement by two pips for very one pip the price goes up (on a buy trade).  So, once the trade has gone up by 100 pips, the stoploss does not trail behind by 200pips, but actually went up to 100 pips.  And then when the price reach 200 pips up, the trail is only 20 pips behind.  So the SL gets adjusted up to a certain point( say 20 pips) and then stay there and keep going up one pip for every one pip.  

Example for traditional fixed pip:  OrderOpenPrice=160.0

                Stoploss= 160.0-200pips= 158.0

Price then goes up by 100pips 

               Bid=161.0

               OrderOpenprice=160.0

               SL=159.0

Price goes up with another 100 pips

              Bid=162.0

              OOP=160.0

              SL=160 (Breakeven point)

Now if trade goes against me, I lose this 200 pips


But with a dynamic trailstop:

                 OOP=160.0

                 SL= 158.0

Up by 100 pips

                Bid=161.0

                OOP=160.0

               SL= 160 (instead of 161-200pips, it is now 161-100 pips)

Up by 200 pips

                Bid=162

                OOP=160

                SL=161.8 (trailing by only 20 pips now and keeping it there.  So it does not increment any more from this extern int parameter


Somehow, this does not seem to work properly.  I think there is a mistake with the minimum and maximum stoploss or the code for setting the stoploss.  Will you please let me know.

for(int i=OrdersTotal()-1; i>=0; i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderSymbol()==Symbol()){               
            if(OrderType()==OP_BUY){
               if(Bid-OrderOpenPrice()>TSTP){ //Trailingstop gets triggered
                  if(OrderStopLoss()<Bid-Min){ //Min Trailingstop.  In my case 20 pips
                     if(OrderStopLoss()<Bid-Trail+Dec*(Bid-OrderOpenPrice())){ //If SL is below this value .....
                        if(OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Trail+(Bid-OrderOpenPrice()),OrderTakeProfit(),0,Orange)) //..... it is rest to this value
                        RefreshRates();
                       }
                     }
                   }
                 }
            if(OrderType()==OP_SELL){
               if(OrderOpenPrice()-Ask>TSTP){
                  if(OrderStopLoss()>Ask+Min){
                     if(OrderStopLoss()>Ask+Trail-Dec*(OrderOpenPrice()-Ask) || OrderStopLoss()==0){
                        if(OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Trail-(OrderOpenPrice()-Ask),OrderTakeProfit(),0,Orange))
                        RefreshRates();
                       }   
                     }
                   }
                 }
               }  
             }
           }
         }
 
trader3000:


Somehow, this does not seem to work properly. 

What is that mean? Any errors or what?

what are this values? inputs?

if(Bid-OrderOpenPrice()>TSTP){ //Trailingstop gets triggered
                  if(OrderStopLoss()<Bid-Min){ //Min Trailingstop.  In my case 20 pips
                     if(OrderStopLoss()<Bid-Trail+Dec*(Bid-OrderOpenPrice())){ //If SL is below this value .....
 

  Not tested, but according to the principle 

void Trail() 
{
   double pipvalue=_Point;
   if (_Digits==5 || _Digits<=3) pipvalue=_Point*10;

  double StopLoss=100;
  double StopMultiplier=2;   // 1=normal 1 pip increment trailing
  double StopMinDistance=20; // minimum distance to  trail
  
  int OM;
  for (int pos=0; pos<OrdersTotal(); pos++) 
  {
     if (OrderSelect(pos,SELECT_BY_POS)==TRUE && OrderSymbol()==Symbol())
     {   
        if (OrderType()== OP_BUY)
        {
           double ProfitBuy = MathMax(Bid-OrderOpenPrice(),0); // calc profit for buy when > 0
           double initialstoppriceB=OrderOpenPrice()-StopLoss*pipvalue; //initial stop level
           double SLbuy = ProfitBuy*StopMultiplier + initialstoppriceB; //factor from initial stop level

           if (!OrderStopLoss() || ((Bid-OrderStopLoss())/pipvalue > StopMinDistance && SLbuy > OrderStopLoss()))
             OM=OrderModify(OrderTicket(),OrderOpenPrice(), SLbuy ,OrderTakeProfit(),0,Green);
        }
        if (OrderType()== OP_SELL)
        {
           double ProfitSell= MathMax(OrderOpenPrice()-Ask,0); // calc profit for sell when > 0
           double initialstoppriceS=OrderOpenPrice()+StopLoss*pipvalue; //initial stop level
           double SLsell= initialstoppriceS - ProfitSell*StopMultiplier; //factor from initial stop level  

           if (!OrderStopLoss() || ((OrderStopLoss()-Ask)/pipvalue > StopMinDistance && SLsell < OrderStopLoss()))
             OM=OrderModify(OrderTicket(),OrderOpenPrice(), SLsell ,OrderTakeProfit(),0,Orange); 
        }
      }
   }
}