Trailing stoploss modify according to distance and counting it

 

Hi all, I have this code for the BUY order trailing stop.

//int counter = 0;
if(OrderStopLoss() == 0 ||
(((Bid - OrderOpenPrice()) > stoplossPoint) &&
  (OrderStopLoss() < (Bid - stoplossPoint))))
     {
      double trailingStop = NormalizeDouble(Bid - stoplossPoint,Digits);
      if(MathMax(OrderOpenPrice(), OrderStopLoss()) < trailingStop)
         {
          bool resSL = OrderModify(orderId,OrderOpenPrice(),trailingStop,OrderTakeProfit(),0);
          //counter++;
          if(!resSL)
             {
              Print("Error in Order Modify. Error code=",GetLastError());
              return;
             }
          else
              Print("Order modify successfully for ticket: ", OrderTicket());
             }
         }

the problem for this code is:

  1. The first trailing stop is moving from initial stoploss (set when opening position) to breakeven, after that it moves every points. I want the stoploss to change according to the (open position price - initial stoploss). How can I do that?
    e.g. if the initial distance between open order and initial stoploss is 500 point, the only change is every 500 point.
  2. I want to count the changing of the stoploss. I can add counter to that code but it will work only under that if statement (BUY position). What I want to do is that the counter will work on the global scope, How can I do that?