defparam drawonlastbaronly = true
defparam calculateonlastbars = 1000
// --- settings
lookback= 200 //channel period
NbDeviation = 1 //Deviation multiplier
// ---------------------------------------------------------------------
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)
dat = ste[lookback]*NbDeviation
drawsegment(barindex[lookback],(a+b*lookback)+dat,barindex,a+b*0+dat)
drawsegment(barindex[lookback],(a+b*lookback)-dat,barindex,a+b*0-dat)
Return