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


//@version=5
indicator("RSI Chart Levels", overlay=true, max_bars_back = 100,max_labels_count = 15,max_lines_count = 15)

////INPUTs
inputgroup = "**********INPUTs**********"

shtline = input.bool(defval = true, title = "Show Trend Lines", group=inputgroup)



////RSI
rsigroup = "**********RSI**********"
len = input.int(14, minval=1, title='RSI Length', group = rsigroup)
src = input(close, 'RSI Source', group = rsigroup)
up = ta.rma(math.max(ta.change(src), 0), len)
down = ta.rma(-math.min(ta.change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - 100 / (1 + up / down)






////TRENDLINEs
trendgroup = "**********TREND LINE**********"
length = input(10, title='Zigzag Length')

r = rsi[length]
l = rsi[14]

ph = ta.pivothigh(rsi,length,length)
pl = ta.pivotlow(rsi,length,length)
valH = ta.valuewhen(ph,r,0)
valL = ta.valuewhen(pl,r,0)
valpH = ta.valuewhen(ph,r,1)
valpL = ta.valuewhen(pl,r,1)

d = math.abs(r)
n = bar_index
label lbl = na
HIH = valH > valpH ? "Higher High" : na 
HIL = valH < valpH ? "Lower High" : na
LOL = valL < valpL ? "Lower Low" : na
LOH = valL > valpL ? "Higher Low" : na 

if ph and valH > valpH and shtline
    lbl := label.new(n[length],close[length],HIH,color=#ff1100  ,
      style=label.style_label_down,textcolor=color.white,size=size.tiny)
    label.delete(lbl[2]) 
    linehh = line.new(n[length],close[length], bar_index,close[length], extend=extend.right, color=color.red)
    line.delete(linehh[2])
else if ph and valH < valpH and shtline
    lbl := label.new(n[length],close[length],HIL,color=#e79700 ,
      style=label.style_label_down,textcolor=color.white,size=size.tiny)
    label.delete(lbl[2])
    linehl = line.new(n[length],close[length], bar_index, close[length], extend=extend.right, color=color.orange)
    line.delete(linehl[2])
else if pl and valL < valpL and shtline
    lbl := label.new(n[length],close[length],LOL,color=#009c0c   ,  
      style=label.style_label_up,textcolor=color.white,size=size.tiny)
    label.delete(lbl[2]) 
    linell = line.new(n[length],close[length], bar_index, close[length], extend=extend.right, color=color.green)
    line.delete(linell[2])
else if pl and valL > valpL and shtline
    lbl := label.new(n[length],close[length],LOH,color= #0073ee ,
      style=label.style_label_up,textcolor=color.white,size=size.tiny)
    label.delete(lbl[2]) 
    linelh = line.new(n[length],close[length], bar_index, close[length], extend=extend.right, color=color.blue)
    line.delete(linelh[2])
label.delete(lbl[200])  
