// Downloaded From https://www.WiseStockTrader.com
SetBarsRequired( 1000, 1000 );
SetFormulaName( "Weekly SMA" );
SetTradeDelays( 1.2, 1.2, 1.2, 1.2 );
SetOption( "initialequity", 1000 );
SetOption( "CommissionMode", 2 );
SetOption( "CommissionAmount", 30 );
SetOption( "MaxOpenPositions", 20 ); //
SetOption( "AllowPositionShrinking", False );
SetOption( "AllowSameBarExit", False );

ATRLevel = 3 * ATR( 20 );

Buy = C > MA( C, 30 );

PositionSize = -10;

Sell = 0;
trailARRAY = Null;
trailstop = 0;

for ( i = 1; i < BarCount; i++ )
{

    if ( trailstop == 0 AND Buy[ i ] )
    {
        trailstop = H[ i ] - ATRLevel[ i ];
    }
    else
        Buy[ i ] = 0; // remove excess buy signals

    if ( trailstop > 0 AND Low[ i ] < trailstop )
    {
        Sell[ i ] = 1;
        SellPrice[ i ] = trailstop;
        trailstop = 0;
    }

    if ( trailstop > 0 )
    {
        trailstop = Max( H[ i ] - ATRLevel[ i ], trailstop );
        trailARRAY[ i ] = trailstop;
    }

}

Sell = trailARRAY;

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