Trendlines Indicator Help

 

Hello,


I am working on a trendlines indicator which  use swing highs/lows with a period of 9, but having some issues with some initial break conditions. I have got it to break the trend one it has fully formed, but am having difficulty getting the trend to not draw at all if it is broken in-between the higher swings lows (uptrend)/ lower swing highs (downtrend) 

The two conditions that should stop the trend from being drawn are-

1. If a high forms below the uptrend. Or a low forms above the downtrend. 

2. if the lines travels less than 15 pips per bar (line speed)

I have a tried using a for loop for point 1 but its doesn't seem to work/makes the indicator buggy. Below is part of the code showing uptrend formation -

//UPTREND//
      
      if(s2==0)
        {
         UpTrendBuffer[w]=EMPTY_VALUE;
         if(down(low,w,s_period)) 
           {
            if(s1==0)s1=w;
            else
              {
               if(low[w]<=low[s1])s1=w;
               else
                 {
                  s2=w;
                  speeds=(low[s2]-low[s1])/(s2-s1);
                  for(int i2=r1;i2==r2;i2++) 
                     {
                     if(high[i2]<low[s1]+speeds*(i2-s1))
                        s1=s2;
                        s2=0;
                        break;
                     }
                  if(s1 != 0 && s2!= 0)
                     r1=0;
                     r2=0;
                     ObjectCreate(
                     0,
                     "Up"+(string)time[s1],
                     OBJ_TREND,
                     0,
                     time[s1],
                     low[s1],
                     time[s2],
                     low[s1]+speeds*(s2-s1)
                     );
                     ObjectSetInteger(0,"Up"+(string)time[s1],OBJPROP_COLOR,clrGreen);
                     ObjectSetInteger(0,"Up"+(string)time[s1],OBJPROP_STYLE,STYLE_DASH);
                 }
              }
           }
        }
        
     while(s2!=0 && w<rates_total && !IsStopped())
        {
         DownTrendBuffer[w]=EMPTY_VALUE;
         UpTrendBuffer[w]=low[s1]+speeds*(w-s1);
         if(high[w]<UpTrendBuffer[w])
            {
            s1=0;
            s2=0;
            UpTrendBuffer[w]=EMPTY_VALUE;
            break;
            }
         w++;
        }
     }
   return(rates_total);
  }

attached in the image "bug" its shows is one of the bugs it causes. It seems the drawing of the initial trendline is reversed -

I was also thinking of adding something like this in for breaking if the line speed is too slow -

if((speedr*-1)<min_pipspeed*_Point)
                        s1=0;
                        s2=0;

But this also doesn't work either! Is there something I am missing? Any help would be much appreciated. Attached is an image of the condition for break and full code

Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Drawing Styles
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Drawing Styles
  • www.mql5.com
When creating a custom indicator, you can specify one of 18 types of graphical plotting (as displayed in the main chart window or a chart subwindow), whose values are specified in the ENUM_DRAW_TYPE enumeration. Depending on the drawing style, you may need one to four value buffers (marked as INDICATOR_DATA). If a style admits dynamic...
Files:
Capture3.PNG  47 kb
Capture.PNG  14 kb
Trendlines.mq5  15 kb
Bug.PNG  28 kb