Nicolas

Anchored Standard Deviation & Standard Error Linear Regression Channel

Category: Indicators By: Nicolas Created: June 28, 2019, 8:15 AM
June 28, 2019, 8:15 AM
Indicators
9 Comments
Anchored Standard Deviation & Standard Error Linear Regression Channel

This version of the Linear Regression Channel with Standard Deviation (STD) and Standard Error (STE) bands has dynamic lookback for its calculation.

You can define in the setting a precise date and time to anchor the start of the channel, so its length evolves as time passing by. It can be useful to spot potential breakout of a defined trend or rebound on the top and bottom of the channel.

The original version with fixed bars lookback can be found here: Standard Deviation & Standard Error Linear Regression Channel

 

//PRC_StdSte LinRegChannAnchored | indicator
//Standard Deviation and Standard Error with date&time anchor
//Linear Regression Channel
//28.06.2019
//Nicolas @ www.prorealcode.com
//Sharing ProRealTime knowledge

defparam drawonlastbaronly=true
defparam calculateonlastbars=1000
// --- settings
AnchorDate = 20190628 //Date anchor YYYYMMDD format
AnchorTime = 050000 //Time anchor HHMMSS format
ChannelType = 1 //1= Standard Deviation ; 2= Standard Erro
NbDeviation = 1 //Deviation multiplier
colorRed = 255
colorGreen = 255
colorBlue = 0
// --- end of settings

if date=anchordate and time=anchortime then
 startbar=barindex
endif

lookback = max(1,barindex-startbar)

if lookback>0 and date>=AnchorDate then
 sumx = 0
 sumy = 0
 sumxy = 0
 sumx2 = 0

 for cmpt = lookback downto 0 do
  tmpx = cmpt
  tmpy = close[cmpt]
  sumy = sumy+tmpy
  sumx = sumx+tmpx
  sumx2 = sumx2 + (tmpx*tmpx)
  sumxy = sumxy + (tmpy*tmpx)
 next

 n = lookback+1

 if (sumx2 = sumx * sumx) then // protection to avoid infinite values
  b = sumxy - sumx * sumy
 else
  b = (n * sumxy - sumx * sumy) / (n * sumx2 - sumx * sumx)
 endif
 a = (sumy - b * sumx) / n

 drawsegment(barindex[lookback],a+b*lookback,barindex,a+b*0) coloured(colorRed,colorGreen,colorBlue)

 //channel
 if ChannelType = 1 then //Standard Deviation
  dat = std[lookback]*NbDeviation
 else
  dat = ste[lookback]*NbDeviation
 endif
 drawsegment(barindex[lookback],(a+b*lookback)+dat,barindex,a+b*0+dat) coloured(colorRed,colorGreen,colorBlue)
 drawsegment(barindex[lookback],(a+b*lookback)-dat,barindex,a+b*0-dat) coloured(colorRed,colorGreen,colorBlue)
endif

return

 

Download
Filename: PRC_StdSte-LinRegChannAnchored.itf
Downloads: 479
Nicolas
Nicolas Legend
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

jonpt88
4 years ago
#

interesting code thanks!

redactv7
4 years ago
#

On my charts, the indicator doesnt go on the upper pannel, on prices, but under like an oscillator... What should I do to get it on prices

Nicolas
4 years ago
#

apply in on the chart, look at this how-to video: https://www.prorealcode.com/blog/video-tutorials/how-to-add-an-indicator-on-price-prorealtime/

SB-FO
6 years ago
#

Thanks Nicolas.

SB-FO
6 years ago
#

Nicolas, can you share with me how PRT calculates LinearRegression ? Is it (3 * WeightedAverage Close X - 2 * AVERAGE Close X) X= Period?

Nicolas
6 years ago
#

The above code is the way it is calculated in PRT. However the exact indicator can be found here: https://www.prorealcode.com/prorealtime-indicators/standard-deviation-standard-error-linear-regression-channel/

Violet
7 years ago
#

Hi Nicolas, There seems to be a small error in the code, which causes the channel not to be shown. Line 20 should read as follows: if date = anchordate and time >= anchortime then Note the condition : >= anchortime Regards, violet

Nicolas
7 years ago
#

Hmm, I don't think so, otherwise the channel will have a moving start and not anchored anymore. If the channel doesn't show, it may because the starttime is not met in the timeframe you are using.

swapping
7 years ago
#

Excellent Nicolas ;)

ProRealCode ProRealCode
Loading...