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