Best trendline afl

EXPERTS / SENIORS Could you please make the below afl workable. Below is the code and error msg screenshot and source from where I got it:

_SECTION_BEGIN("_automatictrendlines");

GraphXSpace=10;

indicatorMode = Status( "action" ) == actionIndicator;
Buy = Short = Sell = Cover = 0;
BuyZone = 0;
ShortZone = 0;


tangent = 0;
firstVisible = Status( "FirstVisibleBarIndex" );
LastVisible = Status( "LastVisibleBarIndex" );
isVisible = 0;
breakOut = 0;
graphColor = colorOrange;

ParamStr("TRIX Default Period", "2");

indyOpt =ParamList("Indicator", "RSI|CCI|ROC|TRIX|BLI");
Period = Param("pds ",7,1,28,1);

//bollingerline Indicator
width = Optimize("width ",Param("width ",2,0.1,10,0.1),2,2,0.1);
C1 = C - BBandBot( Close, period, width) ;
C2 = BBandTop (Close,period,width) - BBandBot( Close, period, width);
RS = (AMA( C1, 0.5 ) / AMA( C2, 0.5 ))*100; //smooth
uptrendS = rs > Ref(rs,-1);
downtrendS = rs < Ref(rs,-1);
//einde

r= Optimize("Reverse ",Param("Reverse ",1,1,28,1),1,28,1);
reverse = r / 100;

pds = Optimize("pds ",Period,1,28,1);

switch (indyOpt)
{
case "RSI":
SetChartOptions(1,0,chartGrid20 |chartGrid50 |chartGrid80);
myArray = RSI( pds );
break;

case "CCI":
SetChartOptions(1,0,chartGrid100 );
myArray = CCI( pds );
break;

case "ROC":
SetChartOptions(1,0,chartGrid0);
myArray = ROC( Close, pds );
break;

case "TRIX":
SetChartOptions(1,0, chartGrid0);
pds = Optimize("pds ",Param("pds ",2,1,28,1),1,28,1);
myArray = Trix( pds );
break;

case "BLI":
SetChartOptions(1,0,chartGrid20|chartGrid30 |chartGrid50 |chartGrid70|chartGrid80);
myArray = RS ;
break;
}

j = 0;
direction[0] = 0;
dd = 0 ;
y[0] = myArray[pds];
x[0] = 0;

for ( i = pds + 1; i < BarCount; i++ )
{
if ( direction[j] == 0 )
{
if ( myArray <= y[j] )
{
y[j] = myArray;
x[j] = i;
}
else
if ( myArray >= y[j]*( 1 + reverse ) )
{
j++;
y[j] = myArray;
direction[j] = 1;
x[j] = i;
}
}
else
{
if ( myArray >= y[j] )
{
y[j] = myArray;
x[j] = i;
}
else
if ( myArray <= y[j]* ( 1 - reverse ) )
{
j++;
y[j] = myArray;
direction[j] = 0;
x[j] = i;

}
}

dd = direction[j];

if ( x[j] >= firstVisible && x[j] <= LastVisible )
isVisible[j] = 1;

if ( j > 2 )
{
dy = ( y[j] - y[j-2] ) ;
dx = x[j] - x[j-2];
tangent[j] = dy / dx;

if ( direction[j] )
{
breakOut[j] = tangent[j] > tangent[j-2] ;

if ( breakOut [j] )
{
BuyZone = 1 ;
graphColor = colorGreen;
}
}
else
{
breakOut[j] = tangent[j] < tangent[j-2];
if ( breakOut[j] )
{
ShortZone = 1;
graphColor = colorDarkRed;
}

}

}

}


Buy = Cover = ExRem( BuyZone, !dd );
Short = Sell = ExRem( ShortZone, dd ) ;

if ( IndicatorMode )
{
Shortname = " USE_"+indyOpt+"("+Pds+")";
Plot ( myArray, Shortname , Graphcolor ,styleThick);
}
PlotShapes( Buy * shapeUpArrow , colorGreen, 0, myArray );
PlotShapes( Short * shapeDownArrow , colorRed, 0, myArray);

//-------display trendlines------------//

tlcolor = colorGreen;
myTrendLine = Null;

for ( n = 1; n <= j; n++ )
{
if ( isVisible[n] && n > 3 )
{
if ( direction[n] )
tlcolor = colorGreen;
else
tlcolor = colorRed;

if ( breakOut[n] )
myTrendline = LineArray( x[n-4], y[n-4], x[n-2], y[n-2], 1 );

myTrendline = IIf( BarIndex() > x[n], Null, myTrendline );
Plot( myTrendline, "", tlcolor, styleLine + styleNoRescale | styleNoLabel | styleDashed );
}
}

_SECTION_END();

image

Reverse is now a AFL function: Reverse

Probably it was not when that formula was originally written. To fix just change it references (for instance, just call it “reversed”).

Here is the “fixed” code (properly formatted with the “Prettify Selection” option of the AFL editor.

// Original code posted by Birdseye and revised after suggestions by KelvinHand 
// http://www.traderji.com/community/threads/_automatictrendlinescci.87695/

_SECTION_BEGIN( "_automatictrendlines" );

GraphXSpace = 10;

indicatorMode = Status( "action" ) == actionIndicator;
Buy = Short = Sell = Cover = 0;
BuyZone = 0;
ShortZone = 0;


tangent = 0;
firstVisible = Status( "FirstVisibleBarIndex" );
LastVisible = Status( "LastVisibleBarIndex" );
isVisible = 0;
breakOut = 0;
graphColor = colorOrange;

ParamStr( "TRIX Default Period", "2" );

indyOpt = ParamList( "Indicator", "RSI|CCI|ROC|TRIX|BLI" );
Period = Param( "pds ", 7, 1, 28, 1 );

//bollingerline Indicator
width = Optimize( "width ", Param( "width ", 2, 0.1, 10, 0.1 ), 2, 2, 0.1 );
C1 = C - BBandBot( Close, period, width ) ;
C2 = BBandTop( Close, period, width ) - BBandBot( Close, period, width );
RS = ( AMA( C1, 0.5 ) / AMA( C2, 0.5 ) ) * 100; //smooth
uptrendS = rs > Ref( rs, -1 );
downtrendS = rs < Ref( rs, -1 );
//einde

r = Optimize( "Reverse", Param( "Reverse ", 1, 1, 28, 1 ), 1, 28, 1 );
reversed = r / 100;

pds = Optimize( "pds ", Period, 1, 28, 1 );

switch( indyOpt )
{
case "RSI":
    SetChartOptions( 1, 0, chartGrid20 | chartGrid50 | chartGrid80 );
    myArray = RSI( pds );
    break;

case "CCI":
    SetChartOptions( 1, 0, chartGrid100 );
    myArray = CCI( pds );
    break;

case "ROC":
    SetChartOptions( 1, 0, chartGrid0 );
    myArray = ROC( Close, pds );
    break;

case "TRIX":
    SetChartOptions( 1, 0, chartGrid0 );
    pds = Optimize( "pds ", Param( "pds ", 2, 1, 28, 1 ), 1, 28, 1 );
    myArray = Trix( pds );
    break;

case "BLI":
    SetChartOptions( 1, 0, chartGrid20 | chartGrid30 | chartGrid50 | chartGrid70 | chartGrid80 );
    myArray = RS ;
    break;
}

j = 0;
direction[0] = 0;
dd = 0 ;
y[0] = myArray[pds];
x[0] = 0;

for( i = pds + 1; i < BarCount; i++ )
{
    if( direction[j] == 0 )
    {
        if( myArray[i] <= y[j] )
        {
            y[j] = myArray[i];
            x[j] = i;
        }
        else
            if( myArray[i] >= y[j] * ( 1 + reversed ) )
            {
                j++;
                y[j] = myArray[i];
                direction[j] = 1;
                x[j] = i;
            }
    }
    else
    {
        if( myArray[i] >= y[j] )
        {
            y[j] = myArray[i];
            x[j] = i;
        }
        else
            if( myArray[i] <= y[j] * ( 1 - reversed ) )
            {
                j++;
                y[j] = myArray[i];
                direction[j] = 0;
                x[j] = i;

            }
    }

    dd[i] = direction[j];

    if( x[j] >= firstVisible && x[j] <= LastVisible )
        isVisible[j] = 1;

    if( j > 2 )
    {
        dy = ( y[j] - y[j - 2] ) ;
        dx = x[j] - x[j - 2];
        tangent[j] = dy / dx;

        if( direction[j] )
        {
            breakOut[j] = tangent[j] > tangent[j - 2] ;

            if( breakOut [j] )
            {
                BuyZone[i] = 1 ;
                graphColor[i] = colorGreen;
            }
        }
        else
        {
            breakOut[j] = tangent[j] < tangent[j - 2];

            if( breakOut[j] )
            {
                ShortZone[i] = 1;
                graphColor[i] = colorDarkRed;
            }

        }

    }

}


Buy = Cover = ExRem( BuyZone, !dd );
Short = Sell = ExRem( ShortZone, dd ) ;

if( IndicatorMode )
{
    Shortname = " USE_" + indyOpt + "(" + Pds + ")";
    Plot( myArray, Shortname , Graphcolor , styleThick );


    PlotShapes( Buy * shapeUpArrow , colorGreen, 0, myArray );
    PlotShapes( Short * shapeDownArrow , colorRed, 0, myArray );

//-------display trendlines------------//

    tlcolor = colorGreen;
    myTrendLine = Null;

    for( n = 1; n <= j; n++ )
    {
        if( isVisible[n] && n > 3 )
        {
            if( direction[n] )
                tlcolor = colorGreen;
            else
                tlcolor = colorRed;

            if( breakOut[n] )
                myTrendline = LineArray( x[n - 4], y[n - 4], x[n - 2], y[n - 2], 1 );

            myTrendline = IIf( BarIndex() > x[n], Null, myTrendline );
            Plot( myTrendline, "", tlcolor, styleLine + styleNoRescale | styleNoLabel | styleDashed );
        }
    }

}

_SECTION_END();

There was another easy to spot mistake in multiple locations in the (looping bar by bar) code where “myArray” was treated as a numeric value when actually it is an array. The code in the original thread by KelvinHand has it right plus if fixes a couple of other more subtle issues!

In the end, if this is correct or not, and if the formula works as the original author conceived it, I leave it to you as an exercise in order to learn how to work with the AFL.

3 Likes

Frankly speaking , I did not expect that somebody will answer and help.

When I saw your reply , I can not express how happy I was and how Thankful I am to you…

Thanks a lot .

Could plot the AFL correctly now. But My little brain will take its time to test and understand – if it can be useful at least reasonably to apply for intraday scalping kind of trade.

@santy, unfortunately, I cannot help you since I do not actually trade intraday. My interest is mainly on market anomalies, momentum or mean reversion strategies on daily/weekly intervals.

Couls you please help me out in the below thread:
Manual backtesting codes