Trend line between peaks

Hi Guys,
Here is some code from @empottasch. This is his Zig zag code and pattern trends. This has been taken from parts of his code. I'm struggling to plot a very simple combination. When there is a HH and then a HL for example.

  1. I cannot seem to connect the peaks
  2. The present trend line also starts 1 bar too late and finishes 1 bar too early.

Any help with this would be much appreciated, as I have tried and read everything about trend lines. There is something I'm obviously not grasping on this concept.
Below is the code and a screenshot, showing that I can plotshapes of the points, but cannot plot the trend line correctly.
Trend%20line

_SECTION_BEGIN( "Trail Stop" );
GfxSetOverlayMode( 1 ); 
GfxSetCoordsMode( 1 ); // bar/price mode (instead of pixel) 
 
tc=ParamList("Display Mode","ZIG|OFF",0);
disp=ParamToggle("Display labels","Off|On",1);
tf=Param("Time Frame (min)",5,1,1440,1);tfrm=in1Minute*tf;

bi=BarIndex();
start = FirstVisibleValue( bi ); 
end   = LastVisibleValue( bi ); 
xx=BarIndex();x=xx;Lx=LastValue(x);
 
HaClose =(O+H+L+C)/4; 
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 ); 
HaHigh = Max( H, Max( HaClose, HaOpen ) ); 
HaLow = Min( L, Min( HaClose, HaOpen ) ); 
 
TimeFrameSet(tfrm);
function vstop_func(U,sameVOL_bull,sameVOL_bear)
{
    trailArray[0]=U[0];
    for(i=1;i<BarCount;i++)
    {
        prev=trailArray[i-1];
  
        if(U[i]>prev AND U[i-1]>prev)
            trailArray[i]=Max(prev,U[i]-sameVOL_bull[i]);
        else if(U[i]<prev AND U[i-1]< prev)
            trailArray[i]=Min(prev,U[i]+sameVOL_bear[i]);
        else if (U[i]>prev)
            trailArray[i]=U[i]-sameVOL_bull[i];
        else
            trailArray[i]=U[i]+sameVOL_bear[i];    
    }
    return trailArray;
}
nm     = ( haHigh - haLow );
U     = EMA(( haOPen + hahigh + halow + haClose ) / 4,2);
rfsctor= EMA( nm, 20 );
sameVOL_bull = 1.5 * rfsctor;
sameVOL_bear = 1.5 * rfsctor;
SAMEnw = vstop_func( U, sameVOL_bull, sameVOL_bear );

ts=IIf(SAMEnw>U,SAMEnw,Null);
tl=IIf(SAMEnw<U,SAMEnw,Null);
TimeFrameRestore();

ts=TimeFrameExpand(ts,tfrm,expandLast);
tl=TimeFrameExpand(tl,tfrm,expandLast);
  
GraphXSpace=5;

lll=LLV(L,BarsSince(!IsEmpty(tl)));lll=IIf(ts,lll,Null);llls=lll;
ttt1=IIf((!IsEmpty(ts) AND IsEmpty(Ref(ts,1))) OR BarIndex()==BarCount-1,1,Null);
ttt=ValueWhen(ttt1,lll,0);ttt=IIf(ts,ttt,Null);ttt=IIf(ttt1,Ref(ttt,-1),ttt);
tr=L==ttt;lll=Sum(tr,BarsSince(!IsEmpty(tl)));
qqq=ValueWhen(ttt1,lll,0);qqq=IIf(ts,qqq,Null);qqq=IIf(ttt1,Ref(qqq,-1),qqq);tr=tr AND lll==qqq;
tr=IIf((!IsEmpty(ts) AND IsEmpty(Ref(ts,1)) AND IsEmpty(Ref(ts,-1))),1,tr);//exception
hhh=HHV(H,BarsSince(!IsEmpty(ts)));hhh=IIf(tl,hhh,Null);hhhs=hhh;
ttt1=IIf((!IsEmpty(tl) AND IsEmpty(Ref(tl,1))) OR BarIndex()==BarCount-1,1,Null);
ttt=ValueWhen(ttt1,hhh,0);ttt=IIf(tl,ttt,Null);ttt=IIf(ttt1,Ref(ttt,-1),ttt);
pk=H==ttt;hhh=Sum(pk,BarsSince(!IsEmpty(ts)));
sss=ValueWhen(ttt1,hhh,0);sss=IIf(tl,sss,Null);sss=IIf(ttt1,Ref(sss,-1),sss);pk=pk AND hhh==sss;
pk=IIf((!IsEmpty(tl) AND IsEmpty(Ref(tl,1)) AND IsEmpty(Ref(tl,-1))),1,pk);//exception
 
px0=ValueWhen(pk,bi,0); tx0=ValueWhen(tr,bi,0);
px1=ValueWhen(pk,bi,1); tx1=ValueWhen(tr,bi,1);
px2=ValueWhen(pk,bi,2); tx2=ValueWhen(tr,bi,2);
px3=ValueWhen(pk,bi,3); tx3=ValueWhen(tr,bi,3);
px4=ValueWhen(pk,bi,4); tx4=ValueWhen(tr,bi,4);

ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);
ph3=ValueWhen(pk,H,3); tl3=ValueWhen(tr,L,3);
ph4=ValueWhen(pk,H,4); tl4=ValueWhen(tr,L,4);

switch(tc)
{
    case("ZIG"):
        aa1=IIf(px0>tx1,(ph0-tl1)/(px0-tx1),0);aa1=IIf(pk,Ref(aa1,-1),aa1);ls1=aa1*(bi-tx1)+tl1;
        bb1=IIf(px0>tx1 AND px1<tx1,1,0);bb1=bb1+Ref(bb1,-1);bb1=IIf(bb1,1,0);ls1=IIf(bb1,ls1,Null);
        Plot(ls1,"",colorBlue,styleLine+styleNoLabel,0,0,0,2,3);

        aa1=IIf(tx0>px1,(tl0-ph1)/(tx0-px1),0);aa1=IIf(tr,Ref(aa1,-1),aa1);ls1=aa1*(bi-px1)+ph1;
        bb1=IIf(tx0>px1 AND tx1<px1,1,0);bb1=bb1+Ref(bb1,-1);bb1=IIf(bb1,1,0);ls1=IIf(bb1,ls1,Null);
        Plot(ls1,"",colorOrange,styleLine+styleNoLabel,0,0,0,2,3);
        break;
            
    case("OFF"):
         Null;    
         break;
    }
    
    //PlotShapes(shapeSmallCircle*tr,colorGreen,0,L,-10);
    //PlotShapes(shapeSmallCircle*pk,colorRed,0,H,10);

    
        ll=tr AND tl1<tl2;
        hl=tr AND tl1>tl2;
        hh=pk AND ph1>ph2;
        lh=pk AND ph1<ph2;
        dt=pk AND ph1==ph2;
        db=tr AND tl1==tl2;
     
      /////////////////////////////////////pattern///////////////////////       
ll_h=IIf(ll,1,0);
hl_h=IIf(hl,2,0);
hh_h=IIf(hh,3,0);
lh_h=IIf(lh,4,0);
dt_h=IIf(dt,5,0);
db_h=IIf(db,6,0);
 
combi=ll_h+hl_h+lh_h+hh_h+dt_h+db_h;
 
t0=ValueWhen(combi,combi,0);
t1=ValueWhen(combi,combi,1);
t2=ValueWhen(combi,combi,2);
t3=ValueWhen(combi,combi,3);
t4=ValueWhen(combi,combi,4);
t5=ValueWhen(combi,combi,5);
///FUTURE Ref//
F0=ValueWhen(combi,combi,0);
F1=ValueWhen(combi,combi,-1);
F2=ValueWhen(combi,combi,-2);
F3=ValueWhen(combi,combi,-3);
F4=ValueWhen(combi,combi,-4);
F5=ValueWhen(combi,combi,-5);
cnt=0;
Yofs =10;
sz= 7; 

    if(disp)
    {
    for ( i = start; i <= end; i++ ) 
    {
             
        if(ll[i]) 
        PlotTextSetFont( "LL", "Arial Black", sz, i, L[i], colorDarkGreen, colorDefault, -Yofs-10);
        if(hl[i]) 
        PlotTextSetFont( "HL", "Arial Black", sz, i, L[i], colorDarkGreen, colorDefault, -Yofs-10 );
        if(db[i]) 
        PlotTextSetFont( "DB", "Arial Black", sz, i, L[i], colorLightBlue, colorDefault, -Yofs-10 );
        if(hh[i]) 
        PlotTextSetFont( "HH", "Arial Black", sz, i, H[i], colorRed, colorDefault, Yofs+10 );
        if(lh[i]) 
        PlotTextSetFont( "LH", "Arial Black", sz, i, H[i], colorRed, colorDefault, Yofs+10 );
        if(dt[i]) 
        PlotTextSetFont( "DT", "Arial Black", sz, i, H[i], colorLightBlue, colorDefault, Yofs+10 );
    }}
    
    
     /////////////////////////////////////pattern///////////////////////       
ll_h=IIf(ll,1,0);
hl_h=IIf(hl,2,0);
hh_h=IIf(hh,3,0);
lh_h=IIf(lh,4,0);
dt_h=IIf(dt,5,0);
db_h=IIf(db,6,0);
 
combi=ll_h+hl_h+lh_h+hh_h+dt_h+db_h;
 
t0=ValueWhen(combi,combi,0);
t1=ValueWhen(combi,combi,1);
t2=ValueWhen(combi,combi,2);
t3=ValueWhen(combi,combi,3);
t4=ValueWhen(combi,combi,4);
t5=ValueWhen(combi,combi,5);
///FUTURE Ref//
F0=ValueWhen(combi,combi,0);
F1=ValueWhen(combi,combi,-1);
F2=ValueWhen(combi,combi,-2);
F3=ValueWhen(combi,combi,-3);
F4=ValueWhen(combi,combi,-4);
F5=ValueWhen(combi,combi,-5);

px0=ValueWhen(pk,bi,0); tx0=ValueWhen(tr,bi,0);
px1=ValueWhen(pk,bi,1); tx1=ValueWhen(tr,bi,1);
px2=ValueWhen(pk,bi,2); tx2=ValueWhen(tr,bi,2);
px3=ValueWhen(pk,bi,3); tx3=ValueWhen(tr,bi,3);
px4=ValueWhen(pk,bi,4); tx4=ValueWhen(tr,bi,4);

ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);
ph3=ValueWhen(pk,H,3); tl3=ValueWhen(tr,L,3);
ph4=ValueWhen(pk,H,4); tl4=ValueWhen(tr,L,4);

///top line///
STARTptTOP   = PK AND HH AND t1==3 AND F1==4;  //START POINT//(1)peak/HH(2)next peak is LH
ENDptTOP     = PK AND LH AND t3==3;            //END POINT//(1)peak/LH(2)Previous peak HH

starts1      = ValueWhen(STARTptTOP,H);
ends1        = ValueWhen(ENDptTOP,H);
PlotShapes( IIf(ENDptTOP, shapeSmallSquare, shapeNone ), colorYellow, 0, H, 20);
PlotShapes( IIf(STARTptTOP, shapeSmallSquare, shapeNone ), colorYellow, 0, H, 20);

///////TOP LINE//////////////////////////////////////////
PKi1a=ValueWhen(STARTptTOP,px0);//pk index 1
PKi2a=ValueWhen(STARTptTOP,px1);//pk index 2
PKv1a=ValueWhen(STARTptTOP,ph0);//pk value 1
PKv2a=ValueWhen(STARTptTOP,ph1);//pk value 2

PKi1a1=ValueWhen(ENDptTOP,px0);//pk index 1
PKi2a1=ValueWhen(ENDptTOP,px1);//pk index 2
PKv1a1=ValueWhen(ENDptTOP,ph0);//pk value 1
PKv2a1=ValueWhen(ENDptTOP,ph1);//pk value 2

//rr=BarsSince(STARTptTOP)>=0 AND BarsSince(ENDptTOP) <=1  ;
//rr=BarsSince(STARTptTOP)>=0  ;
//rr=BarsSince(ENDptTOP) >=0  ;
rr=BarsSince(ENDptTOP) >=0 AND BarsSince(ENDptTOP)>=BarsSince(STARTptTOP);
//rr=BarsSince(STARTptTOP) < extendRange;
idx=IIf(rr,xx-ValueWhen(STARTptTOP,PKi2a),Null);//pk index 2

//aa=(PKv1a1-PKv1a)/(PKi1a1-PKi1a); //(peakvalue1 -peakvalue2) / (peakindex 2 -peakindex 1)
//aa=(PKv2a-PKv2a1)/(PKi2a-PKi1a1); //(peakvalue1 -peakvalue2) / (peakindex 2 -peakindex 1)
aa=(PKv1a1-PKv1a)/(PKi2a-PKi1a1); //(peakvalue1 -peakvalue2) / (peakindex 2 -peakindex 1)

ENDPOINT1   = ValueWhen(STARTptTOP,aa);
STARTPOINT1 = ValueWhen(STARTptTOP,PKv2a);//peak value 2
topTrendline=IIf(idx,STARTPOINT1+ENDPOINT1*idx,Null);
Plot(topTrendline,"",colorYellow,styleLine,0,0,0,0,3);

@Kevin
Put /* … */ ;; from…… ///////TOP LINE//////////////////////////////////////////
then add this...

//taken from @Milosz code
fvbi = Status( "firstvisiblebarindex" );
lvbi = Status( "lastvisiblebarindex" );
for( i = fvbi; i <= Min( lvbi, BarCount - 1 ) ; i++ )
{
    GfxSelectPen( colorGreen, 2 );
    GfxSelectSolidBrush( colorYellow );
    if( STARTptTOP[i])
    {
    y1 = High[i];
    GfxMoveTo( i, y1 );
    }
    if( ENDptTOP[i])
    {
    y2 = High[i];
    GfxLineTo( i, y2 );
    }
}

Beside @Fossil 's solution you can use array based solution to get much faster and better performance .. this code would draw a trendline from HH to LH


TS = 0;
TD1  = Distance_in_Time_1  = ValueWhen(LH,BarsSince(HH),0);
TD2  = Distance_in_Time_2  = ValueWhen(HH,BarsSince(LH),0);
VLH  = Value_of_LH	       = ValueWhen(LH,High,0);
VHH  = Value_of_HH	       = ValueWhen(HH,High,1);
TS   = Trend_Slope	       = (VHH-VLH)/TD1;
TL   = Trend_Line 		   = VHH-TS*BarsSince(HH);
TL   = IIf(ROC(TL,1)>0 OR BarsSince(LH) < TD2 OR BarsSince(LH) < BarsSince(HH),Null,TL);

Plot(TL,"TL",colorWhite,styleLine);        
3 Likes

Thanks very much guys for the responses. I would never have got that. Please could you re look at your array based solution. Its nearly perfect. It plots it correctly, just a few times, it doesn't . Quite strange. Here is the example, circles in yellow seem to not connect the start point.
updated

waiting your feedback

        
TS = 0;
TD  = Distance_in_Time     = ValueWhen(LH,BarsSince(HH),0);
VLH  = Value_of_LH	       = ValueWhen(LH,High,0);
VHH  = Value_of_HH	       = ValueWhen(HH,High,1);
TS   = Trend_Slope	       = (VHH-VLH)/TD;
TL   = Trend_Line 		   = VHH-TS*BarsSince(HH);

Y = ValueWhen(HH,HHV(LH,BarsSince(HH)),0);
X = Flip(HH AND Ref(Y,1) == 1 ,LH);

TL = IIf( X,TL,Null); 

Plot(TL,"TL",colorWhite,styleLine);        
2 Likes

Hi @Sebastian, that is working great now, perfect!!!! Thanks so much. There is a slight issue, It plots all peaks perfectly, but also plots a trend line on the last bar or on the last Peak. Circled in yellow.
trend%20line

ok you can solve this by replacing this line

X = Flip(HH AND Ref(Y,1) == 1 ,LH);

by this one

X = Flip(HH AND Ref(Y,1) == 1 ,( LH or  cum(HH) == lastvalue(cum(HH)));
3 Likes

@Sebastian, thanks you so much for your help. It works perfectly now. I would have never been able to code that. So I'm going to learn what you have done here! Thanks once again much appreciated!
There was a slight Syntax error. I just had to remove the the (
X = Flip(HH AND Ref(Y,1) == 1 , LH or cum(HH) == lastvalue(cum(HH)));

You are welcome @Kevin
The code have one flaw , because sometime HH is not preceded by LH so it will not show you the first trend line from HH to LH , now i leave this mission to you to deal with it , good luck :slight_smile:

Hi @Sebastian, thanks for that insight. I'll take a look. It seems to working well, I haven't yet seen this problem as yet, but Ill keep it in mind.
One very last question. I tried to now extend the line past the last peak. I would like either the extension to extend until the Zig meets the line or when the zig goes below the previous low. I realise this might be quite difficult. I tried a manual version to start, by just using a PARAM to extend the line.
But this doesn't seem to work.

extend=Param("Extend range",100,0,500,1);
X = Flip(HH AND Ref(Y,1) == 1 , LH or cum(HH) == lastvalue(cum(HH)));
X1 = BarsSince(X)  <extendRange ;
```  ![extend|690x370](upload://kwTSSxG5iiTPF60m43aujiH5InJ.png)

That's fairly simple , however the original code must undergo some minor modification to comply correctly with the new added part
So i will post for you the entire code with the new part i added to meet your conditions

_SECTION_BEGIN( "Trail Stop" );
GfxSetOverlayMode( 1 ); 
GfxSetCoordsMode( 1 ); // bar/price mode (instead of pixel) 
 
tc=ParamList("Display Mode","ZIG|OFF",0);
disp=ParamToggle("Display labels","Off|On",1);
tf=Param("Time Frame (min)",5,1,1440,1);tfrm=in1Minute*tf;

bi=BarIndex();
start = FirstVisibleValue( bi ); 
end   = LastVisibleValue( bi ); 
xx=BarIndex();x=xx;Lx=LastValue(x);
 
HaClose =(O+H+L+C)/4; 
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 ); 
HaHigh = Max( H, Max( HaClose, HaOpen ) ); 
HaLow = Min( L, Min( HaClose, HaOpen ) ); 
 
TimeFrameSet(tfrm);
function vstop_func(U,sameVOL_bull,sameVOL_bear)
{
    trailArray[0]=U[0];
    for(i=1;i<BarCount;i++)
    {
        prev=trailArray[i-1];
  
        if(U[i]>prev AND U[i-1]>prev)
            trailArray[i]=Max(prev,U[i]-sameVOL_bull[i]);
        else if(U[i]<prev AND U[i-1]< prev)
            trailArray[i]=Min(prev,U[i]+sameVOL_bear[i]);
        else if (U[i]>prev)
            trailArray[i]=U[i]-sameVOL_bull[i];
        else
            trailArray[i]=U[i]+sameVOL_bear[i];    
    }
    return trailArray;
}
nm     = ( haHigh - haLow );
U     = EMA(( haOPen + hahigh + halow + haClose ) / 4,2);
rfsctor= EMA( nm, 20 );
sameVOL_bull = 1.5 * rfsctor;
sameVOL_bear = 1.5 * rfsctor;
SAMEnw = vstop_func( U, sameVOL_bull, sameVOL_bear );

ts=IIf(SAMEnw>U,SAMEnw,Null);
tl=IIf(SAMEnw<U,SAMEnw,Null);
TimeFrameRestore();

ts=TimeFrameExpand(ts,tfrm,expandLast);
tl=TimeFrameExpand(tl,tfrm,expandLast);
  
GraphXSpace=5;

lll=LLV(L,BarsSince(!IsEmpty(tl)));lll=IIf(ts,lll,Null);llls=lll;
ttt1=IIf((!IsEmpty(ts) AND IsEmpty(Ref(ts,1))) OR BarIndex()==BarCount-1,1,Null);
ttt=ValueWhen(ttt1,lll,0);ttt=IIf(ts,ttt,Null);ttt=IIf(ttt1,Ref(ttt,-1),ttt);
tr=L==ttt;lll=Sum(tr,BarsSince(!IsEmpty(tl)));
qqq=ValueWhen(ttt1,lll,0);qqq=IIf(ts,qqq,Null);qqq=IIf(ttt1,Ref(qqq,-1),qqq);tr=tr AND lll==qqq;
tr=IIf((!IsEmpty(ts) AND IsEmpty(Ref(ts,1)) AND IsEmpty(Ref(ts,-1))),1,tr);//exception
hhh=HHV(H,BarsSince(!IsEmpty(ts)));hhh=IIf(tl,hhh,Null);hhhs=hhh;
ttt1=IIf((!IsEmpty(tl) AND IsEmpty(Ref(tl,1))) OR BarIndex()==BarCount-1,1,Null);
ttt=ValueWhen(ttt1,hhh,0);ttt=IIf(tl,ttt,Null);ttt=IIf(ttt1,Ref(ttt,-1),ttt);
pk=H==ttt;hhh=Sum(pk,BarsSince(!IsEmpty(ts)));
sss=ValueWhen(ttt1,hhh,0);sss=IIf(tl,sss,Null);sss=IIf(ttt1,Ref(sss,-1),sss);pk=pk AND hhh==sss;
pk=IIf((!IsEmpty(tl) AND IsEmpty(Ref(tl,1)) AND IsEmpty(Ref(tl,-1))),1,pk);//exception
 
px0=ValueWhen(pk,bi,0); tx0=ValueWhen(tr,bi,0);
px1=ValueWhen(pk,bi,1); tx1=ValueWhen(tr,bi,1);
px2=ValueWhen(pk,bi,2); tx2=ValueWhen(tr,bi,2);
px3=ValueWhen(pk,bi,3); tx3=ValueWhen(tr,bi,3);
px4=ValueWhen(pk,bi,4); tx4=ValueWhen(tr,bi,4);

ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);
ph3=ValueWhen(pk,H,3); tl3=ValueWhen(tr,L,3);
ph4=ValueWhen(pk,H,4); tl4=ValueWhen(tr,L,4);

switch(tc)
{
    case("ZIG"):
        aa1=IIf(px0>tx1,(ph0-tl1)/(px0-tx1),0);
        aa1=IIf(pk,Ref(aa1,-1),aa1);
        T1=aa1*(bi-tx1)+tl1;
        
        bb1=IIf(px0>tx1 AND px1<tx1,1,0);
        bb1=bb1+Ref(bb1,-1);
        bb1=IIf(bb1,1,0);
        T1=IIf(bb1,T1,Null);
        
        Plot(T1,"",colorBlue,styleLine+styleNoLabel,0,0,0,2,3);

        aa1=IIf(tx0>px1,(tl0-ph1)/(tx0-px1),0);
        aa1=IIf(tr,Ref(aa1,-1),aa1);
        T2=aa1*(bi-px1)+ph1;
        
        bb1=IIf(tx0>px1 AND tx1<px1,1,0);
        bb1=bb1+Ref(bb1,-1);
        bb1=IIf(bb1,1,0);
        T2=IIf(bb1,T2,Null);
        
        Plot(T2,"",colorOrange,styleLine+styleNoLabel,0,0,0,2,3);
        break;
            
    case("OFF"):
         Null;    
         break;
    }
    
    //PlotShapes(shapeSmallCircle*tr,colorGreen,0,L,-10);
    //PlotShapes(shapeSmallCircle*pk,colorRed,0,H,10);

    
        ll=tr AND tl1<tl2;
        hl=tr AND tl1>tl2;
        hh=pk AND ph1>ph2;
        lh=pk AND ph1<ph2;
        dt=pk AND ph1==ph2;
        db=tr AND tl1==tl2;
        


TS   = 0;

TD   = Distance_in_Time    = ValueWhen(LH,BarsSince(HH),0);

VLH  = Value_of_LH	       = ValueWhen(LH,High,0);
VHH  = Value_of_HH	       = ValueWhen(HH,High,1);
TS   = Trend_Slope	       = (VHH-VLH)/(TD+1);
TL   = Trend_Line 		   = VHH-TS*BarsSince(HH);
Y   = Ref(ValueWhen(HH,HHV(LH,BarsSince(HH)),0),1);
X   = Flip( HH AND Y , LH OR Cum(HH) == LastValue(Cum(HH)) );




TL = IIf(X,TL,Null);
Plot(TL,"TL",colorWhite,styleLine);

// New Section .. Extending Trendline That Connect HH to HL





NX   = New_X  	    = (Ref(X,0) == 1 AND Ref(X,-1) == 0)  OR  (Ref(X,0) == 1 AND Ref(X,1) == 0) ;
EVLH = Extended_VLH = ValueWhen(NX == 1,VLH);
EVHH = Extended_VHH = ValueWhen(NX == 1,VHH);
EVLL = Extended_LL  = ValueWhen(LH ,LLV(L,BarsSince(HH)));
ETS  = Extended_TS  = ValueWhen(NX == 1,TS);
ETD  = Extended_TD  = ValueWhen(NX == 1,TD);


Bs  = BarsSince(ROC(EVHH,1) != 0);
ETL = EVHH-((EVHH-EVLH)/ETD)*Bs;

EC  = Extension_Condtion   = Cross( T1 , ETL )  OR  Cross( EVLL , T2 );
EC  = Extension_Condtion   = Flip(Nz(TL),EC);
ETL = IIf(EC,ETL,Null);


Plot(ETL,"NTL",colorGold,styleLine);


      /////////////////////////////////////pattern///////////////////////       
 
cnt=0;
Yofs =10;
sz= 7;
 

if(disp)
{
	for ( i = start; i <= end; i++ ) 
	{
             
        if(ll[i]) 
        PlotTextSetFont( "LL", "Arial Black", sz, i, L[i], colorDarkGreen, colorDefault, -Yofs-10);
        if(hl[i]) 
        PlotTextSetFont( "HL", "Arial Black", sz, i, L[i], colorDarkGreen, colorDefault, -Yofs-10 );
        if(db[i]) 
        PlotTextSetFont( "DB", "Arial Black", sz, i, L[i], colorLightBlue, colorDefault, -Yofs-10 );
        if(hh[i]) 
        PlotTextSetFont( "HH", "Arial Black", sz, i, H[i], colorRed, colorDefault, Yofs+10 );
        if(lh[i]) 
        PlotTextSetFont( "LH", "Arial Black", sz, i, H[i], colorRed, colorDefault, Yofs+10 );
        if(dt[i]) 
        PlotTextSetFont( "DT", "Arial Black", sz, i, H[i], colorLightBlue, colorDefault, Yofs+10 );
	}
}

I am not completely sure that this is what you want so if you have notes on it , please tell me it

2 Likes

Hi @Sebastian, thats nearly correct. In the picture below the yellow one circled is correct, as the price never rises. So the extension can just be where the previous low meets. This one is correct.
If the price rises however (green circles), then the extension just needs to meet the blue line.
The reason for these extensions are
(1) to stop the extension if there is no rise in price, so that the extension doesn't get drawn for a long time, and it is irrelevant (yellow circle).
(2) extend the line if the price rises, as this will be the entry point.
Sorry about this, I hope this is a minor adjustment to this already brilliant work from you.
extend

I have already checked the code again and dicovered some mistakes , here's a revised version

function vstop_func(U,sameVOL_bull,sameVOL_bear)
{
    trailArray[0]=U[0];
    for(i=1;i<BarCount;i++)
    {
        prev=trailArray[i-1];
  
        if(U[i]>prev AND U[i-1]>prev)
            trailArray[i]=Max(prev,U[i]-sameVOL_bull[i]);
        else if(U[i]<prev AND U[i-1]< prev)
            trailArray[i]=Min(prev,U[i]+sameVOL_bear[i]);
        else if (U[i]>prev)
            trailArray[i]=U[i]-sameVOL_bull[i];
        else
            trailArray[i]=U[i]+sameVOL_bear[i];    
    }
    return trailArray;
}


GfxSetOverlayMode( 1 ); 
GfxSetCoordsMode( 1 ); // bar/price mode (instead of pixel) 
 
tc=ParamList("Display Mode","ZIG|OFF",0);
disp=ParamToggle("Display labels","Off|On",1);
tf=Param("Time Frame (min)",5,1,1440,1);tfrm=in1Minute*tf;

bi=BarIndex();
start = FirstVisibleValue( bi ); 
end   = LastVisibleValue( bi ); 
xx=BarIndex();x=xx;Lx=LastValue(x);
 
HaClose =(O+H+L+C)/4; 
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 ); 
HaHigh = Max( H, Max( HaClose, HaOpen ) ); 
HaLow = Min( L, Min( HaClose, HaOpen ) ); 
 
TimeFrameSet(tfrm);



nm     = ( haHigh - haLow );
U     = EMA(( haOPen + hahigh + halow + haClose ) / 4,2);
rfsctor= EMA( nm, 20 );
sameVOL_bull = 1.5 * rfsctor;
sameVOL_bear = 1.5 * rfsctor;
SAMEnw = vstop_func( U, sameVOL_bull, sameVOL_bear );

ts=IIf(SAMEnw>U,SAMEnw,Null);
tl=IIf(SAMEnw<U,SAMEnw,Null);
TimeFrameRestore();

ts=TimeFrameExpand(ts,tfrm,expandLast);
tl=TimeFrameExpand(tl,tfrm,expandLast);
  
GraphXSpace=5;

lll=LLV(L,BarsSince(!IsEmpty(tl)));lll=IIf(ts,lll,Null);llls=lll;
ttt1=IIf((!IsEmpty(ts) AND IsEmpty(Ref(ts,1))) OR BarIndex()==BarCount-1,1,Null);
ttt=ValueWhen(ttt1,lll,0);ttt=IIf(ts,ttt,Null);ttt=IIf(ttt1,Ref(ttt,-1),ttt);
tr=L==ttt;lll=Sum(tr,BarsSince(!IsEmpty(tl)));
qqq=ValueWhen(ttt1,lll,0);qqq=IIf(ts,qqq,Null);qqq=IIf(ttt1,Ref(qqq,-1),qqq);tr=tr AND lll==qqq;
tr=IIf((!IsEmpty(ts) AND IsEmpty(Ref(ts,1)) AND IsEmpty(Ref(ts,-1))),1,tr);//exception
hhh=HHV(H,BarsSince(!IsEmpty(ts)));hhh=IIf(tl,hhh,Null);hhhs=hhh;
ttt1=IIf((!IsEmpty(tl) AND IsEmpty(Ref(tl,1))) OR BarIndex()==BarCount-1,1,Null);
ttt=ValueWhen(ttt1,hhh,0);ttt=IIf(tl,ttt,Null);ttt=IIf(ttt1,Ref(ttt,-1),ttt);
pk=H==ttt;hhh=Sum(pk,BarsSince(!IsEmpty(ts)));
sss=ValueWhen(ttt1,hhh,0);sss=IIf(tl,sss,Null);sss=IIf(ttt1,Ref(sss,-1),sss);pk=pk AND hhh==sss;
pk=IIf((!IsEmpty(tl) AND IsEmpty(Ref(tl,1)) AND IsEmpty(Ref(tl,-1))),1,pk);//exception
 
px0=ValueWhen(pk,bi,0); tx0=ValueWhen(tr,bi,0);
px1=ValueWhen(pk,bi,1); tx1=ValueWhen(tr,bi,1);
px2=ValueWhen(pk,bi,2); tx2=ValueWhen(tr,bi,2);
px3=ValueWhen(pk,bi,3); tx3=ValueWhen(tr,bi,3);
px4=ValueWhen(pk,bi,4); tx4=ValueWhen(tr,bi,4);

ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);
ph3=ValueWhen(pk,H,3); tl3=ValueWhen(tr,L,3);
ph4=ValueWhen(pk,H,4); tl4=ValueWhen(tr,L,4);

switch(tc)
{
    case("ZIG"):
        aa1=IIf(px0>tx1,(ph0-tl1)/(px0-tx1),0);
        aa1=IIf(pk,Ref(aa1,-1),aa1);
        T1=aa1*(bi-tx1)+tl1;
        
        bb1=IIf(px0>tx1 AND px1<tx1,1,0);
        bb1=bb1+Ref(bb1,-1);
        bb1=IIf(bb1,1,0);
        T1=IIf(bb1,T1,Null);
        
        Plot(T1,"",colorBlue,styleLine+styleNoLabel,0,0,0,2,3);

        aa1=IIf(tx0>px1,(tl0-ph1)/(tx0-px1),0);
        aa1=IIf(tr,Ref(aa1,-1),aa1);
        T2=aa1*(bi-px1)+ph1;
        
        bb1=IIf(tx0>px1 AND tx1<px1,1,0);
        bb1=bb1+Ref(bb1,-1);
        bb1=IIf(bb1,1,0);
        T2=IIf(bb1,T2,Null);
        
        Plot(T2,"",colorOrange,styleLine+styleNoLabel,0,0,0,2,3);
        break;
            
    case("OFF"):
         Null;    
         break;
    }
    
    //PlotShapes(shapeSmallCircle*tr,colorGreen,0,L,-10);
    //PlotShapes(shapeSmallCircle*pk,colorRed,0,H,10);

    
        ll=tr AND tl1<tl2;
        hl=tr AND tl1>tl2;
        hh=pk AND ph1>ph2;
        lh=pk AND ph1<ph2;
        dt=pk AND ph1==ph2;
        db=tr AND tl1==tl2;

      /////////////////////////////////////pattern///////////////////////       
 
cnt=0;
Yofs =10;
sz= 7;
 

if(disp)
{
	for ( i = start; i <= end; i++ ) 
	{
             
        if(ll[i]) 
        PlotTextSetFont( "LL", "Arial Black", sz, i, L[i], colorDarkGreen, colorDefault, -Yofs-10);
        if(hl[i]) 
        PlotTextSetFont( "HL", "Arial Black", sz, i, L[i], colorDarkGreen, colorDefault, -Yofs-10 );
        if(db[i]) 
        PlotTextSetFont( "DB", "Arial Black", sz, i, L[i], colorLightBlue, colorDefault, -Yofs-10 );
        if(hh[i]) 
        PlotTextSetFont( "HH", "Arial Black", sz, i, H[i], colorRed, colorDefault, Yofs+10 );
        if(lh[i]) 
        PlotTextSetFont( "LH", "Arial Black", sz, i, H[i], colorRed, colorDefault, Yofs+10 );
        if(dt[i]) 
        PlotTextSetFont( "DT", "Arial Black", sz, i, H[i], colorLightBlue, colorDefault, Yofs+10 );
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
TS   = 0;

TD   = Distance_in_Time    = ValueWhen(LH,BarsSince(HH),0);

VLH  = Value_of_LH	       = ValueWhen(LH,High,0);
VHH  = Value_of_HH	       = ValueWhen(HH,High,1);
TS   = Trend_Slope	       = (VHH-VLH)/(TD+1);
TL   = Trend_Line 		   = VHH-TS*BarsSince(HH);
Y    = Ref(ValueWhen(HH,HHV(LH,BarsSince(HH)),0),1);
X    = Flip( HH AND Y , LH OR Cum(HH) == LastValue(Cum(HH)) );




TL = IIf(X,TL,Null);
Plot(TL,"TL",colorWhite,styleLine);

// New Section .. Extending Trendline That Connect HH to HL





NX   = New_X  	    = ((Ref(X,0) == 1 AND Ref(X,-1) == 0)  OR  (Ref(X,0) == 1 AND Ref(X,1) == 0)) ;
EVLH = Extended_VLH = ValueWhen(NX == 1,VLH);
EVHH = Extended_VHH = ValueWhen(NX == 1,VHH);
EVLL = Extended_LL  = ValueWhen(LH AND Ref(NX,-1) ,LLV(L,BarsSince(HH)));
ETS  = Extended_TS  = ValueWhen(NX == 1,TS);
ETD  = Extended_TD  = ValueWhen(NX == 1,TD);


Bs  = BarsSince(ROC(EVHH,1) != 0);
ETL = EVHH-((EVHH-EVLH)/ETD)*Bs;
EC  = Extension_Condtion   = IIf(IsTrue(T2),Cross(EVLL,T2),IIf( Cross(T1,ETL) ,1,0));
EC  = Extension_Condtion   = Flip(LH AND Ref(NX,-1),EC OR HH);
ETL = IIf(EC,ETL,Null);


Plot(ETL,"NTL",colorGold,styleLine);

when i overlaid the indicator on some charts , I found a special case and i think that you may be interested to take a look over it

There's a question in the first image with two option,

if you going to choose the first option ( Green Dot ) , this code will satisfy your need , however selecting the second options will force you to work with loop , array would have nothing to do here

Now the choice is yours

1
2

1 Like

Hi @Sebastian
This is PERFECT!!!! Thank you Thank you Thank you!!!! Just one small issue, if you could check on your side. I just noticed that there should be a line drawn if conditions are met (HH, LH) even on the current bar. Screenshot to show you what I'm saying. extend

But you asked me to eliminate the last trend line from HH to LH .. right ?

Yes I did, as that was plotting a trend line without the LH to connect it. Sorry if I was not clear enough. it that example you will see the line plots from the HH, but there is no LH. Hopefully that makes sense?

Hi @Sebastian,
I just wanted to check if there was a way to be able to plot the last HH to LH lines. Here is examples, circled in red. I hope its not too much of an issue to solve?
Trend%20line

Hi @Sebastian
Not to worry. I think I fixed it.

Hello everyone, this topic is really helpfull to me!

would some one please add trend line between up peaks like in picture ?

29%20AM

Using search:
https://forum.amibroker.com/search?q=automatic%20trendlines