Super Trend Indicator in EasyLanguage

SuperTrend Indicator – What Is It?

SuperTrend is a trading strategy and indicator all built into one entity.  There are a couple of versions floating around out there.  MultiCharts and Sierra Chart both have slightly different flavors of this combo approach.

Ratcheting Trailing Stop Paradigm

This indic/strat falls into this category of algorithm.  The indicator never moves away from your current position like a parabolic stop or chandelier exit.  I used the code that was disclosed on Futures.io or formerly known as BigMikesTrading blog.   This version differs from the original SuperTrend which used average true range.  I like Big Mike’s version so it will discussed here.

Big Mike’s Math

The math for this indicator utilizes volatility in the terms of the distance the market has travelled over the past N days.  This is determined by calculating the highest high of the last N days/bars and then subtracting the lowest low of last N days/bars.   Let’s call this the highLowRange.  The next calculation is an exponential moving average of the highLowRange.  This value will define the market volatility.   Exponential moving averages of the last strength days/bars highs and lows are then calculated and divided by two – giving a midpoint.  The volatility measure (multiplied my mult) is then added to this midpoint to calculate an upper band.  A lower band is formed by subtracting the volatility measure X mult from the midpoint.

Upper or Lower Channel?

If the closing price penetrates the upper channel and the close is also above the highest high of strength days/bars back (offset by one of course) then the trend will flip to UP.  When the trend is UP,  then the Lower Channel is plotted.  Once the trend flips to DN, the upper channel will be plotted.  If the trend is UP the lower channel will either rise with the market or stay put.  The same goes for a DN trend – hence the ratcheting.  Here is a graphic of the indicator on CL.

Super Trend by Bike Mike

If you plan on using an customized indicator in a strategy it is always best to build the calculations inside a function.  The function then can be used in either an indicator or a strategy.

Function Name: SuperTrend_BM

Function Type: Series – we will need to access prior variable values

SuperTrend_BM Function Code:

//SuperTrend from Big Mike now futures.io

inputs:
length(NumericSimple), mult(NumericSimple), strength(NumericSimple), STrend(NumericRef);

vars:
highLowRange(0),
xAvgRng(0),
xAvg(0),
dn(0),
up(0),
trend(1),
trendDN(False),
trendUP(False),
ST(0);

highLowRange = Highest(high, length) - Lowest(low, length);

xAvgRng = XAverage(highLowRange, length);

xAvg = (XAverage(high, Strength) + XAverage(low, Strength))/2;

up = xAvg + mult * xAvgRng;
dn = xAvg - mult * xAvgRng;

if c > up[1] and c > Highest(High, strength)[1] then
trend = 1
else
if c < dn[1] and c < Lowest(Low, Strength)[1] then
trend = -1;

//did trend flip?
trendDN = False;
trendUP = False;

if trend < 0 and trend[1] > 0 then
trendDN = True;
if trend > 0 and trend[1] < 0 then
trendUP = True;

//ratcheting mechanism
if trend > 0 then dn = maxList(dn,dn[1]);
if trend < 0 then up = minList(up,up[1]);

// if trend dir. changes then assign
// up and down appropriately
if trendUP then
up = xAvg + mult * xAvgRng;
if trendDN then
dn = xAvg - mult * xAvgRng;

if trend = 1 then
ST = dn
else
ST = up;

STrend = trend;

SuperTrend_BM = ST;
SuperTrend ala Big Mike

The Inputs to the Function

The original SuperTrend did include the Strength input.  This input is a Donchian like signal.  Not only does the price need to close above/below the upper/lower channel but also the close must be above/below the appropriate Donchian Channels to flip the trend,  Also notice we are using a numericRef as the type for STrend.  This is done because we need the function to return two values:  trend direction and the upper or lower channel value.  The appropriate channel value is assigned to the function name and STrend contains the Trend Direction.

A Function Driver in the Form of an Indicator

A function is a sub-program and must be called to be utilized.   Here is the indicator code that will plot the values of the function using: length(9), mult(1), strength(9).

// SuperTrend indicator
// March 25 2010
// Big Mike https://www.bigmiketrading.com
inputs:
length(9), mult(1), strength(9);

vars:
strend(0),
st(0);

st = SuperTrend_BM(length, mult,strength,strend);

if strend = 1 then Plot1(st,"SuperTrendUP");
if strend = -1 then Plot2(st,"SuperTrendDN");
Function Drive in the form of an Indicator

 

This should be a fun indicator to play with in the development of a trend following approach.   My version of Big Mike’s code is a little different as I wanted the variable names to be a little more descriptive.

Update Feb 28 2022

I forgot to mention that you will need to make sure your plot lines don’t automatically connect.

Plot Style Setting

Can You Do This with Just One Plot1?

An astute reader brought it to my attention that we could get away with a single plot and he was right.  The reason I initially used two plot was to enable the user to chose his/her own plot colors by using the Format dialog.

//if strend = 1 then Plot1(st,"SuperTrendUP");
//if strend = -1 then Plot2(st,"SuperTrendDN");

if strend = 1 then SetPlotColor(1,red);
if strend = -1 then SetPlotColor(1,green);

Plot1(st,"SuperTrend_BM");
Method to just use one Plot1