SUPER TREND indicator by Olivier Seban coded for Amibroker (AFL)

Amibroker has many indicators and oscillators, but this useful and famous indicator by Olivier Seban called SUPER TREND is missing.

Example of SuperTrend Indicator

I coded the SUPER TREND indicator in afl for Amibroker and share with you the result.

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

    for ( i = 1; 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
    }
    return ST;
}

If you want to have a Super Trend Indicator charted with two colors like the one in the image above, you can read “How to color an indicator“.

You can download the script here

Leave a Reply

Your email address will not be published. Required fields are marked *