Trailing-stoploss with breakeven risk-free function

 

Hey there!

I'm working on an MQL5 trading algorithm but facing a challenge. I want to activate a trailing stop feature only after the stop-loss has been adjusted to breakeven. However, I'm having trouble getting it to work as intended.

Can anyone share insights or suggest alternative approaches to implement this effectively?

Thanks a bunch!

#include <Trade/Trade.mqh>
CTrade trade;


input int TslTriggerPoints=  300; //StopLoss Trigger in Points
input int BreakEvenGap = 50;
input double tradingrange_distance_filer =0.0010; 
input int TslPoints = 150; //Initial StopLoss size in points
input int tp_size = 550; //Take_Profit size in Points
input int Slow_Ma_Period = 101;
input int Fast_Ma_Period = 41;
input int More_Than_Profit_Canceler_filter = 50;
input int Less_Than_loss_Canceler_filter = -100;
              
int OnInit(){;return(INIT_SUCCEEDED);}   
   
void OnDeinit(const int reason){}


void OnTick()
{
                  
            for(int i = PositionsTotal()-1; i>=0;i--)//for loop for all opened positions
                {
                   ulong PosTick = PositionGetTicket(i);
                   if(PositionSelectByTicket(PosTick))//checks the position number selected
                        {
                           if (PositionGetString(POSITION_SYMBOL)==_Symbol)//checks whether the ticket symbol matches the chart symbol or not
                           {   
                                  double posOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
                                  double posSl = PositionGetDouble(POSITION_SL);
                                  double posTp = PositionGetDouble(POSITION_TP);
                                  double bid  = SymbolInfoDouble(_Symbol, SYMBOL_BID);
                                  double ask  = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
                                  double Loss = PositionGetDouble(POSITION_PROFIT) / SymbolInfoDouble(_Symbol, SYMBOL_POINT);
                              
                                 if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)//if the positin was a BUY
                                   {
                                       
                                       if(bid > posOpenPrice + TslTriggerPoints * _Point)
                                         {
                                             double breakevensl = posOpenPrice + BreakEvenGap *_Point;
                                             if(posSl<  breakevensl)
                                               {
                                                  if(trade.PositionModify(PosTick, breakevensl, posTp))//EXECUTE THE SL AND NEW TP USING CTRADE$
                                                      {
                                                         Print(__FUNCTION__,"> Pos # ", PosTick, "was modified by normal...");
                                                      }
                                               }
                                         
                                          }
                                    
                                    else if (bid > posOpenPrice +TslPoints * _Point)
                                    {
                                       double sl = bid - TslPoints *_Point;
                                       if (sl > posSl)
                                       {
                                        if(trade.PositionModify(PosTick, sl, posTp))//EXECUTE THE SL AND NEW TP USING CTRADE$
                                                      {
                                                         Print(__FUNCTION__,"> Pos # ", PosTick, "was modified by normal...");
                                                      }
                                       }
                                    }
                                    }
                  
                                 else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)//if the position is sell then:
                                     {
                                         if(ask < posOpenPrice -TslTriggerPoints *_Point)//if in sell the market move in good direciton, then:
                                           {
                                              double breakevensl = posOpenPrice - BreakEvenGap *_Point;
                                              
                                               if(posSl >breakevensl)
                                                  {
                                                      if(trade.PositionModify(PosTick, breakevensl, posTp))
                                                      {
                                                         Print(__FUNCTION__,"> Pos # ", PosTick, "was modified...");
                                                      }
                                                  }
                                                 
                                            
                                            }
                                      else if (ask < posOpenPrice - TslPoints * _Point)
                                      {
                                      double sl = ask + TslPoints *_Point;
                                      if (sl < posSl || posSl == 0)
                                      {
                                      if(trade.PositionModify(PosTick, sl, posTp))
                                                      {
                                                         Print(__FUNCTION__,"> Pos # ", PosTick, "was modified...");
                                                      }
                                      }
                                      }
                                      }               
      
                              }
                           }
                        }//the following is the strategy...
Reason: