Trailing SL not working with broker have StopLevel greater than 0

 

Hello,


I am using ICMarkets and it have StopLevel = 0 for all currency pairs. I designed the Trailing based on ICMarket and code is really working amazing without any issue. 

Working fine with Broker having 0 Stop Level :

 double Pips;  
if(Digits == 3 || Digits == 5)
      Pips = 10.0 * Point;
   else
      Pips = Point;

void TrailingStopLoss(int TrailingStopLoss_magic)
  {
// Loop through all open orders
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         double TrailingStopLoss_entryPrice = OrderOpenPrice();



         if(OrderSymbol() == Symbol() && OrderMagicNumber() == TrailingStopLoss_magic && OrderType() == OP_BUY)
           {

            if(Bid - (TrailingStopLoss_entryPrice + Trailing_TP * Pips) > Pips * Trailing_Gap)
              {
               if(OrderStopLoss() < Bid - Pips * Trailing_Gap)
                 {
                  OrderModify(OrderTicket(), TrailingStopLoss_entryPrice + Trailing_TP * Pips, Bid - Pips * Trailing_Gap, OrderTakeProfit(), 0, clrNONE);

                 }
              }
           }


         if(OrderSymbol() == Symbol() && OrderMagicNumber() == TrailingStopLoss_magic && OrderType() == OP_SELL)
           {

            if((TrailingStopLoss_entryPrice - Trailing_TP * Pips) - Ask > Pips * Trailing_Gap)
              {
               if(OrderStopLoss() > Ask + Pips * Trailing_Gap || OrderStopLoss() == 0.0)
                 {

                  OrderModify(OrderTicket(), TrailingStopLoss_entryPrice - Trailing_TP * Pips, Ask + Pips * Trailing_Gap, OrderTakeProfit(), 0, clrNONE);


                 }
              }
           }
        }
     }
  }


When i changed my broker from ICMarkets to Hotforex which have StopLevel ranging from 30-50 based the currency pair, my above code get failed and i started getting error code 130. Than i realized it due to the StopLevel, so i made some changes which can work with both StopLevel 0 pairs and StopLevel more than 0 pairs.


Modification 1 (Failed) :


 double Pips;  
if(Digits == 3 || Digits == 5)
      Pips = 10.0 * Point;
   else
      Pips = Point;

void TrailingStopLoss(int TrailingStopLoss_magic)
  {
// Loop through all open orders
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         double TrailingStopLoss_entryPrice = OrderOpenPrice();


         // Check if the order is a buy order with magic number 1
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == TrailingStopLoss_magic && OrderType() == OP_BUY)
           {
                        if(Bid - (TrailingStopLoss_entryPrice + Trailing_TP * Pips) > Pips * Trailing_Gap)
              {
               if(OrderStopLoss() < Bid - Pips * Trailing_Gap)
                 {
                  OrderModify(OrderTicket(), TrailingStopLoss_entryPrice + Trailing_TP * Pips, Bid - (Pips * Trailing_Gap) - (MarketInfo(Symbol(), MODE_STOPLEVEL) * Pips), OrderTakeProfit(), 0, clrNONE);
                  if(_LastError != 0)
                                  Print(__FUNCTION__ + " => Buy Trailing Returning Error Code : " + GetLastError());
                 }
              }
           }

         // Check if the order is a sell order with magic number 2
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == TrailingStopLoss_magic && OrderType() == OP_SELL)
           {

            if((TrailingStopLoss_entryPrice - Trailing_TP * Pips) - Ask > Pips * Trailing_Gap)
              {
               if(OrderStopLoss() > Ask + Pips * Trailing_Gap || OrderStopLoss() == 0.0)
                 {

                  OrderModify(OrderTicket(), TrailingStopLoss_entryPrice - Trailing_TP * Pips, Ask + (Pips * Trailing_Gap) + (MarketInfo(Symbol(), MODE_STOPLEVEL) * Pips), OrderTakeProfit(), 0, clrNONE);
                                  if(_LastError != 0)
                                  Print(__FUNCTION__ + " => Sell Trailing Returning Error Code  : " + GetLastError());

                 }
              }
           }
        }
     }
  }

The Issue with Modification 1, it change Stop Loss in opposite to favor direction too and thus it never touch Stop-loss, as current price change against the favorable direction than  Stop-loss also get changed against the favorable direction.


Modification 2 :

 double Pips;  
if(Digits == 3 || Digits == 5)
      Pips = 10.0 * Point;
   else
      Pips = Point;

void TrailingStopLoss(int TrailingStopLoss_magic)
  {
   double stopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL);
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         double TrailingStopLoss_entryPrice = OrderOpenPrice();
         double stopLoss = OrderStopLoss();
         double stopAdjustment = stopLevel > 0 ? stopLevel * Point : 0.0;

         if(OrderSymbol() == Symbol() && OrderMagicNumber() == TrailingStopLoss_magic)
           {
            if(OrderType() == OP_BUY)
              {
               if(Bid - (TrailingStopLoss_entryPrice + Trailing_TP * Pips) > Pips * Trailing_Gap + stopAdjustment)
                 {
                  double TrailingStopLoss_Buy_newStopLoss = NormalizeDouble(Bid - Trailing_TP * Pips - stopAdjustment, Digits);
                  if(stopLoss < TrailingStopLoss_Buy_newStopLoss || stopLoss == 0.0)
                    {
                     OrderModify(OrderTicket(), TrailingStopLoss_entryPrice + Trailing_TP * Pips, TrailingStopLoss_Buy_newStopLoss, OrderTakeProfit(), 0, clrNONE);
                     if(_LastError != 0)
                        Print(__FUNCTION__ + " => Buy Trailing Error Code : " + GetLastError());
                    }
                 }
              }
            else
               if(OrderType() == OP_SELL)
                 {
                  if((TrailingStopLoss_entryPrice - Trailing_TP * Pips) - Ask > Pips * Trailing_Gap + stopAdjustment)
                    {
                     double TrailingStopLoss_Sell_newStopLoss = NormalizeDouble(Ask + Trailing_TP * Pips + stopAdjustment, Digits);
                     if(stopLoss > TrailingStopLoss_Sell_newStopLoss || stopLoss == 0.0)
                       {
                        OrderModify(OrderTicket(), TrailingStopLoss_entryPrice - Trailing_TP * Pips, TrailingStopLoss_Sell_newStopLoss, OrderTakeProfit(), 0, clrNONE);
                        if(_LastError != 0)
                           Print(__FUNCTION__ + " => Sell Trailing Error Code : " + GetLastError());
                       }
                    }
                 }
           }
        }
     }
  }


In Modification 2, still i am unable to understand what the mistake i am doing.


How can i modify my code so it can work in all kind of broker and pairs whether it have 0 stop-level or above 0 stoplevel