Category Archives: Trend Following

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

Another Good Year For Trend Following

Take a Look at the Last Two Years

Simple Donchian on a one contract basis.  $100 Commission/slippage.  Tested from 2000 thru December 31, 2021.  Do you see why most trend followers failed after the 2008 monstrous year.   Many funds caught the 2008 move and more funds were added soon thereafter.  Promises of similar performance came to fruition in 2011.  This kept much of the “new money” on the board.  However, reality set in and weak handed funds left for greener pastures.  Those that stuck it out were rewarded in 2014.  The trend drought of 2014 -2019 eroded most of the confidence in managed futures.  The rationalization that limited resources would eventually rise in price sounded good initially, but then fell on deaf ears after months of draw down.  Well known CTAs and hedge funds shut their doors forever.   The long awaited promise of 2008 came in the form of a pandemic – but it was too late.   Maybe now the deluge that ended the drought will persevere (hopefully not in the form of a pandemic) into the future.  Prices do not need to rise endlessly, but they need to move one direction or another without many hiccups.   

Simple Donchian Caught Most of the Commodities Up Moves

Which Sectors Pushed this Curve through the Roof

These reports were generated by my Python based Trading Simula-18 using Pinnacle continuous data – rollover triggered by date.  This is my new sector analysis report where I graph the last four years performance.  The tabular data is for the entire 21 year history.  The best sectors were energy, grains, financials and metals.  Lumber was extraordinary

Sector Analysis Report
################################################
Currency   -------------------------------------
BN           -28012      44681 
SN           -26925      55337 
AN             6560      34350 
DX            16284      24387 
FN            67463      31737 
JN           -22212      50362 
CN           -25355      44110 
------------------------------------------------
Totals:      -12198     141445 
------------------------------------------------
Currency   Last 4 Years    ---------------------
        |                                       
      ||| |                                     
     ||||||                                     
 |   ||||||||                                   
 |  |||||||||                                   
 | ||||||||||                                   
 |||||||||||||                                  
 ||||||||||||||                                 
 ||||||||||||||  |                              
|||||||||||||||||||                             
|||||||||||||||||||||           |               
||||||||||||||||||||||          |      |        
||||||||||||||||||||||          |      |        
||||||||||||||||||||||  |      ||   |  |  |     
------------------------------------------------ 0
|||||||||||||||||||||||||      |||  |||| ||| |  
|||||||||||||||||||||||||      |||| ||||||||||| 
||||||||||||||||||||||||| |    |||| ||||||||||||
|||||||||||||||||||||||||||    |||||||||||||||||
||||||||||||||||||||||||||||  ||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||
2018--------2019--------2020--------2021--------
################################################
Energies   -------------------------------------
ZU           180750      38330 
ZH           155696      85541 
ZN            70630      74400 
ZB           131874      66651 
------------------------------------------------
Totals:      538951     154434 
------------------------------------------------
Energies   Last 4 Years    ---------------------
                                      |       | 
                                      ||  ||  ||
                                      ||| || |||
                            |         ||||||||||
                            |        |||||||||||
                           |||       |||||||||||
                           |||| |   ||||||||||||
                           |||||||  ||||||||||||
                           |||||||||||||||||||||
                           |||||||||||||||||||||
                           |||||||||||||||||||||
                           |||||||||||||||||||||
            |   ||         |||||||||||||||||||||
           ||   ||         |||||||||||||||||||||
           ||  ||||        |||||||||||||||||||||
           |||||||||      ||||||||||||||||||||||
 |         ||||||||||    |||||||||||||||||||||||
||  ||     |||||||||||||||||||||||||||||||||||||
||||||     |||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||
2018--------2019--------2020--------2021--------
################################################
Metals     -------------------------------------
ZG           -17070      43540 
ZI            68395     146885 
ZK           101888      29475 
ZP            82885      27600 
ZA           174955      83910 
------------------------------------------------
Totals:      411052     166703 
------------------------------------------------
Metals     Last 4 Years    ---------------------
                                             |  
                                |       |    |  
                               ||       ||   |  
                               ||       |||| |||
                          |    ||| |   |||||||||
                          ||   |||||||||||||||||
                          || |||||||||||||||||||
                         |||||||||||||||||||||||
                         |||||||||||||||||||||||
                         |||||||||||||||||||||||
                    | | ||||||||||||||||||||||||
                    ||||||||||||||||||||||||||||
              |   ||||||||||||||||||||||||||||||
              || |||||||||||||||||||||||||||||||
             |||||||||||||||||||||||||||||||||||
            ||||||||||||||||||||||||||||||||||||
         | |||||||||||||||||||||||||||||||||||||
        ||||||||||||||||||||||||||||||||||||||||
||     |||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||
2018--------2019--------2020--------2021--------
################################################
Grains     -------------------------------------
ZS            79175      20312 
ZW           -43438      51975 
ZC             5238      26688 
ZL            13248      24588 
ZM            29860      28810 
------------------------------------------------
Totals:       84083      88850 
------------------------------------------------
Grains     Last 4 Years    ---------------------
                                        |       
                                        |       
                                        ||      
                                        ||      
                                      ||||||||| 
                                     |||||||||||
                                     |||||||||||
                                     |||||||||||
                                    ||||||||||||
  |                                 ||||||||||||
  |   |                             ||||||||||||
| ||  |||                           ||||||||||||
||||| |||||                        |||||||||||||
||||||||||||                       |||||||||||||
||||||||||||    ||| |              |||||||||||||
||||||||||||| ||||||| ||          ||||||||||||||
||||||||||||||||||||||||||||      ||||||||||||||
||||||||||||||||||||||||||||||   |||||||||||||||
|||||||||||||||||||||||||||||||  |||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||
2018--------2019--------2020--------2021--------
################################################
Financials -------------------------------------
US            35991      24959 
TY             -350      29175 
TU             1473      23969 
EC             4700       9650 
------------------------------------------------
Totals:       41813      56453 
------------------------------------------------
Financials Last 4 Years    ---------------------
                            |          |   |||| 
                           |||         |   |||||
                           ||| |       || ||||||
                           |||||      ||||||||||
                           ||||||     ||||||||||
                           ||||||||   ||||||||||
                           ||||||||   ||||||||||
                          |||||||||  |||||||||||
                          ||||||||||||||||||||||
                    |     ||||||||||||||||||||||
                    |     ||||||||||||||||||||||
                  | |     ||||||||||||||||||||||
  |               | ||    ||||||||||||||||||||||
  ||             ||||||  |||||||||||||||||||||||
 |||             |||||||||||||||||||||||||||||||
 ||||       || | |||||||||||||||||||||||||||||||
 |||||      |||| |||||||||||||||||||||||||||||||
|||||||     ||||||||||||||||||||||||||||||||||||
||||||||    ||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||
2018--------2019--------2020--------2021--------
################################################
Softs      -------------------------------------
SB            25927      15035 
KC           -49775      94069 
CC           -72140      76660 
CT            16785      45470 
Lumber       218513      51745 
JO             2588      15760 
------------------------------------------------
Totals:      141898     128540 
------------------------------------------------
Softs      Last 4 Years    ---------------------
                                            | ||
                                            ||||
                                           |||||
                                           |||||
                                          ||||||
                                        | ||||||
                                        ||||||||
                                        ||||||||
                                        ||||||||
                                        ||||||||
------------------------------------------------ 0
                                |   |  |||||||||
                                |   | ||||||||||
                                ||||||||||||||||
                                ||||||||||||||||
                               |||||||||||||||||
                               |||||||||||||||||
         ||                |  ||||||||||||||||||
     |  ||||||||||| |      |||||||||||||||||||||
    ||||||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||
2018--------2019--------2020--------2021--------
################################################
Meats      -------------------------------------
ZT           -29940      57680 
ZZ            38480      15080 
ZF            18413      57550 
------------------------------------------------
Totals:       26952      66515 
------------------------------------------------
Meats      Last 4 Years    ---------------------
                            |           ||      
                            |    ||     ||      
                           ||  | ||     ||      
                           ||| | ||     ||      
                           |||||||||   ||||     
   |                      ||||||||||   ||||     
|  |                      ||||||||||| |||||     
|  |                      ||||||||||| |||||| |  
|  ||                     |||||||||||||||||| |  
|  |||            |      |||||||||||||||||||||  
|| |||            |      |||||||||||||||||||||  
||||||           ||    | ||||||||||||||||||||| |
||||||           ||   |||||||||||||||||||||||| |
||||||           ||   ||||||||||||||||||||||||||
|||||||| |       ||   ||||||||||||||||||||||||||
|||||||| |       |||  ||||||||||||||||||||||||||
||||||||||    |  |||||||||||||||||||||||||||||||
|||||||||||   |  |||||||||||||||||||||||||||||||
|||||||||||| || ||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||
2018--------2019--------2020--------2021--------

How Do You Program this in Python

Here is the module for TS-18.  There is a little extra code to keep track of sectors in case you want to limit sector exposure.  However, this code takes every trade on a one contract basis.  This code reflects my latest version of TS-18, which will be released shortly.

#  Define Long, Short, ExitLong and ExitShort Levels - mind your indentations
            buyLevel = highest(myHigh,40,curBar,1)
            shortLevel = lowest(myLow,40,curBar,1)
            longExit = lowest(myLow,20,curBar,1)
            shortExit = highest(myHigh,20,curBar,1)
            ATR = sAverage(myTrueRange,30,curBar,1)
            stopAmt = 2000/myBPV

            ATR = sAverage(myTrueRange,30,curBar,1)

            posSize = 1
            mmLxit = 99999999
            mmSxit = -99999999
            if mp == 1 : mmLxit = entryPrice[-1] - stopAmt
            if mp ==-1 : mmSxit = entryPrice[-1] + stopAmt



#  Long Exit
            if mp == 1 and myLow[curBar] <= mmLxit and mmLxit > longExit and barsSinceEntry > 1:
                price = min(myOpen[curBar],mmLxit)
                tradeName = "LxitMM"
                numShares = curShares
                exitPosition(price, curShares, tradeName, sysMarkDict)
                unPackDict(sysMarkDict)
#  Long Exit
            if mp == 1 and myLow[curBar] <= longExit and barsSinceEntry > 1:
                price = min(myOpen[curBar],longExit)
                tradeName = "Lxit"
                numShares = curShares
                exitPosition(price, curShares, tradeName, sysMarkDict)
                unPackDict(sysMarkDict)
#  Short Exit
            if mp == -1 and myHigh[curBar] >= shortExit and barsSinceEntry > 1:
                price = max(myOpen[curBar],shortExit)
                tradeName = "Sxit"
                numShares = curShares
                exitPosition(price, curShares, tradeName, sysMarkDict)
                unPackDict(sysMarkDict)
#  Short Exit
            if mp == -1 and myHigh[curBar] >= entryPrice[-1] + stopAmt and barsSinceEntry > 1:
                price = max(myOpen[curBar],entryPrice[-1] + stopAmt)
                tradeName = "SxitMM"
                numShares = curShares
                exitPosition(price, curShares, tradeName,sysMarkDict)
                unPackDict(sysMarkDict)
#  Long Entry
            if myHigh[curBar] >= buyLevel and mp !=1:
                price = max(myOpen[curBar],buyLevel)
                tradeName = "Simple Buy"
                numShares = posSize
                enterLongPosition(price,numShares,tradeName,sysMarkDict)
                unPackDict(sysMarkDict)
#  Short Entry
            if myLow[curBar] <= shortLevel and mp !=-1 :
                price = min(myOpen[curBar],shortLevel)
                if mp == 0 : sectorTradesTodayList[curSector] +=1
                tradeName = "Simple Sell"
                numShares = posSize
                enterShortPosition(price, numShares, tradeName, sysMarkDict)
                unPackDict(sysMarkDict)
Python within Trading Simula-18