// Downloaded From https://www.WiseStockTrader.com
// Amibroker AFL code by Edward Pottasch, 2013 - 2016
SetBarsRequired( sbrAll, sbrAll );
SetTradeDelays( 0, 0, 0, 0 );
SetOption( "FuturesMode", True );
SetOption( "PriceBoundChecking", False );
SetOption( "AllowSameBarExit", True );
SetOption( "CommissionMode", 3 );
SetOption( "CommissionAmount", 2.01 );
NumContracts = 1;
//SetOption( "MaxOpenPositions", 6 );
PositionSize = NumContracts * MarginDeposit;

x = BarIndex();
Lx = LastValue( x );
fvb = FirstVisibleValue( x );
lvb = LastVisibleValue( x );
rightStrength = Param( "Fractal Pivot Right side Strength", 5, 0, 50, 1 );
leftStrength = Param( "Fractal Pivot Left side Strength", 10, 0, 50, 1 );

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;

for( i = 0; i < 3; i++ )
{
    VarSet( "px" + i, ValueWhen( pk, x, i ) );
    VarSet( "tx" + i, ValueWhen( tr, x, 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;

lowLine = ValueWhen( Ref( ll OR hl, -rightStrength ), Ref( L, -rightStrength ) );
highLine = ValueWhen( Ref( hh OR lh, -rightStrength ), Ref( H, -rightStrength ) );

Buy = Cross( C, highLine );
BuyPrice = C;
Short = Cross( lowLine, C );
ShortPrice = C;

Buy = Buy AND ValueWhen( Buy, highLine, 1 ) != ValueWhen( Buy, highline, 2 );
Short = Short AND ValueWhen( Short, lowLine, 1 ) != ValueWhen( Short, lowLine, 2 );

InLongTrade = ExRem( Buy, Short );
InShortTrade = ExRem( Short, Buy );
InLongTrade = Flip( InLongTrade, InShortTrade );
InShortTrade = Flip( InShortTrade, InLongTrade );

numBuy = Sum( Buy, BarsSince( InLongTrade AND Ref( InShortTrade, -1 ) ) );
numBuy = IIf( Buy, numBuy + 1, 0 );
numShort = Sum( Short, BarsSince( InShortTrade AND Ref( InLongTrade, -1 ) ) );
numShort = IIf( Short, numShort + 1, 0 );

Buy = IIf( numBuy == 1, 1, Buy );
Buy = IIf( numBuy > 1, sigScaleIn, Buy );
Short = IIf( numShort == 1, 1, Short );
Short = IIf( numShort > 1, sigScaleIn, Short );

Sell = IIf( numShort == 1, 1, 0 );
SellPrice = C;
Cover = IIf( numBuy == 1, 1, 0 );
CoverPrice = C;

GraphXSpace = 5;
SetChartBkColor( colorBlack );
SetChartOptions( 0, chartShowDates );
SetBarFillColor( IIf( C > O, colorGreen, IIf( C <= O, colorRed, colorLightGrey ) ) );
Plot( C, "Last", IIf( C > O, colorDarkGreen, IIf( C <= O, colorDarkRed, colorLightGrey ) ), 64, null, null, 0, 0, 1 );
PlotShapes( shapeSmallCircle*tr, IIf( Lx - ValueWhen( tr, x ) >= rightStrength, ColorRGB( 0, 100, 0 ), colorWhite ), 0, L, -10 );
PlotShapes( shapeSmallCircle*pk, IIf( Lx - ValueWhen( pk, x ) >= rightStrength, ColorRGB( 255, 0, 0 ), colorWhite ), 0, H, 10 );
Plot( lowLine, "", ColorRGB( 100, 0, 0 ), 1, null, null, 0, 0, 2 );
Plot( highLine, "", ColorRGB( 0, 100, 0 ), 1, null, null, 0, 0, 2 );

PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorDarkGreen, 0, L, -15 );
PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, -15 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );
PlotShapes( IIf( Short, shapeSmallDownTriangle, shapeNone ), colorRed, 0, H, IIf( Short AND Sell, -30, -15 ) );
PlotShapes( IIf( Short, shapeSmallCircle, shapeNone ), colorWhite, 0, ShortPrice, 0 );
PlotShapes( IIf( Cover, shapeSmallUpTriangle, shapeNone ), colorDarkGreen, 0, L, IIf( Cover AND Buy, -30, -15 ) );
PlotShapes( IIf( Cover, shapeSmallCircle, shapeNone ), colorWhite, 0, CoverPrice, 0 );

function drawPivotLabels()
{
    clr = ColorRGB( 50, 50, 50 );

    for( i = lvb; i > fvb; i-- )
    {
        {
            if( Buy[i] AND Cover[i] )
            {
                str = "";
                str = str + "+" + numBuy[i] + " | ";
                str = str + BuyPrice[i];
                PlotTextSetFont( str, "Arial Black", 8, i, L[i], colorGreen, clr, -50 );
            }

            if( Buy[i] AND !Cover[i] )
            {
                str = "";
                str = str + "+" + numBuy[i] + " | ";
                str = str + BuyPrice[i];
                PlotTextSetFont( str, "Arial Black", 8, i, L[i], colorGreen, clr, -38 );
            }

            if( Short[i] AND Sell[i] )
            {
                str = "";
                str = str + "-" + numShort[i] + " | ";
                str = str + ShortPrice[i];
                PlotTextSetFont( str, "Arial Black", 8, i, H[i], colorRed, clr, 40 );
            }

            if( Short[i] AND !Sell[i] )
            {
                str = "";
                str = str + "-" + numShort[i] + " | ";
                str = str + ShortPrice[i];
                PlotTextSetFont( str, "Arial Black", 8, i, H[i], colorRed, clr, 24 );
            }
        }
    }
}

drawPivotLabels();