SuperTrend Indicator
September 27, 2018

I recently completed a client project that utilizes the SuperTrend indicator. The indicator is basically a variation on other types of volatility bands, using a multiple of ATR to define bands above and below the current average price. The SuperTrend line follows the lower band when the price is in an up trend (has most recently broken the upper band), and follows the upper band when the price is in a down trend (has most recently broken the lower band).

I have not used the indicator enough to have a strong opinion on its usefulness, although it seems to be particularly popular with traders from India. However, I had a hard time finding written definitions of the indicator, and the one that I did find was incorrect. I finally resorted to creating a definition by reverse engineering some code written by Rajandran R at I then wrote my own version in AmiBroker AFL in a way that makes it easy to use on a chart or in an exploration. In addition, the SuperTrend function could be easily copied to another AFL used for back testing or other AmiBroker analysis.

Here is the definition of the SuperTrend indicator that I derived from Rajandran’s code:

SuperTrend requires two parameters:

  1. lenATR: The number of periods used to calculate ATR
  2. width: The factor by which to multiply the ATR value when determining upper and lower bands

We begin by calculating preliminary values for the upper and lower bands as follows:

UpperBand = (High + Low) / 2 + (width * ATR(lenATR))
LowerBand = (High + Low) / 2 - (width * ATR(lenATR))

Next we examine the bands and the Close of each bar in the series to determine the trend:

If Current Close > Previous UpperBand Then Trend is UP
Else If Current Close < Previous LowerBand Then Trend is DOWN
Else Trend is unchanged from previous bar

The direction of the trend allows us to modify the preliminary band values, and also determine whether the SuperTrend line is currently following the upper or lower band:

If UP trend Then 
    Current LowerBand = max(Current LowerBand , Previous LowerBand )
    Current Supertrend = Current LowerBand
If DOWN trend Then 
    Current UpperBand = min(Current UpperBand , Previous UpperBand )
    Current Supertrend = Current UpperBand

Here is the AmiBroker AFL for the SuperTrend indicator:

// Supertrend Indicator
// AmiBroker implementation by Matt Radtke,
// History
// v1: Initial Implementation

function SuperTrend(lenATR, width)
    nATR = ATR(lenATR);
    pAvg = (H+L) / 2;

    upperBand = pAvg + width * nATR;
    lowerBand = pAvg - width * nATR;
    isUpTrend = True;
    dn = DateNum();

    for (i=lenATR; i<BarCount; ++i)
        if (C[i] > upperBand[i-1])
            isUpTrend[i] = True;
        else if (C[i] < lowerBand[i-1])
            isUpTrend[i] = False;
            isUpTrend[i] = isUpTrend[i-1];

        if (isUpTrend[i])
            lowerBand[i] = Max(lowerBand[i], lowerBand[i-1]);
            upperBand[i] = Min(upperBand[i], upperBand[i-1]);

    super = IIf(isUpTrend, lowerBand, upperBand); 
    return super;

lengthATR = Param("ATR Length", 10, 1, 100, 1);
widthBands = Param("Band Width", 3, 1, 20, 0.1);
st = Supertrend(lengthATR,widthBands);

Plot(st, "Supertrend("+lengthATR+","+widthBands+")", ParamColor( "Color", colorCycle ), ParamStyle("Style") );

Filter = True;
AddColumn(ATR(lengthATR), "ATR("+lengthATR+")");
AddColumn(st, "Supertrend("+lengthATR+","+widthBands+")");

Copyright 2024 Quant Alpha – ‘Quant Alpha’ and ‘Quantive Alpha’ are Trade Marks of Quantive Alpha – all rights reserved.

All the information contained on this website is general in nature and does not constitute personal or investment advice. Quant Alpha produces algorithms and software only and does not trade or arrange any trading on your behalf. Quant Alpha will not accept liability for any loss or damage, including without limitation, any loss which may arise directly or indirectly from the use of, or reliance on: its algorithms; the information on this site; or information provided by its managers, partners or affiliates. You should seek independent financial advice and conduct your due diligence prior to acquiring any Quant Alpha technology. Quant Alpha is neither a registered investment advisor nor an investment advisory service and does not provide any recommendations to buy or sell particular financial products. 
Before engaging in any trading activities, you should understand the nature and extent of your rights and obligations and be aware of the risks involved. Don’t trade with money you can’t afford to lose. Your trading and investing decisions are entirely your own responsibility. All securities and financial product transactions involve risks. Where Quant Alpha provides hypothetical representations of what the technology has achieved in the past, this has been done with the greatest know-how, data and expert technology that is available, but still, Quant Alpha cannot guarantee that these results have any likelihood whatsoever of being achieved in future. Where records have been provided of how the software has performed on management’s own accounts, whilst these are an accurate and true record of what has taken place in the past, they are not necessarily indicative of future results – the future is as unknown to Quant Alpha management as it is to anyone else. The past performance of any trading system or methodology is not necessarily indicative of future results.