Auto Trend-Line Support

Hello,
I am facing a problem to plot a line from one HH to other HH and from LL to other LL. I tried LineArray, But I couldn't do it. I am providing the code below. Can anyone please plot a extension line between two HH ( Previous two ) and LL ( Previous two ).

_SECTION_BEGIN("Formula 15");
// Fractals
// AFL code by Edward Pottasch, Apr 2013
RequestTimedRefresh(0.1,True); 
xx=BarIndex();x=xx;Lx=LastValue(x);
rightStrength=Param("Fractal Pivot Right side Strength",5,2,50,1); 
leftStrength=Param("Fractal Pivot Left side Strength",10,2,50,1); 
tf=Param("Time Frame (min)",5,1,1440,1);tfrm=in1Minute*tf;
CleanPivots=ParamToggle("Force Pivots to Alternate","Off|On",0);
Cttop=Param("Code Title Top",15,1,250,1); 
Ctspac=Param("Code Title Spacing",15,1,250,1);
dispLabels=ParamToggle("Display Labels","Off|On",1);
LabelColor=ParamColor("Label Color",colorYellow);
GfxSetTextColor(ParamColor("Code Title Color",colorLightGrey));
backGroundColor=ParamColor("Chart Background Color",colorBlack);
 
TimeFrameSet(tfrm); 
pk=H>Ref(HHV(H,leftStrength),-1) AND Ref(HHV(H,rightStrength),rightStrength)<=H;
tr=L<Ref(LLV(L,leftStrength),-1) AND Ref(LLV(L,rightStrength),rightStrength)>=L;
 
px0=ValueWhen(pk,x,0); tx0=ValueWhen(tr,x,0);
px1=ValueWhen(pk,x,1); tx1=ValueWhen(tr,x,1);
px2=ValueWhen(pk,x,2); tx2=ValueWhen(tr,x,2);
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);
 
if(CleanPivots)
{
    pk=IIf((ph0>=ph1 AND tx0>px0 AND px0!=px1) 
    OR (ph1<ph2 AND px2>tx1)
    OR (ph0>=ph1 AND tx0<px0 AND tx0==tx1 AND px0!=px1)
    ,False,pk);
    tr=IIf((tl0<=tl1 AND px0>tx0 AND tx0!=tx1) 
    OR (tl1>tl2 AND tx2>px1)
    OR (tl0<=tl1 AND px0<tx0 AND px0==px1 AND tx0!=tx1)
    ,False,tr);
     
    px0=ValueWhen(pk,x,0); tx0=ValueWhen(tr,x,0);
    px1=ValueWhen(pk,x,1); tx1=ValueWhen(tr,x,1);
    px2=ValueWhen(pk,x,2); tx2=ValueWhen(tr,x,2);
    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);   
}
 
pkh=IIf(pk,H,Null);trl=IIf(tr,L,Null);
TimeFrameRestore();
fact=Nz(Max(tfrm/60,Interval()/60)/(Interval()/60));
if(fact==0)fact=1;
Lkbk=Nz(tfrm/Interval());
if(Lkbk>1)
{
    pk=TimeFrameExpand(pk,tfrm,expandFirst);
    pkh=TimeFrameExpand(pkh,tfrm,expandFirst);
    pkhs=IIf(!IsEmpty(pkh),1,0);pkhs=pkhs-Ref(pkhs,-1);
    pk=pk AND H==pkh;
    cond1=Sum(pk,BarsSince(pkhs==1)+1)==1 AND pk;
    pk=pk AND cond1;
      
    tr=TimeFrameExpand(tr,tfrm,expandFirst);    
    trl=TimeFrameExpand(trl,tfrm,expandFirst);
    trls=IIf(!IsEmpty(trl),1,0);trls=trls-Ref(trls,-1);
    tr=tr AND L==trl;
    cond1=Sum(tr,BarsSince(trls==1)+1)==1 AND tr;
    tr=tr AND cond1;
}
px0=ValueWhen(pk,x,0); tx0=ValueWhen(tr,x,0);
px1=ValueWhen(pk,x,1); tx1=ValueWhen(tr,x,1);
px2=ValueWhen(pk,x,2); tx2=ValueWhen(tr,x,2);
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);   
 
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;
 
GraphXSpace=5;SetChartBkColor(backGroundColor);SetChartOptions(0,chartShowDates);
SetBarFillColor(IIf(C>O,colorGreen,IIf(C<=O,colorRed,colorLightGrey)));
Plot(C,"Price",IIf(C>O,colorDarkGreen,IIf(C<=O,colorDarkRed,colorLightGrey)),64,0,0,0,0);
Plot(pkh,"",colorRed,styleThick,0,0,0,-1);
Plot(trl,"",colorBrightGreen,styleThick,0,0,0,-1);
PlotShapes(shapeSmallCircle*tr,IIf(Lx-ValueWhen(tr,x)>=rightStrength*fact,ColorRGB(0,100,0),colorWhite),0,L,-10);
PlotShapes(shapeSmallCircle*pk,IIf(Lx-ValueWhen(pk,x)>=rightStrength*fact,ColorRGB(255,0,0),colorWhite),0,H,10);
    
qq=Interval()/60;
if(qq < 60){tf=" min";tt=qq;}
else if(qq >= 60 AND qq < 1440){tf=" hrs";tt=qq/60;}
else if(qq >= 1440){tf=" days";tt=(qq/60)/24;}
qq=Max(tfrm/60,Interval()/60);
if(qq < 60){tfa=" min";tta=qq;}
else if(qq >= 60 AND qq < 1440){tfa=" hrs";tta=qq/60;}
else if(qq >= 1440){tfa=" days";tta=(qq/60)/24;}
 
GfxSetOverlayMode(0);
GfxSetBkMode(1);
GfxSelectFont("Tahoma",11,200);
GfxTextOut("Right Strenght: "+rightStrength,5,Cttop);
GfxTextOut("Left Strenght: "+leftStrength,5,Cttop+Ctspac);
GfxTextOut("Chart Timeframe: "+tt+""+tf,5,Cttop+Ctspac*2);
GfxTextOut("Pivot Timeframe: "+tta+""+tfa,5,Cttop+Ctspac*3);
 
abcdy_up=27;
abcdy_dn=15;
function GetVisibleBarCount() 
{
    lvb=Status("lastvisiblebar");
    fvb=Status("firstvisiblebar");
    return Min(lvb-fvb,BarCount-fvb);
} 
function GfxConvertPixelToValueY(Pixels) 
{
    local Miny,Maxy,pxchartbottom,pxchartheight;
    Miny=Status("axisminy");
    Maxy=Status("axismaxy");
    pxchartbottom=Status("pxchartbottom");
    pxchartheight=Status("pxchartheight");
    fac=pxchartheight/Pixels;
    Value=(Maxy-Miny)/fac;
    return Nz(Value);
} 
function GfxConvertBarToPixelX(bar) 
{ 
    lvb=Status("lastvisiblebar"); 
    fvb=Status("firstvisiblebar"); 
    pxchartleft=Status("pxchartleft"); 
    pxchartwidth=Status("pxchartwidth"); 
    return Nz(pxchartleft+bar*pxchartwidth/(lvb-fvb+1)); 
} 
function GfxConvertValueToPixelY(value) 
{
    local Miny,Maxy,pxchartbottom,pxchartheight; 
    Miny=Status("axisminy");
    Maxy=Status("axismaxy");
    pxchartbottom=Status("pxchartbottom");
    pxchartheight=Status("pxchartheight");
    return Nz(pxchartbottom-floor(0.5+(Value-Miny)*pxchartheight/(Maxy-Miny))); 
} 
AllVisibleBars=GetVisibleBarCount();
fvb=Status("firstvisiblebar");
abcdy_up=GfxConvertPixelToValueY(abcdy_up);
abcdy_dn=GfxConvertPixelToValueY(abcdy_dn);
 
for(i=0;i<AllVisibleBars;i++) 
{
    GfxSelectFont("Tahoma",8,500);GfxSetBkMode(1); 
    if(ll[i+fvb] AND dispLabels)
    {
        lvix=i+fvb;
        GfxSetTextColor(LabelColor);GfxTextOut("LL",
        GfxConvertBarToPixelX(i)-3,GfxConvertValueToPixelY(L[lvix]-abcdy_dn)); 
    }
    if(hl[i+fvb] AND dispLabels)
    {
        lvix=i+fvb;
        GfxSetTextColor(LabelColor);GfxTextOut("HL",
        GfxConvertBarToPixelX(i)-3,GfxConvertValueToPixelY(L[lvix]-abcdy_dn)); 
    }   
    if(db[i+fvb] AND dispLabels)
    {
        lvix=i+fvb;
        GfxSetTextColor(LabelColor);GfxTextOut("DB",
        GfxConvertBarToPixelX(i)-3,GfxConvertValueToPixelY(L[lvix]-abcdy_dn)); 
    }       
    if(hh[i+fvb] AND dispLabels)
    {
        lvix=i+fvb;
        GfxSetTextColor(LabelColor);GfxTextOut("HH",
        GfxConvertBarToPixelX(i)-5,GfxConvertValueToPixelY(H[lvix]+abcdy_up)); 
    }  
    if(lh[i+fvb] AND dispLabels)
    {
        lvix=i+fvb;
        GfxSetTextColor(LabelColor);GfxTextOut("LH",
        GfxConvertBarToPixelX(i)-5,GfxConvertValueToPixelY(H[lvix]+abcdy_up)); 
    }   
    if(dt[i+fvb] AND dispLabels)
    {
        lvix=i+fvb;
        GfxSetTextColor(LabelColor);GfxTextOut("DT",
        GfxConvertBarToPixelX(i)-5,GfxConvertValueToPixelY(H[lvix]+abcdy_up)); 
    }
}
_SECTION_END();

Thank You in Advance.

is it just for a visual indicator? Below some code that gives an example when using GFX. At the bottom of the code I add the lines between pivot HH's. Maybe it will help you further

rightstrength = Param( "Right Strength", 5, 2, 50, 1 );
leftstrength = Param( "Left Strength", 10, 2, 50, 1 );
labelsswitch = ParamToggle( "Show Labels", "Off|On", 1 );
trendlineswitch = ParamToggle( "Show Trendlines", "Off|On", 1 );
alternatepivotswitch = ParamToggle( "Alternate Pivots", "No|Yes", 0 );

upColor = ColorRGB( 0, 255, 0 );
dnColor = ColorRGB( 255, 0, 0 );

pk = tr = 0;
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

pk = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
pk = pk AND LastValue( bi ) - ValueWhen( pk, bi ) > rightstrength;
tr = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;
tr = tr AND LastValue( bi ) - ValueWhen( tr, bi ) > rightstrength;

if( alternatepivotswitch )
{
// alternate pivots
    pkHigh = 0;
    pkHighIndex = 0;
    trLow = 1e10;
    trLowIndex = 0;

    for( i = 0; i < BarCount; i++ )
    {
        if( pk[i] AND tr[i] )
        {
            pk[i] = tr[i] = 0;
        }

        if( pk[i] AND H[i] >= pkHigh )
        {
            pk[pkHighIndex] = 0;
            trLow = 1e10;
            trLowIndex = 0;
            pkHigh = H[i];
            pkHighIndex = i;
        }
        else
            if( pk[i] AND H[i] < pkHigh )
            {
                pk[i] = 0;
                trLow = 1e10;
                trLowIndex = 0;
            }

        if( tr[i] AND L[i] <= trLow )
        {
            tr[trLowIndex] = 0;
            pkHigh = 0;
            pkHighIndex = 0;
            trLow = L[i];
            trLowIndex = i;
        }
        else
            if( tr[i] AND L[i] > trLow )
            {
                tr[i] = 0;
                pkHigh = 0;
                pkHighIndex = 0;
            }
    }
}

for( i = 0; i < 3; i++ )
{
    VarSet( "px" + i, ValueWhen( pk, bi, i ) );
    VarSet( "tx" + i, ValueWhen( tr, bi, i ) );
    VarSet( "ph" + i, ValueWhen( pk, H, i ) );
    VarSet( "tl" + i, ValueWhen( tr, L, i ) );
}

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;

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );
PlotShapes( shapeSmallCircle * tr, colorGreen, 0, L, -10 );
PlotShapes( shapeSmallCircle * pk, colorRed, 0, H, 10 );

ft = "Arial Black";
clr = colorDefault;
sz = 8;

if( labelsswitch )
{
    for( i = fvb; i <= lvb; i++ )
    {
        str1 = "";

        if( pk[i] )
        {
            if( dt[i] )
                str1 = "DT";
            else
                if( hh[i] )
                    str1 = "HH";
                else
                    if( lh[i] )
                        str1 = "LH";

            PlotTextSetFont( str1 + " (" + H[i] + ")", ft, sz, i, H[i], colorRed, clr, sz * 3 );
        }

        if( tr[i] )
        {
            str1 = "";

            if( db[i] )
                str1 = "DB";
            else
                if( ll[i] )
                    str1 = "LL";
                else
                    if( hl[i] )
                        str1 = "HL";

            PlotTextSetFont( str1 + " (" + L[i] + ")", ft, sz, i, L[i], colorGreen, clr, -sz * 4 );
        }
    }
}

// adding lines between pivots
GfxSetZOrder( 0 );
GfxSetCoordsMode( 1 );

idxhh1 = ValueWhen( hh, bi, 1 ); // index at HH
idxhh2 = ValueWhen( hh, bi, 2 ); // index at prior HH

if( trendlineswitch )
{
    for( i = lvb; i > fvb; i-- )
    {
        if( hh[i] )
        {
            // coordinates at HH
            x0 = idxhh1[i]; // is same as i
            y0 = H[i];

            // coordinates at prior HH
            x1 = Nz( idxhh2[i] );
            y1 = H[x1];

            // draw the line
            GfxSelectPen( colorGreen, 2 );
            GfxMoveTo( x0, y0 );
            GfxLineTo( x1, y1 );
        }
    }
}

1 Like

Thank you so much @empottasch, for allotting some time to help me. Sir, I am new to AFL, So i am facing many problems with the code. Sir, Can you please modify that code to plot lines between

  1. Current HH to previous HH greater than current HH.
  2. Current LL to previous LL lesser than current LL.

And sir, those lines can be extended with Dotted Lines ( i.e in Blank Area ) ?

I will be very grateful to you if you resolve this issue.

Thank You So Much.

you could then use LineArray

rightstrength = Param( "Right Strength", 5, 2, 50, 1 );
leftstrength = Param( "Left Strength", 10, 2, 50, 1 );
labelsswitch = ParamToggle( "Show Labels", "Off|On", 1 );
trendlineswitch = ParamToggle( "Show Trendlines", "Off|On", 1 );
alternatepivotswitch = ParamToggle( "Alternate Pivots", "No|Yes", 0 );

upColor = ColorRGB( 0, 255, 0 );
dnColor = ColorRGB( 255, 0, 0 );

pk = tr = 0;
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

pk = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
pk = pk AND LastValue( bi ) - ValueWhen( pk, bi ) > rightstrength;
tr = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;
tr = tr AND LastValue( bi ) - ValueWhen( tr, bi ) > rightstrength;

if( alternatepivotswitch )
{
// alternate pivots
    pkHigh = 0;
    pkHighIndex = 0;
    trLow = 1e10;
    trLowIndex = 0;

    for( i = 0; i < BarCount; i++ )
    {
        if( pk[i] AND tr[i] )
        {
            pk[i] = tr[i] = 0;
        }

        if( pk[i] AND H[i] >= pkHigh )
        {
            pk[pkHighIndex] = 0;
            trLow = 1e10;
            trLowIndex = 0;
            pkHigh = H[i];
            pkHighIndex = i;
        }
        else
            if( pk[i] AND H[i] < pkHigh )
            {
                pk[i] = 0;
                trLow = 1e10;
                trLowIndex = 0;
            }

        if( tr[i] AND L[i] <= trLow )
        {
            tr[trLowIndex] = 0;
            pkHigh = 0;
            pkHighIndex = 0;
            trLow = L[i];
            trLowIndex = i;
        }
        else
            if( tr[i] AND L[i] > trLow )
            {
                tr[i] = 0;
                pkHigh = 0;
                pkHighIndex = 0;
            }
    }
}

for( i = 0; i < 3; i++ )
{
    VarSet( "px" + i, ValueWhen( pk, bi, i ) );
    VarSet( "tx" + i, ValueWhen( tr, bi, i ) );
    VarSet( "ph" + i, ValueWhen( pk, H, i ) );
    VarSet( "tl" + i, ValueWhen( tr, L, i ) );
}

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;

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );
PlotShapes( shapeSmallCircle * tr, colorGreen, 0, L, -10 );
PlotShapes( shapeSmallCircle * pk, colorRed, 0, H, 10 );

ft = "Arial Black";
clr = colorDefault;
sz = 8;

if( labelsswitch )
{
    for( i = fvb; i <= lvb; i++ )
    {
        str1 = "";

        if( pk[i] )
        {
            if( dt[i] )
                str1 = "DT";
            else
                if( hh[i] )
                    str1 = "HH";
                else
                    if( lh[i] )
                        str1 = "LH";

            PlotTextSetFont( str1 + " (" + H[i] + ")", ft, sz, i, H[i], colorRed, clr, sz * 3 );
        }

        if( tr[i] )
        {
            str1 = "";

            if( db[i] )
                str1 = "DB";
            else
                if( ll[i] )
                    str1 = "LL";
                else
                    if( hl[i] )
                        str1 = "HL";

            PlotTextSetFont( str1 + " (" + L[i] + ")", ft, sz, i, L[i], colorGreen, clr, -sz * 4 );
        }
    }
}

// adding lines between pivots
GfxSetZOrder( 0 );
GfxSetCoordsMode( 1 );

idxhh1 = ValueWhen( hh, bi, 1 ); // index at HH
idxhh2 = ValueWhen( hh, bi, 2 ); // index at prior HH
valhh1 = ValueWhen( hh, H, 1 ); // value at HH
valhh2 = ValueWhen( hh, H, 2 ); // value at prior HH

if( trendlineswitch )
{
    for( i = lvb; i > fvb; i-- )
    {
        if( hh[i] AND valhh1[i] < valhh2[i] )
        {
            // coordinates at HH
            x0 = idxhh1[i]; // is same as i
            y0 = H[i];

            // coordinates at prior HH
            x1 = Nz( idxhh2[i] );
            y1 = H[x1];

            // extended line
            line = LineArray( x1, y1, x0, y0, 1 );

            // extension until crosses
            extension = IIf( Flip( x0 == bi, Cross( H, line ) ), line, Null );

            // un-extended line
            line = LineArray( x1, y1, x0, y0, 0 );

            // plot line & extension
            Plot( IIf( line, line, Null ), "", colorGreen , styleLine, Null, Null, 0, 0, 1 );
            Plot( IIf( extension, extension, Null ), "", colorGreen , styleDots | styleNoLine | styleNoRescale, Null, Null, 0, 0, 1 );
        }
    }
}

4 Likes

@empottasch, Thank You so much for spending your valuable time. I am very grateful to you. I tried using LineArray, But I was unsuccessful so asked in this forum.

You are Genius!
Thank You so much once again.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.