Trailing STOP loss code

 

for trailing stop loss, i am running below code but getting modified error and update SL to 0.0001 always. Can someone please advice who to get this fixed.

void OnTick(){
  if(PositionsTotal()>0)  {
      //Print(__FUNCTION__," > Pos #",PositionsTotal()," was modified..");
      TrailSL();
       
     }
  }
//+------------------------------------------------------------------+

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

void TrailSL(){
  for ( int i = PositionsTotal()-1;i>=0;i--){
      
      ulong posTicket = PositionGetTicket(i);
      if (NewBar()){     
         if(PositionSelectByTicket(posTicket)){
            if(PositionGetString(POSITION_SYMBOL)==_Symbol){
               Print(__FUNCTION__," > Pos #",posTicket," was modified..");
               
               double posSl = PositionGetDouble(POSITION_SL);
               double posTp = PositionGetDouble(POSITION_TP);
               double posOp = PositionGetDouble(POSITION_PRICE_OPEN);
               
               double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
               double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
                           
               Print(i," -> ",posTicket," ",PositionGetString(POSITION_SYMBOL)," ",_Symbol); 
                           
               if(PositionGetInteger(POSITION_TYPE)== POSITION_TYPE_BUY){
                  if(bid > posOp+TslTriggerPoints * _Point){
                     double sl = bid-TslPoints *_Point;
                     
                     if(sl> posSl && posOp >= sl){
                                          
                        if(trade.PositionModify(posTicket,sl,posTp)){
                           Print(__FUNCTION__," > Pos #",posTicket," was modified..");
                      }
                     }
                  }
                             
                  
               }else if(PositionGetInteger(POSITION_TYPE)== POSITION_TYPE_SELL){
                  if(ask < posOp-TslTriggerPoints * _Point){
                     double sl = ask+TslPoints *_Point;
                     
                     if(sl< posSl && sl >= posOp){
                                          
                        if(trade.PositionModify(posTicket,sl,posTp)){
                           Print(__FUNCTION__," > Pos #",posTicket," was modified..");
                        }
                     }  
                  } 
               }
            }
         }
      } 
   }

}  

 
                     double sl = bid-TslPoints *_Point;
                     
                     if(sl> posSl && posOp >= sl){
  1. Compute the new value, then check that you are moving the existing value at least a tick (in the currect direction).
              What is a TICK? - MQL4 programming forum (2014)

    1. Floating point has an infinite number of decimals, it's you were not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
                Double-precision floating-point format - Wikipedia, the free encyclopedia

      See also The == operand. - MQL4 programming forum (2013)

    2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

    3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
                On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum (2011)

      And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

    4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
                Trailing Bar Entry EA - MQL4 programming forum (2013)
                Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

    5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
                (MT4 2013)) (MT5 2022))

    6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
                MT4:NormalizeDouble - MQL5 programming forum (2017)
                How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

    7. Prices you get from the terminal are already correct (normalized).

    8. PIP, Point, or Tick are all different in general.
                What is a TICK? - MQL4 programming forum (2014)

  2.   for ( int i = PositionsTotal()-1;i>=0;i--){
          
          ulong posTicket = PositionGetTicket(i);
          if (NewBar()){  

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              MT4: New candle - MQL4 programming forum #3 (2014)
              MT5: Accessing variables - MQL4 programming forum #3 (2022)

    I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011)

 
Thank you William Roeder, will try to edit.