SuperTrend adaptive periods (r-squared)

Category: Indicators By: Nicolas Created: February 10, 2017, 2:20 PM
February 10, 2017, 2:20 PM
Indicators
16 Comments

New SuperTrend indicator version made of adaptive periods with comparison made from the coefficient of determination / average of the least squares.

The adaptive formula makes the SuperTrend more reactive to the little fluctuation of the market while still doing what it is supposed to do: be a trend following indicator.

Another great idea of this indicator is to draw “floating levels” above or below the SuperTrend which are calculated with percentage of the highest and lowest values of the SuperTrend within the last Y “flLookBack” periods. These floating levels could act as filter to detect / prevent false trend reverse.

The adaptive function code of this indicator can be use in many other indicators and new original concept, I suppose I’ll post more of them into the library in the future days/weeks.
Another indicator made of this kind of adaptive periods if the r-squared adaptive exponential moving average already posted in the prorealtime code library here.

//PRC_adaptive SuperTrend (r-square method) | indicator
//10.02.2017
//Nicolas @ www.prorealcode.com
//Sharing ProRealTime knowledge

// --- parameters
// Period = 20
// mult = 2
// ---

Data = customclose
//Period = 20

SumX  = 0
SumXX = 0
SumXY = 0
SumYY = 0
SumY  = 0

if barindex>Period then

// adaptive r-squared periods
for k=0 to period-1 do
 tprice = Data[k]
 SumX  = SumX+(k+1)
 SumXX = SumXX+((k+1)*(k+1))
 SumXY = SumXY+((k+1)*tprice)
 SumYY = SumYY+(tprice*tprice)
 SumY  = SumY+tprice
next
   
Q1  = SumXY - SumX*SumY/period
Q2  = SumXX - SumX*SumX/period
Q3  = SumYY - SumY*SumY/period

iRsq=((Q1*Q1)/(Q2*Q3))

avg = supertrend[mult,round(Period+Period*(iRsq-0.25))]

//floating levels
flLookBack      = 25 // Floating levels lookback period
flLevelUp       = 90 // Floating levels up level %
flLevelDown     = 10 // Floating levels down level %
mini   = lowest[flLookBack](avg)
maxi   = highest[flLookBack](avg)
rrange = maxi-mini
flu    = mini+flLevelUp*rrange/100.0
fld    = mini+flLevelDown*rrange/100.0
flm    = mini+0.5*rrange

endif

RETURN avg coloured(0,200,200) STYLE(line,2), flu coloured(0,191,255) STYLE(dottedline,1) as "upper level", fld coloured(244,164,96) STYLE(dottedline,1) as "lower level", flm coloured(100,100,100) STYLE(dottedline,1) as "median level"

Download
Filename: PRC_adaptive-SuperTrend.itf
Downloads: 337
Nicolas Master
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
Author’s Profile

Comments

Logo Logo
Loading...