Waddah Attar RSI Levels [Loxx]
// This source code is subject to the terms of the Mozilla Public license 2.0 at https://mozilla.org/MPl/2.0/
// © loxx

//@version=5
indicator("Waddah Attar RSI levels [Loxx]", shorttitle = "WARSIlVlS [Loxx]", overlay = true)

greencolor = #2DD204
redcolor = #D2042D 
bluecolor = #042dd2

lightgreencolor = #96E881 
lightredcolor =  #DF4F6C
lightbluecolor =  #4f6cdf

darkGreenColor =  #1B7E02 
darkRedColor = #93021F
darkBlueColor = #021f93

_wilders_rsi(x, y) => 
    res = ta.rsi(x, y)
    res

_rapid_rsi(src, len)=>
    upSum = math.sum(math.max(ta.change(src), 0), len)
    dnSum = math.sum(math.max(-ta.change(src), 0), len)
    rrsi = dnSum == 0 ? 100 : upSum == 0 ? 0 : 100 - 100 / (1 + upSum / dnSum)
    rrsi
    
f_tickFormat() =>
    _s = str.tostring(syminfo.mintick)
    _s := str.replace_all(_s, '25', '00')
    _s := str.replace_all(_s, '5', '0')
    _s := str.replace_all(_s, '1', '0')
    _s
    
tfInMinutes(simple string tf = "") =>
    float chartTf =
      timeframe.multiplier * (
      timeframe.isseconds ? 1. / 60             :
      timeframe.isminutes ? 1.                  :
      timeframe.isdaily   ? 60. * 24            :
      timeframe.isweekly  ? 60. * 24 * 7        :
      timeframe.ismonthly ? 60. * 24 * 30.4375  : na)
    float result = tf == "" ? chartTf : request.security(syminfo.tickerid, tf, chartTf)
    
float chartTFInMinutes = tfInMinutes()

if chartTFInMinutes > 60
    runtime.error("Error: Invalid timerame. This indicaator only works on timeframes 1 hour and below.")

rsiType = input.string("Wilders", "RSI Type", options =["Wilders", "Rapid"], group = "Basic Settings")

_close0 = request.security(syminfo.tickerid, "D", close, barmerge.gaps_off, barmerge.lookahead_on)
_close1 = request.security(syminfo.tickerid, "D", close[1], barmerge.gaps_off, barmerge.lookahead_on)
_close2 = request.security(syminfo.tickerid, "D", close[2], barmerge.gaps_off, barmerge.lookahead_on)
_close3 = request.security(syminfo.tickerid, "D", close[3], barmerge.gaps_off, barmerge.lookahead_on)
_open1 = request.security(syminfo.tickerid, "D", open[1], barmerge.gaps_off, barmerge.lookahead_on)
_high1 = request.security(syminfo.tickerid, "D", high[1], barmerge.gaps_off, barmerge.lookahead_on)
_low1 = request.security(syminfo.tickerid, "D", low[1], barmerge.gaps_off, barmerge.lookahead_on)

int kRSIDayPeriod= 5

wrsi0 = request.security(syminfo.tickerid, "D", _wilders_rsi(close, kRSIDayPeriod), barmerge.gaps_off, barmerge.lookahead_on)
wrsi1 = request.security(syminfo.tickerid, "D", _wilders_rsi(close[1], kRSIDayPeriod), barmerge.gaps_off, barmerge.lookahead_on)
wrsi2 = request.security(syminfo.tickerid, "D", _wilders_rsi(close[2], kRSIDayPeriod), barmerge.gaps_off, barmerge.lookahead_on)
wrsi3 = request.security(syminfo.tickerid, "D", _wilders_rsi(close[3], kRSIDayPeriod), barmerge.gaps_off, barmerge.lookahead_on)

raprsi0 = request.security(syminfo.tickerid, "D", _rapid_rsi(close, kRSIDayPeriod), barmerge.gaps_off, barmerge.lookahead_on)
raprsi1 = request.security(syminfo.tickerid, "D", _rapid_rsi(close[1], kRSIDayPeriod), barmerge.gaps_off, barmerge.lookahead_on)
raprsi2 = request.security(syminfo.tickerid, "D", _rapid_rsi(close[2], kRSIDayPeriod), barmerge.gaps_off, barmerge.lookahead_on)
raprsi3 = request.security(syminfo.tickerid, "D", _rapid_rsi(close[3], kRSIDayPeriod), barmerge.gaps_off, barmerge.lookahead_on)

rsi0 = rsiType == "Wilders" ? wrsi0 : raprsi0 
rsi1 = rsiType == "Wilders" ? wrsi1 : raprsi1  
rsi2 = rsiType == "Wilders" ? wrsi2 : raprsi2 
rsi3 = rsiType == "Wilders" ? wrsi3 : raprsi3  

c1 = nz(_close1)
c2 = nz(_close2)

drsi = rsi1 - rsi2
trsi = rsi3

if(drsi == 0)  
    rsi2 := trsi

drsi := rsi1 - rsi2
if(drsi == 0)  
    drsi := rsi1

dc = c1 - c2
if(dc == 0) 
    c2 := nz(_close3)

dc := c1 - c2
if(dc == 0) 
    dc := c1


l10 = (((10 - rsi1) * dc) / drsi) + c1
lvl10Buffer = l10

l20 = (((20 - rsi1) * dc) / drsi) + c1
lvl20Buffer = l20

l30 = (((30 - rsi1) * dc) / drsi) + c1
lvl30Buffer = l30

l40 = (((40 - rsi1) * dc) / drsi) + c1
lvl40Buffer = l40

l50 = (((50 - rsi1) * dc) / drsi) + c1
lvl50Buffer = l50

l60 = (((60 - rsi1) * dc) / drsi) + c1
lvl60Buffer = l60

l70 = (((70 - rsi1) * dc) / drsi) + c1
lvl70Buffer = l70

l80 = (((80 - rsi1) * dc) / drsi) + c1
lvl80Buffer = l80

l90 = (((90 - rsi1) * dc) / drsi) + c1
lvl90Buffer = l90


dn = plot(lvl10Buffer, "RSI level 10", color = redcolor, style = plot.style_stepline, linewidth = 4)

plot(lvl20Buffer, "RSI level 20", color = redcolor, style = plot.style_stepline, linewidth = 1)
plot(lvl30Buffer, "RSI level 30", color = redcolor, style = plot.style_stepline, linewidth = 1)
plot(lvl40Buffer, "RSI level 40", color = redcolor, style = plot.style_stepline, linewidth = 1)

mid = plot(lvl50Buffer, "RSI level 50", color = color.white, style = plot.style_stepline, linewidth = 2)

plot(lvl60Buffer, "RSI level 60", color = greencolor, style = plot.style_stepline, linewidth = 1)
plot(lvl70Buffer, "RSI level 70", color = greencolor, style = plot.style_stepline, linewidth = 1)
plot(lvl80Buffer, "RSI level 80", color = greencolor, style = plot.style_stepline, linewidth = 1)

up = plot(lvl90Buffer, "RSI level 90", color = greencolor, style = plot.style_stepline, linewidth = 4)

fill(dn, mid, title = "Fill down", color = color.new(redcolor, 90))
fill(up, mid, title = "Fill top", color = color.new(greencolor, 90))

 
l10Bufferlabel = 
     label.new(x=bar_index[1] + 5, 
         y = l10, 
         textalign = text.align_center, 
         color = darkRedColor,
         textcolor = color.white, 
         text= "RSI 10: " + str.tostring(l10, f_tickFormat()),
         size=size.normal, 
         style=label.style_label_left)  

l20Bufferlabel = 
     label.new(x=bar_index[1] + 5, 
         y = l20, 
         textalign = text.align_center, 
         color = darkRedColor,
         textcolor = color.white, 
         text= "RSI 20: " + str.tostring(l20, f_tickFormat()),
         size=size.normal, 
         style=label.style_label_left)  
         
l30Bufferlabel = 
     label.new(x=bar_index[1] + 5, 
         y = l30, 
         textalign = text.align_center, 
         color = darkRedColor,
         textcolor = color.white, 
         text= "RSI 30: " + str.tostring(l30, f_tickFormat()), 
         size=size.normal, 
         style=label.style_label_left) 
         
l40Bufferlabel = 
     label.new(x=bar_index[1] + 5, 
         y = l40, 
         textalign = text.align_center, 
         color = darkRedColor,
         textcolor = color.white, 
         text= "RSI 40: " + str.tostring(l40, f_tickFormat()), 
         size=size.normal, 
         style=label.style_label_left) 
         
l50Bufferlabel = 
     label.new(x=bar_index[1] + 5, 
         y = l50, 
         textalign = text.align_center, 
         color = color.gray,
         textcolor = color.white, 
         text= "RSI 50: " + str.tostring(l50, f_tickFormat()),
         size=size.normal, 
         style=label.style_label_left) 
   
l60Bufferlabel = 
     label.new(x=bar_index[1] + 5, 
         y = l60, 
         textalign = text.align_center, 
         color = darkGreenColor,
         textcolor = color.white, 
         text= "RSI 60: " + str.tostring(l60, f_tickFormat()),
         size=size.normal, 
         style=label.style_label_left) 
         
l70Bufferlabel = 
     label.new(x=bar_index[1] + 5, 
         y = l70, 
         textalign = text.align_center, 
         color = darkGreenColor,
         textcolor = color.white, 
         text= "RSI 70: " + str.tostring(l70, f_tickFormat()),
         size=size.normal, 
         style=label.style_label_left) 
      
l80Bufferlabel = 
     label.new(x=bar_index[1] + 5, 
         y = l80, 
         textalign = text.align_center, 
         color = darkGreenColor,
         textcolor = color.white, 
         text= "RSI 80: " + str.tostring(l80, f_tickFormat()),
         size=size.normal, 
         style=label.style_label_left) 
         
l90Bufferlabel = 
     label.new(x=bar_index[1] + 5, 
         y = l90, 
         textalign = text.align_center, 
         color = darkGreenColor,
         textcolor = color.white, 
         text= "RSI 90: " + str.tostring(l90, f_tickFormat()),
         size=size.normal, 
         style=label.style_label_left) 
      
 
label.delete(l10Bufferlabel[1])
label.delete(l20Bufferlabel[1])
label.delete(l30Bufferlabel[1])
label.delete(l40Bufferlabel[1])
label.delete(l50Bufferlabel[1])
label.delete(l60Bufferlabel[1])
label.delete(l70Bufferlabel[1])
label.delete(l80Bufferlabel[1])
label.delete(l90Bufferlabel[1])
 

































