Issue with trailingStop

 

Hi all,

I've a problem with the follow code to get the trailing Stop to start after a minimum profit is reached.

As it is when a profit of 5 pips is reached, the stop loss should get positioned at 3 pips after the order open price, but what is happening is that once we get a profit  of 5 pips the stop loss line start to move but is keeping beyond almost 40 pips.

void Trail()
    {//
    double TrailingStop =  StopLevel;   
       
       double BuyTrailing =  Bid - (TrailingStop * pips2dbl);
       double SellTrailing = Ask + (TrailingStop * pips2dbl);
                       
       for(int OrderCounter = OrdersTotal()-1; OrderCounter >= 0; OrderCounter--)                     
       {//1
          if(!OrderSelect(OrderCounter,SELECT_BY_POS))continue;
          if(OrderType()==OP_BUY && OrderSymbol()== Symbol()&& OrderMagicNumber()== MagicNumber)            
          {//2 
             RefreshRates();           
             if(OrderStopLoss() < (Bid - TrailingStop * pips2dbl-Point) && (Bid - OrderOpenPrice()) >= (MinimumProfit * pips2dbl))  
              {//3               
                if(!OrderModify(OrderTicket(),OrderOpenPrice(),BuyTrailing, OrderTakeProfit(),0,CLR_NONE))
                 {//4
                   ErrorCode = GetLastError();                   
                   string ErrDesc = ErrorDescription(ErrorCode);
                            
                   string ErrAlert = StringConcatenate(" Buy Trailing Stop Failed: ",ErrorCode, ": ",ErrDesc);
                   Alert(ErrAlert);
                  
                   string ErrLog = StringConcatenate(" Buy Trailing Stop Failed -  Bid: ",DoubleToStr(Bid,Digits)," Spread: ",DoubleToStr(Spread,Digits)," Ticket: " , OrderTicket()," Buy Trailing: ",DoubleToStr(BuyTrailing,Digits)," Stop Loss: ",DoubleToStr(BuyStopLoss,Digits)," Stop Level: ",StopLevel," Freeze Level: ",FreezeLevel);
                   Print(ErrLog);
                  }//4               
              }//3
          }//2          
          if(OrderType()==OP_SELL)             
          {//5
             RefreshRates();
             if(OrderStopLoss() > (Ask + TrailingStop * pips2dbl)+Point|| OrderStopLoss()< Point && (OrderOpenPrice() - Ask) >= (MinimumProfit * pips2dbl))                       
              {//6                  
                if(!OrderModify(OrderTicket(),OrderOpenPrice(),SellTrailing,OrderTakeProfit(),0,CLR_NONE))
                 {//7
                   ErrorCode = GetLastError();
                   ErrDesc =ErrorDescription(ErrorCode);
                           
                   ErrAlert = StringConcatenate(" Sell Trailing Stop Failed: ",ErrorCode,": ",ErrDesc);
                   Alert(ErrAlert);
                  
                   ErrLog = StringConcatenate(" Sell Trailing Stop Failed -  Ask: ",DoubleToStr(Ask,Digits)," Spread: ",DoubleToStr(Spread,Digits)," Ticket: ",OrderTicket()," Sell Trailing: ",DoubleToStr(SellTrailing,Digits)," Stop Loss: ",DoubleToStr(SellStopLoss,Digits)," Stop Level: ",StopLevel," Freeze Level: ",FreezeLevel);
                   Print(ErrLog);
                 }//7                                                     
             }//6
          }//5
       }//1
    }//0             

 

Is that possible to get from you an idea where I'm wrong ?

Thank you in advance for any support provided

Luis 

 

Hi all,

It's working. StopLevel must have "*Points" after.

Thank you anyway

Luis 

Reason: