Trailing candle stoploss - page 2

 

I see my failings. Its should be "timePre"=Time[0];

 

This coding is a very steep learning curve ha. Enjoying it though. 

 

Ok so now i'm completly lost. I cant for the life of me work out why the stoploss keeps moving away when the price keeps moving towards it. It defeats its purpose. I keep getting loads of ordermodify errors. Its meant to find the low of a certain amount of candles and move the SL to it. However it then decides to move it again should price move towards it.

 

 

void AdjustTrail()
  {
   double minstoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL); // Gets min SL Level

   int BuyStopCandle=iLowest(NULL,0,1,CandlesBackSL,1); // Finds the swing low
   int SellStopCandle=iHighest(NULL,0,2,CandlesBackSL,1); // Finds the swing high.

   double sell_stop_price=High[SellStopCandle]+SwingPadAmount*pips; // Adds SL to High
   double buy_stop_price=Low[BuyStopCandle]-SwingPadAmount*pips; // Adds SL on low when buying + Pad amount
//---

      for(int b=OrdersTotal()-1; b>=0; b--)
     {


      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==MagicNumber)
            if(OrderSymbol()==Symbol())
              {
               if(OrderType()==OP_BUY)
                 {
                  if(OrderStopLoss()<Low[BuyStopCandle]-TrailingStopPad*pips) // User variable to look back x amount of candles.
                     OrderModify(OrderTicket(),OrderOpenPrice(),buy_stop_price,OrderTakeProfit(),0,CLR_NONE);

                  Print(" Candle SL Buy error",GetLastError());

                 }

               if(OrderType()==OP_SELL)

                 {
                  if(OrderStopLoss()>High[SellStopCandle]+TrailingStopPad*pips) // User variable to look back x amount of candles.
                     OrderModify(OrderTicket(),OrderOpenPrice(),sell_stop_price,OrderTakeProfit(),0,CLR_NONE);

                  Print(" Candle SL sell error",GetLastError());

                 }

              }

     }
  }

//+------------------------------------------------------------------+
 

You are comparing  the OrderStopLoss to one value, then trying to modify it with a different value.

You don't check whether the new SL value is above the current price (for a Buy) or too close to current price and therefore invalid