// Downloaded From https://www.WiseStockTrader.com
/*
© AFL code by E.M.Pottasch, 11/2017

Example of extended spline

DLL programming using:
Cubic Spline http://kluge.in-chemnitz.de/opensource/spline/
*/

rightstrength = Param( "Right Strength", 5, 2, 50, 1 );
leftstrength = Param( "Left Strength", 5, 2, 50, 1 );
fact = Param( "Chart Time Frame Factor", 1, 1, 10, 1 );

bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

rightStrength = rightStrength * fact;
leftStrength = leftStrength * fact;

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

SetChartOptions( 0, chartShowDates );
SetChartBkColor( ColorRGB( 0, 0, 0 ) );
Plot( Close, "Price", colorDefault, styleCandle, Null, Null, 0, 0, 1 );
PlotShapes( shapeSmallCircle*tr, ColorRGB( 0, 250, 0 ), 0, L, -10 );
PlotShapes( shapeSmallCircle*pk, ColorRGB( 250, 0, 0 ), 0, H, 10 );

dn = DateTime();
sd = SelectedValue( dn );
start = dn == sd;
stidx = LastValue( ValueWhen( start, BarIndex() ) );

// Spline calculations
top = extendtop = xarr = yarr = Null;
cnt = 0;

for( i = fvb; i <= stidx; i++ )
{
    if( pk[i] )
    {
        xarr[cnt] = bi[i];
        yarr[cnt] = H[i];
        cnt = cnt + 1;
    }
}

for( i = fvb; i <= stidx; i++ )
{
    if( cnt > 0 )
        top[i] = empSpline( xarr, yarr, cnt, i );
}

for( i = stidx + 1; i <= lvb; i++ )
{
    if( cnt > 0 )
        extendtop[i] = empSpline( xarr, yarr, cnt, i );
}

Plot( top, "", colorRed, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( extendtop, "", ColorRGB( 0, 255, 255 ), styleLine | styleNoRescale, Null, Null, 0, 0, 2 );

bot = extendbot = xarr = yarr = Null;
cnt = 0;

for( i = fvb; i <= stidx; i++ )
{
    if( tr[i] )
    {
        xarr[cnt] = bi[i];
        yarr[cnt] = L[i];
        cnt = cnt + 1;
    }
}

for( i = fvb; i <= stidx; i++ )
{
    if( cnt > 0 )
        bot[i] = empSpline( xarr, yarr, cnt, i );
}

for( i = stidx + 1; i <= lvb; i++ )
{
    if( cnt > 0 )
        extendbot[i] = empSpline( xarr, yarr, cnt, i );
}

Plot( bot, "", colorGreen, styleLine | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( extendbot, "", ColorRGB( 255, 0, 255 ), styleLine | styleNoRescale, Null, Null, 0, 0, 2 );
Plot( bi == stidx, "", colorDarkBlue, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 10 );