Visual RSI
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

//@version=5

indicator(title='Visual RSI', overlay=true)


length = input(14)
overSold = input(35)
overBought = input(65)
p = close

vrsi = ta.rsi(p, length)
price = close
var bool long = na
var bool short = na

long := ta.crossover(vrsi, overSold)
short := ta.crossunder(vrsi, overBought)

var float last_open_long = na
var float last_open_short = na

last_open_long := long ? close : nz(last_open_long[1])
last_open_short := short ? close : nz(last_open_short[1])
mpoint=(last_open_long+last_open_short)/2

entry_value = last_open_long
entry_value1 = last_open_short

// Rounding levels to min tick
nround(x) =>
    n = math.round(x / syminfo.mintick) * syminfo.mintick
    n
//
disp_panels = input(true, title='Display info panels?')
fibs_label_off = input(40, title='fibs label offset')
fibs_label_size = input.string(size.normal, options=[size.tiny, size.small, size.normal, size.large, size.huge], title='fibs label size')
r1_x = timenow + math.round(ta.change(time) * fibs_label_off)
r1_y = last_open_short
text1 = 'High : ' + str.tostring(nround(last_open_short))
s1_y = last_open_long
text3 = 'low : ' + str.tostring(nround(last_open_long))

R1_label = disp_panels ? label.new(x=r1_x, y=r1_y, text=text1, xloc=xloc.bar_time, yloc=yloc.price, color=color.orange, style=label.style_label_down, textcolor=color.black, size=fibs_label_size) : na
S1_label = disp_panels ? label.new(x=r1_x, y=s1_y, text=text3, xloc=xloc.bar_time, yloc=yloc.price, color=color.lime, style=label.style_label_up, textcolor=color.black, size=fibs_label_size) : na

label.delete(R1_label[1])
label.delete(S1_label[1])
//
plot(mpoint, title='avreage', color=color.new(color.red, 40), style=plot.style_linebr, linewidth=3, trackprice=true, offset=-9999)
plot(last_open_short, title='high', color=color.new(color.red, 40), style=plot.style_linebr, linewidth=3, trackprice=true, offset=-9999)
plot(last_open_long, title='low', color=color.new(color.blue, 40), style=plot.style_linebr, linewidth=3, trackprice=true, offset=-9999)
//
trend = input(false)
if barstate.islast and trend == true
    line z = line.new(bar_index[1], last_open_short[1], bar_index, last_open_short, extend=extend.both, color=color.red, style=line.style_dashed, width=1)
    line f = line.new(bar_index[1], mpoint[1], bar_index, mpoint, extend=extend.both, color=color.blue, style=line.style_dashed, width=1)
    line w = line.new(bar_index[1], last_open_long[1], bar_index, last_open_long, extend=extend.both, color=color.green, style=line.style_dashed, width=1)
    line.delete(z[1])
    line.delete(f[1])
    line.delete(w[1])
    
//
len = input.int(200, minval=1)
off = 0
dev = input(2, 'Deviation')
c1 = close

lreg1 = ta.linreg(c1, len, off)
lreg_x1 = ta.linreg(c1, len, off + 1)
b1 = bar_index
s1 = lreg1 - lreg_x1
intr1 = lreg1 - b1 * s1
dS1 = 0.0
for i1 = 0 to len - 1 by 1
    dS1 += math.pow(c1[i1] - (s1 * (b1 - i1) + intr1), 2)
    dS1
de1 = math.sqrt(dS1 / len)
up1 = -de1 * dev + lreg1
down1 = de1 * dev + lreg1
//

//
t1 = 0
x11 = bar_index - len
x21 = bar_index
y11 = s1 * (bar_index - len) + intr1
y21 = lreg1
u1 = t1 <= 0 or bar_index < t1
if u1
    line pr1 = line.new(x11, y11, x21, y21, xloc.bar_index, extend.right, color.red, width=1)
    line.delete(pr1[1])
    line px1 = line.new(x11, de1 * dev + y11, x21, de1 * dev + y21, xloc.bar_index, extend.right, width=1)
    line.delete(px1[1])
    line pz1 = line.new(x11, -de1 * dev + y11, x21, -de1 * dev + y21, xloc.bar_index, extend.right, width=1)
    line.delete(pz1[1])

//
disp_panels1 = input(true, title='Display info panels?')
fibs_label_off1 = 0
fibs_label_size1 = input.string(size.normal, options=[size.tiny, size.small, size.normal, size.large, size.huge], title='label size')
r1_x1 = timenow + math.round(ta.change(time) * fibs_label_off1)
r1_y1 = up1
text11 = 'L : ' + str.tostring(nround(up1))
s1_y1 = down1
text31 = 'H : ' + str.tostring(nround(down1))
M1_y = lreg1
text21 = 'M : ' + str.tostring(nround(lreg1))
R1_label1 = disp_panels1 ? label.new(x=r1_x1, y=r1_y1, text=text11, xloc=xloc.bar_time, yloc=yloc.price, color=color.yellow, style=label.style_label_upper_left, textcolor=color.black, size=fibs_label_size1) : na
S1_label1 = disp_panels1 ? label.new(x=r1_x1, y=s1_y1, text=text31, xloc=xloc.bar_time, yloc=yloc.price, color=color.yellow, style=label.style_label_upper_left, textcolor=color.black, size=fibs_label_size1) : na
M1_label = disp_panels1 ? label.new(x=r1_x1, y=M1_y, text=text21, xloc=xloc.bar_time, yloc=yloc.price, color=color.yellow, style=label.style_label_upper_left, textcolor=color.black, size=fibs_label_size1) : na

label.delete(R1_label1[1])
label.delete(S1_label1[1])
label.delete(M1_label[1])

