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 www.marketcalls.in. 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:
- lenATR: The number of periods used to calculate ATR
- 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, www.quantforhire.com // // 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; else isUpTrend[i] = isUpTrend[i-1]; if (isUpTrend[i]) lowerBand[i] = Max(lowerBand[i], lowerBand[i-1]); else 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(O,"Open"); AddColumn(H,"High"); AddColumn(L,"Low"); AddColumn(C,"Close"); AddColumn((H+L)/2,"Avg"); AddColumn(ATR(lengthATR), "ATR("+lengthATR+")"); AddColumn(st, "Supertrend("+lengthATR+","+widthBands+")");