// Downloaded From https://www.WiseStockTrader.com
displayType = ParamToggle( "Display Type", "Total|Single", 1 );

SetBarsRequired( sbrAll, sbrAll );
SetTradeDelays( 0, 0, 0, 0 );
SetOption( "FuturesMode", True );
SetOption( "PriceBoundChecking", False );
SetOption( "AllowSameBarExit", True );
SetOption( "CommissionMode", 3 );
SetOption( "CommissionAmount", 2.01 );

BuyPrice = SellPrice = CoverPrice = ShortPrice = C;
Short = Cover = 0;

MA200 = MA( C, 200 );
MA50 = MA( C, 50 );
MA20 = MA( C, 20 );
MA10 = MA( C, 10 );
MA5 = MA( C, 5 );
RSI2 = RSI( 2 );

index = IIf( C > MA20, 2, 0 ) + IIf( C > MA10, 1, 0 ) + IIf( C > MA5, 1, 0 ) + IIf( RSI2 > 0.8, 1, 0 ) + IIf( RSI2 > 0.9, 1, 0 ) ;

plus = IIf( index > Ref( index, -1 ), 1, 0 );
minus = IIf( index < Ref( index, -1 ), 1, 0 );

Buy = C > MA200 AND C > MA50;
Sell = C < MA50;

Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
InLongTrade = Flip( Buy, Sell );

Buy = IIf( plus AND !Buy AND !Sell AND InLongTrade, sigScaleIn, IIf( minus AND !Buy AND !Sell AND InLongTrade, sigScaleOut, Buy ) );

numberOfShares = IIf( Buy == 1, 5, IIf( Buy == sigScaleIn, 1, IIf( Buy == sigScaleOut, -1, 0 ) ) );
totalNumberOfShares = Sum( numberOfShares, BarsSince( Buy == 1 ) + 1 );
totalNumberOfShares = IIf( InLongTrade, totalNumberOfShares, 0 );

SetPositionSize( numberOfShares, spsShares * ( Buy == 1 ) );
SetPositionSize( abs( numberOfShares ), spsShares * ( Buy == sigScaleIn ) );
SetPositionSize( abs( numberOfShares ), spsShares * ( Buy == sigScaleOut ) );

SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "Close", colorBlack , styleCandle );
Plot( MA200, "ma200", colorBlue , styleLine );
Plot( MA50 , "ma50", colorRed , styleLine );
//Plot ( index , "index", colorAqua, styleOwnScale);

PlotShapes( IIf( Buy == 1, shapeUpArrow, shapeNone ), colorDarkGreen, 0, L, -15 );
PlotShapes( IIf( Buy == 1, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Buy == sigScaleIn, shapeUpArrow, shapeNone ), colorDarkGreen, 0, L, -15 );
PlotShapes( IIf( Buy == sigScaleIn, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell == 1, shapeDownArrow, shapeNone ), colorRed, 0, H, -15 );
PlotShapes( IIf( Sell == 1, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );
PlotShapes( IIf( Buy == sigScaleOut, shapeDownArrow, shapeNone ), colorRed, 0, H, -15 );
PlotShapes( IIf( Buy == sigScaleOut, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );

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

function drawPivotLabels()
{
    clr = ColorRGB( 50, 50, 50 );
    clr1 = ColorRGB( 150, 150, 150 );
    clr2 = ColorRGB( 200, 200, 50 );

    if( displayType )
        nn = numberOfShares;
    else
        nn = totalNumberOfShares;

    for( i = lvb; i > fvb; i-- )
    {
        {
            if( Buy[i] == 1 )
            {
                str = "";
                str = str + nn[i];
                PlotTextSetFont( str, "Arial Black", 8, i, L[i], colorGreen, clr1, -38 );
            }

            if( Buy[i] == sigScaleIn )
            {
                str = "";
                str = str + nn[i];
                PlotTextSetFont( str, "Arial Black", 8, i, L[i], colorGreen, clr, -38 );
            }

            if( Buy[i] == sigScaleOut )
            {
                str = "";
                str = str + nn[i];
                PlotTextSetFont( str, "Arial Black", 8, i, H[i], colorRed, clr, 24 );
            }

            if( Sell[i] == 1 )
            {
                str = "";
                str = str + totalNumberOfShares[i - 1];
                PlotTextSetFont( str, "Arial Black", 8, i, H[i], colorRed, clr2, 40 );
            }
        }
    }
}

drawPivotLabels();