//@version=4
study("RSI LINEAR REGRESION")
len = input(21, "RSI Length", minval=1)
rsi = rsi(close, len)
t(x) => security( syminfo.tickerid, timeframe.period, rsi(x, len))
o = t(open)
h = t(high)
l = t(low)
c = t(close)
upper=input(70)
lower=input(30)
plot(upper, color=color.orange)
plot(lower, color=color.green)
plotcandle(o, h, l, c, color=o < c ? color.green : color.red)
plot(rsi,color=color.black,linewidth=2)
//
period = input( 200, "Period" , input.integer, minval=3)
deviations = input( 2.0, "Deviation(s)" , input.float , minval=0.1, step=0.1)
periodMinusOne = period-1
Ex = 0.0, Ey = 0.0, Ex2 = 0.0, Exy = 0.0, for i=0 to periodMinusOne
closeI = nz(rsi[i]), Ex := Ex + i, Ey := Ey + closeI, Ex2 := Ex2 + (i * i), Exy := Exy + (closeI * i)
ExEx = Ex * Ex, slope = Ex2==ExEx ? 0.0 : (period * Exy - Ex * Ey) / (period * Ex2 - ExEx)
linearRegression = (Ey - slope * Ex) / period
intercept = linearRegression + bar_index * slope
deviation = 0.0, for i=0 to periodMinusOne
deviation := deviation + pow(nz(rsi[i]) - (intercept - slope * (bar_index[i])), 2.0)
deviation := deviations * sqrt(deviation / periodMinusOne)
startingPointY = linearRegression + slope / periodMinusOne
lineColor = startingPointY > startingPointY[1] ? color.blue : color.red
a=plot(startingPointY-deviation, title=" Curve low", color=lineColor, linewidth=3, transp=0)
c1 = plot(startingPointY, title=" Curve", color=lineColor, linewidth=3, transp=0)
b=plot(startingPointY+deviation, title=" Curve high", color=lineColor, linewidth=3, transp=0)
z=startingPointY-deviation
up=crossover(rsi,z)
p=startingPointY+deviation
down=crossunder(rsi,p)
plotshape(up, title='UP', style=shape.xcross, color=color.green, location=location.bottom, textcolor=color.black, text="U", transp = 0, size = size.tiny)
plotshape(down , title='down', style=shape.cross, color=color.red, location=location.top, textcolor=color.black, text="D", transp = 0, size = size.tiny)