The Supertrend Indicator Strategy (Amibroker Code)

The original article can be found here.

Amibroker code:

//First, make the the following function in a a new formula and save it:

function SuperTrend( Periods, Multiplier )
{
    AverageTrueRange = ATR( Periods );
    MedianPrice = ( H + L ) / 2;

    UpBand = MedianPrice + Multiplier * AverageTrueRange;
    DnBand = MedianPrice - Multiplier * AverageTrueRange;

    ST = Null;

    Direction[0] = 1;//just to initialize

    i = 1;

    while ( IsNull( Close[i] ) )
        i++;

    for ( ; i < BarCount; i++ )
    {
        // Begin Direction calculation
        if ( Close[i] > UpBand[i-1] )
            Direction[i] = 1;
        else
            if ( Close[i] < DnBand[i-1] )
                Direction[i] = -1;
            else
                Direction[i] = Direction[i-1];

        // End Direction calculation
        // Begin SuperTrend calculation
        if ( Direction[i] == 1 )
        {
            if ( DnBand[i-1] > DnBand[i] )
                DnBand[i] = DnBand[i-1];

            ST[i] = DnBand[i];
        }
        else
            if ( Direction[i] == -1 )
            {
                if ( UpBand[i-1] < UpBand[i] )
                    UpBand[i] = UpBand[i-1];

                ST[i] = UpBand[i];
            }

        // End SuperTrend calculation
    }
    
    //Plot(MedianPrice + Multiplier * AverageTrueRange, "UPBand", colorGrey40, styleDashed, Null, Null, 0, 99);
	//Plot(MedianPrice - Multiplier * AverageTrueRange, "UPBand", colorGrey40, styleDashed, Null, Null, 0, 99);
    return ST;
}
//Make a new formula sheet and past the following:


("//" is not part of the code but used to explain the code in plain English)

setOption("holdminbars",1);

SetOption("initialEquity",100000);  //start with 100 000 in equity

SetPositionSize(100,spsPercentOfEquity); //we invest 100% of equity - compounding

#include_once "Formulas\Custom\Supertrend function.afl";   //Remember where you saved it

PROCfast = 11;//Param( "PROCfast", 11, 1, 99, 1 );
PROCslow = 14;//Param( "PROCslow", 14, 2, 100, 1 );
WMAp = 10;//Optimize( "WMAp", 10, 1, 30, 1 );

//Exclude = PROCslow <= PROCfast;

//Coppock Curve = Weighted moving average (10) of (11-month ROC + 14-month ROC)

UpperBand = ( High + Low ) / 2 + 3 * ATR( 10 );

LowerBand = ( High + Low ) / 2 - 3 * ATR( 10 );


ST = SuperTrend( Optimize("p1", 10, 2, 50, 2), Optimize("p2", 3, 0.5, 5, 0.5));

BuyPrice = SellPrice = ShortPrice = CoverPrice = C;

Buy = Sell = Short = Cover = False;

Buy = Cover = Cross( C, ST );
Sell = Short = Cross( ST, C );

RoundLotSize = 1;

SetOption( "InitialEquity", 100000 );
SetOption( "ExtraColumnsLocation", 1 );
SetOption( "CommissionMode", 0 );
SetOption( "FuturesMode", False );
//SetOption( "CommissionAmount", 0 );
//SetPositionSize( 1, spsShares );
//SetPositionSize( 10000, spsValue );
SetPositionSize( 100, spsPercentOfEquity );
SetOption( "CommissionMode", 2 );  
SetOption( "CommissionAmount", 0.00 );  

Plot( ST, "SuperTrend", colorRed );
//Plot( 0, "ZeroLine", colorBlack );