Need Help! - How to make trailing stop trail losing trades when negative rather in profit.

 
void NewTrailingStop(int xticket) {
   double NewStop = 0;
   int digits = MarketInfo(symb,MODE_DIGITS);
        double bid = MarketInfo(symb,MODE_BID);
        double ask = MarketInfo(symb,MODE_ASK);
        double point = MarketInfo(symb,MODE_POINT);
        double NewRealStop = 0;
        double TriggerAdjustment = 0;
        if (digits==3 || digits==5) point = point*10;
        for (int i2=0; i2<40; i2++){
      if (orders_ticket[i2] == xticket) {
        if (AdjustTrailOnBadFill && orders_had_bad_fill[i2]) TriggerAdjustment = ReduceTrailTriggerBy;
         if (orders_trailing_stop[i2] == 0) {                                                                      // no stops on yet         
            if (OrderType() == OP_BUY) {                                                                  
               if (bid-OrderOpenPrice() >= (TrailingTrigger-TriggerAdjustment)*point) {
                  NewStop = NormalizeDouble(bid - TrailingStop*point,digits);
                  if (UseRealTrailingStop) NewRealStop = NormalizeDouble(bid-RealTrailingStop*point,digits);
               }
            }
            if (OrderType() == OP_SELL) {                                                               
               if (OrderOpenPrice() - ask >= (TrailingTrigger+TriggerAdjustment)*point) {
                  NewStop = NormalizeDouble(ask + TrailingStop*point,digits);
                  if (UseRealTrailingStop) NewRealStop = NormalizeDouble(ask+RealTrailingStop*point,digits);
               }
            }
            if (NewStop != 0) {
               orders_trailing_stop[i2]=NewStop;
               Print ("tkt=" + xticket + "  " + symb + " initial trailing stop = " + DoubleToStr(NewStop,5));
            }
            if (NewRealStop != 0) {
               orders_real_trailing_stop[i2] = NewRealStop;
               Print ("tkt=" + xticket + "  " + symb + " initial real trailing stop = " + DoubleToStr(NewRealStop,5));
            }
            return;
         } 
         if (orders_trailing_stop[i2] != 0) {
            if (OrderType() == OP_BUY) {  
               if (bid - OrderOpenPrice() >= (TrailingTrigger-TriggerAdjustment)*point) {
                  NewStop = NormalizeDouble(bid - TrailingStop*point,digits);
                  if (NewStop > orders_trailing_stop[i2] + TrailingStopStep*point) {
                     orders_trailing_stop[i2]=NewStop;
                     Print ("tkt=" + xticket + "  " + symb + " new trailing stop = " + DoubleToStr(NewStop,5));  
                  }
                  if (UseRealTrailingStop) {
                     NewRealStop = NormalizeDouble(bid - RealTrailingStop*point,digits);
                     if (NewRealStop > orders_real_trailing_stop[i2] + RealTrailingStopStep*point) {
                        OrderModifyReliable(OrderTicket(),OrderOpenPrice(),NewRealStop,0,0,CLR_NONE);
                        Print ("real trailing stop at " + DoubleToStr(NewRealStop,5));
                        orders_real_trailing_stop[i2]=NewRealStop;
                     }
                  }
               }
            }
            if (OrderType() == OP_SELL) {
               if (OrderOpenPrice() - ask >= (TrailingTrigger+TriggerAdjustment)*point) {
                  NewStop = NormalizeDouble(ask + TrailingStop*point,digits);
                  if (NewStop < orders_trailing_stop[i2]-TrailingStopStep*point) {
                     orders_trailing_stop[i2]=NewStop;
                     Print ("tkt=" + xticket + "  " + symb + " new trailing stop = " + DoubleToStr(NewStop,5)); 
                  }                    
                  if (UseRealTrailingStop) {
                     NewRealStop = NormalizeDouble(ask + RealTrailingStop*point,digits);                  
                     if (NewRealStop < orders_real_trailing_stop[i2] - RealTrailingStopStep*point) {
                        OrderModifyReliable(OrderTicket(),OrderOpenPrice(),NewRealStop,0,0,CLR_NONE);
                        Print ("real trailing stop at " + DoubleToStr(NewRealStop,5));
                        orders_real_trailing_stop[i2]=NewRealStop;

my ea has a UseTrailingStop set to true.

the UseTrailingStop only trails when 3 pips had been reached, I want UseTrailingStop to trail negative pips only rather then pips in profit. what needs to change?

Files:
help.mq4  15 kb
 
autt86:

my ea has a UseTrailingStop set to true.

the UseTrailingStop only trails when 3 pips had been reached, I want UseTrailingStop to trail negative pips only rather then pips in profit. what needs to change?

...
Please edit your post and use SRC button for posting code. Thanks
 
autt86:

my ea has a UseTrailingStop set to true.

the UseTrailingStop only trails when 3 pips had been reached, I want UseTrailingStop to trail negative pips only rather then pips in profit. what needs to change?



Out of interest, why do you want to do this? I had this thought briefly once but I cant see how it is any improvement over setting the initial stop loss more optimally??

 

At first sight you will have it with simply adjusting these variables:

TrailingTrigger,  TriggerAdjustment

I am not sure, I don't know what is the initial value of these variables.