Ultimate RSI [LuxAlgo]
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © LuxAlgo

//@version=5
indicator("Ultimate RSI [LuxAlgo]", "LuxAlgo - Ultimate RSI")
//------------------------------------------------------------------------------
//Settings
//-----------------------------------------------------------------------------{
length = input.int(14, minval = 2)
smoType1 = input.string('RMA', 'Method', options = ['EMA', 'SMA', 'RMA', 'TMA'])
src = input(close, 'Source')

arsiCss = input(color.silver, 'Color', inline = 'rsicss')
autoCss = input(true, 'Auto', inline = 'rsicss')

//Signal Line
smooth = input.int(14, minval = 1, group = 'Signal Line')
smoType2 = input.string('EMA', 'Method', options = ['EMA', 'SMA', 'RMA', 'TMA'], group = 'Signal Line')

signalCss = input(#ff5d00, 'Color', group = 'Signal Line')

//OB/OS Style
obValue = input.float(80, 'Overbought', inline = 'ob', group = 'OB/OS Style')
obCss = input(#089981, '', inline = 'ob', group = 'OB/OS Style')
obAreaCss = input(color.new(#089981, 80), '', inline = 'ob', group = 'OB/OS Style')

osValue = input.float(20, 'Oversold    ', inline = 'os', group = 'OB/OS Style')
osCss = input(#f23645, '', inline = 'os', group = 'OB/OS Style')
osAreaCss = input(color.new(#f23645, 80), '', inline = 'os', group = 'OB/OS Style')

//-----------------------------------------------------------------------------}
//Functions
//-----------------------------------------------------------------------------{
ma(x, len, maType)=>
    switch maType
        'EMA' => ta.ema(x, len)
        'SMA' => ta.sma(x, len) 
        'RMA' => ta.rma(x, len) 
        'TMA' => ta.sma(ta.sma(x, len), len)
 
//-----------------------------------------------------------------------------}
//Augmented RSI
//-----------------------------------------------------------------------------{
upper = ta.highest(src, length)
lower = ta.lowest(src, length)
r = upper - lower

d = src - src[1]
diff = upper > upper[1] ? r 
  : lower < lower[1] ? -r 
  : d

num = ma(diff, length, smoType1)
den = ma(math.abs(diff), length, smoType1)
arsi = num / den * 50 + 50

signal = ma(arsi, smooth, smoType2)

//-----------------------------------------------------------------------------}
//Plots
//-----------------------------------------------------------------------------{
plot_rsi = plot(arsi, 'Ultimate RSI'
  , arsi > obValue ? obCss
  : arsi < osValue ? osCss 
  : autoCss ? chart.fg_color : arsiCss)

plot(signal, 'Signal Line', signalCss)

//Levels
plot_up = plot(obValue, color = na, editable = false)
plot_avg = plot(50, color = na, editable = false)
plot_dn = plot(osValue, color = na, editable = false)

//OB-OS
fill(plot_rsi, plot_up, arsi > obValue ? obAreaCss : na)
fill(plot_dn, plot_rsi, arsi < osValue ? osAreaCss : na)

//Gradient
fill(plot_rsi, plot_avg, obValue, 50, obAreaCss, color.new(chart.bg_color, 100))
fill(plot_avg, plot_rsi, 50, osValue, color.new(chart.bg_color, 100), osAreaCss)

hline(obValue, 'Overbought')
hline(50, 'Midline')
hline(osValue, 'Oversold')

//-----------------------------------------------------------------------------}