T.O/REG/Gauss Line
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © shakibsharifian

//@version=5
indicator("T.O/REG/Gauss Line")

vline(BarIndex, Color, LineStyle, LineWidth) => // Verticle Line, 54 lines maximum allowable per indicator
    barindx=bar_index-BarIndex
    _return = line.new(barindx, ta.highest(high,10), barindx, ta.lowest(low,10), xloc.bar_index, extend.both, Color, LineStyle, LineWidth)
linearREGslope(x,y,length)=>
    //y=a*bx
    sumX=math.sum(x,length)
    sumY=math.sum(y,length)
    sumXY=math.sum(x*y,length)
    sumX2=math.sum(math.pow(x,2),length)
    Pow2SumX=math.pow((math.sum(x,length)),2)
    b=((length*sumXY)-(sumX*sumY))/((length*sumX2)-Pow2SumX)
    b

linearREGbias(x,y,length)=>
    //y=a*bx
    sumX=math.sum(x,length)
    sumY=math.sum(y,length)
    sumXY=math.sum(x*y,length)
    sumX2=math.sum(math.pow(x,2),length)
    Pow2SumX=math.pow((math.sum(x,length)),2)
    a=((sumY*sumX2)-(sumX*sumXY))/((length*sumX2)-Pow2SumX)
    a 
//REGRESSION
regLINE(src,length)=>
    b=linearREGslope(bar_index,src,length)
    a=linearREGbias(bar_index,src,length)
    x=bar_index
    y=b*(x)+a
    y
    
gaussianMEAN(src,length,mult)=>
    float sum=0
    for int _i=0 to length-1
        sum:=sum+src[_i]
    _mean=sum/length
    sum:=0
    for int _i=0 to length-1
        sum:=sum+(math.pow((_mean-src[_i]),2))
    _variance=sum/length
    _std=mult*math.sqrt(_variance)

    sum:=0
    float coefSum=0    
    for int _i=0 to length-1
        _coef=(1/(_std*math.sqrt(2*3.1415)))*math.exp(-.5*math.pow(((src[_i]-_mean)/_std),2))
        sum:=sum+(_coef*src[_i])
        coefSum:=coefSum+_coef
    _gaussMean=sum/coefSum
    _gaussMean

srcIni = input.source(low, title='SOURCE',group='GENERAL',inline='a')
bkw  = input.int(200, minval=1, title='Backward',group='GENERAL',inline='b')
mult  = input.float(1, title='GAUSS MULT.',group='GENERAL',inline='c',step=0.1)
supL = input.int(9, minval=1, title='BACKWARD',group='SUPER TREND',inline='a')
fact = input.int(3, minval=1, title='FACTOR',group='SUPER TREND',inline='a')

[_sup,_]=ta.supertrend(fact, supL)
regSup=regLINE(_sup,bkw)
ausw=ta.rma(gaussianMEAN(gaussianMEAN(srcIni,bkw,mult),bkw,mult),bkw)

plot(ausw,color=color.new(#ed37db,0),linewidth=4,title='T.O Line')
plot(gaussianMEAN(srcIni,bkw,mult),color=color.new(#fcba03,0),linewidth=3,title='GAUSS LINE')
plot(regLINE(srcIni,bkw),color=color.new(#03cafc,0),linewidth=2,title='REG. LINE')
plot(regSup,color=color.new(#90f0b0,0),linewidth=2,title='SUPREG. LINE')