Stoploss moving on every tick

 

Hello, everyone. Ich have coded this here:

for(int i=0;i<OrdersTotal();i++)
     {
      bool select = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);              //Iteration through every trade
      if(OrderMagicNumber()==1 && OrderSymbol()==Symbol())                 //if Trade==BUY
        {
         if(Close[1]>OrderStopLoss()+atr && Close[1]>OrderOpenPrice()+atr) //is the price already greater than the opening price+ATR,
           {                                                               // and than the stoploss+ATR?
            bool order = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderStopLoss()+atr-0.0005,5),0,0,clrAquamarine);
            if(!order){Alert(GetLastError());}
           }
         else
           {
            return;
           }
        }
}


The problem is that the stoploss keeps moving on every tick. I know it is because of the "-0.0005", but I would like to keep that margin. What could I do to set the stoploss at that level, but prevent it from moving on every tick?

Thank you very much in advance.

 
     if(OrderMagicNumber()==1 && OrderSymbol()==Symbol())                 //if Trade==BUY

There is no check for whether the trade is a BUY.

     if(Close[1]>OrderStopLoss()+atr && Close[1]>OrderOpenPrice()+atr)

 Why are you using Close[1]? I think that you should be using OrderClosePrice().

 

For a BUY, you should be checking that the new SL is higher than the current SL, lower for a SELL. 

Reason: