Linear Average Price
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © faytterro

//@version=5
indicator("Linear Average Price", overlay=true)
len=input.int(200,title="lenght")
src=input(hlc3, title="source")
sum1=0.0
sum2=0.0
for i=0 to len-1
    sum2+=src[i]/len
for i=0 to len-2
    sum1+=(len-1)*(src[i+1]-src[i])/(2*len-2)
x1=last_bar_index-len+1
x2=last_bar_index
y1=sum2+sum1
y2=sum2-sum1
dev=ta.stdev(src,len)
ldev=input.float(1, title="range", step=0.1, minval=0.5, maxval=2)
linreg=line.new(x1, y1, x2, y2, color= y1<y2? color.rgb(76, 175, 79, 20) : color.rgb(255, 82, 82, 20), extend=extend.right, width=2)
line.delete(linreg[1])
linregtop=line.new(x1, y1+dev*ldev, x2, y2+dev*ldev, color= color.rgb(255, 82, 82, 100), extend=extend.right, width=1)
line.delete(linregtop[1])
linregbot=line.new(x1, y1-dev*ldev, x2, y2-dev*ldev, color=color.rgb(76, 175, 79, 100) , extend=extend.right, width=1)
line.delete(linregbot[1])
linregtop2=line.new(x1, y1+dev*ldev*2, x2, y2+dev*ldev*2, color= color.rgb(255, 82, 82, 100), extend=extend.right, width=1)
line.delete(linregtop2[1])
linregbot2=line.new(x1, y1-dev*ldev*2, x2, y2-dev*ldev*2, color=color.rgb(76, 175, 79, 100) , extend=extend.right, width=1)
line.delete(linregbot2[1])
linregtop3=line.new(x1, y1+dev*ldev*4, x2, y2+dev*ldev*4, color= color.rgb(255, 82, 82, 100), extend=extend.right, width=1)
line.delete(linregtop3[1])
linregbot3=line.new(x1, y1-dev*ldev*4, x2, y2-dev*ldev*4, color=color.rgb(76, 175, 79, 100) , extend=extend.right, width=1)
line.delete(linregbot3[1])
linregtop4=line.new(x1, y1+dev*ldev*0.5, x2, y2+dev*ldev*0.5, color= color.rgb(255, 82, 82, 100), extend=extend.right, width=1)
line.delete(linregtop4[1])
linregbot4=line.new(x1, y1-dev*ldev*0.5, x2, y2-dev*ldev*0.5, color=color.rgb(76, 175, 79, 100) , extend=extend.right, width=1)
line.delete(linregbot4[1])
linefill.new(linregbot2, linregbot, color=color.rgb(76, 175, 79, 80))
linefill.new(linregtop2, linregtop, color=color.rgb(255, 82, 82, 80))
linefill.new(linregbot2, linregbot3, color=color.rgb(76, 175, 79, 70))
linefill.new(linregtop2, linregtop3, color=color.rgb(255, 82, 82, 70))
linefill.new(linregbot4, linregbot, color=color.rgb(76, 175, 79, 90))
linefill.new(linregtop4, linregtop, color=color.rgb(255, 82, 82, 90))

