// LINEAR REGRESSION CHANNEL by Nicolas modifié 19 juin 2022
//PRC_Std and Ste LinRegChannel indicator //Standard Deviation and Standard Error
//Linear Regression Channel 12.03.2019
defparam drawonlastbaronly=true
// — settings
// lookback= max(1,barindex) //channel period
ChannelType = 1 //1= Standard Deviation ; 2= Standard Erro
NbDeviation = 1 //Deviation multiplier
colorRed = 255
colorGreen = 255
colorBlue = 0
// — end of settings
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)style(line,5) 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)style(line,3)
/////////////////////////////////////////////////////
//////////////////////////////////////////////////////
drawsegment(barindex[lookback],(a+b*lookback)+dat,barindex,a+b*0+dat) coloured(colorRed,colorGreen,colorBlue)style(line,3)
NbDeviation2 = 2 //Deviation multiplier
//channel 2
if ChannelType = 2 then //Standard Deviation
dat2 = std[lookback]*NbDeviation2
else
dat2 = ste[lookback]*NbDeviation2
endif
drawsegment(barindex[lookback],(a+b*lookback)–dat2,barindex,a+b*0–dat2) coloured (0,255,8)//(colorRed,colorGreen,colorBlue)
drawsegment(barindex[lookback],(a+b*lookback)+dat2,barindex,a+b*0+dat2) coloured (255,9,0)
/////////////////////////////////////////////////////
return customclose as ” Linear regression channel “
// Variable :
// lookback = 365