1 (edited by geektrader 2022-03-09 01:38:08)

Topic: (Bug?) Slightly Different GetTrailingStopPrice() code between MT4/5

Hi Mr. Popov,

I´ve noticed that the GetTrailingStopPrice() function between MT4/5 is almost 100% identical, just one line (an extra check) is different. Shouldn´t this extra line from the MT4 function be added to the MT5 function as well?

MT4 function:

double GetTrailingStopPrice()
  {
   double bid = Bid();
   double ask = Ask();
   double stopLevelPoints = _Point * stopLevel;
   double stopLossPoints  = pip * Stop_Loss;

   if (posType == OP_BUY)
     {
      double stopLossPrice = High[1] - stopLossPoints;
      if (posStopLoss < stopLossPrice - pip && stopLossPrice < bid)
         return stopLossPrice < bid
                 ? stopLossPrice >= bid - stopLevelPoints
                    ? bid - stopLevelPoints
                    : stopLossPrice
                 : bid;
     }

   if (posType == OP_SELL)
     {
      double stopLossPrice = Low[1] + stopLossPoints;
      if (posStopLoss > stopLossPrice + pip)
         return stopLossPrice > ask
                 ? stopLossPrice <= ask + stopLevelPoints
                    ? ask + stopLevelPoints
                    : stopLossPrice
                 : ask;
     }

   return posStopLoss;
  }

MT5 function:

double GetTrailingStopPrice()
  {
   double bid = Bid();
   double ask = Ask();
   double stopLevelPoints = _Point * stopLevel;
   double stopLossPoints  = pip * Stop_Loss;

   if (posType == OP_BUY)
     {
      double stopLossPrice = High(1) - stopLossPoints;
      if (posStopLoss < stopLossPrice - pip)
         return stopLossPrice < bid
                  ? stopLossPrice >= bid - stopLevelPoints
                     ? bid - stopLevelPoints
                     : stopLossPrice
                  : bid;
     }

   if (posType == OP_SELL)
     {
      double stopLossPrice = Low(1) + stopLossPoints;
      if (posStopLoss > stopLossPrice + pip)
         return stopLossPrice > ask
                  ? stopLossPrice <= ask + stopLevelPoints
                     ? ask + stopLevelPoints
                     : stopLossPrice
                  : ask;
     }

   return posStopLoss;
  }

The difference is in this line (MT4) which is missing in MT5:

      if (posStopLoss < stopLossPrice - pip && stopLossPrice < bid)

I think that extra check "&& stopLossPrice < bid" should be added to the MT5 function as well?

Thank you :-)

Re: (Bug?) Slightly Different GetTrailingStopPrice() code between MT4/5

Hello GeekTrader,

Thank you for the report.
Actually the "&& stopLossPrice < bid" should be removed from MT4 code.

We do the check inside the if.
If "stopLossPrice < bid" then we set SL. The SL is set to "bid" otherwise.
It works in the case of a big gap where the "bid" jumps over SL.

Re: (Bug?) Slightly Different GetTrailingStopPrice() code between MT4/5

I see, smart idea. Then I will remove it. Many thanks.

Re: (Bug?) Slightly Different GetTrailingStopPrice() code between MT4/5

The Experts code is updated.

Thank you again for the report!

Re: (Bug?) Slightly Different GetTrailingStopPrice() code between MT4/5

Thank you for fixing it.