Relative Strength Index Ex
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gautamsatpathy

//@version=5
indicator(title="Relative Strength Index Ex", shorttitle="RSI Ex", format=format.price, precision=2, timeframe="", timeframe_gaps=true)

///////////////////////////////////////////////////////////////////////////////
//	functions
///////////////////////////////////////////////////////////////////////////////
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

///////////////////////////////////////////////////////////////////////////////
//	Inputs
///////////////////////////////////////////////////////////////////////////////
rsiLengthInput = input.int( 12, minval=1, title = "RSI Length", group = "RSI Settings")
rsiSourceInput = input.source( close, "Source", group="RSI Settings" )
maTypeInput = input.string( "SMA", title = "MA Type", options = ["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group = "MA Settings" )
maLengthInput = input.int( 4, title = "MA Length", group = "MA Settings" )
bbMultInput = input.float( 2.0, minval = 0.001, maxval = 50, title = "BB StdDev", group = "MA Settings" )
rsima2len = input.int( 12, minval = 1, title = "RSI MA 2 Length" )

///////////////////////////////////////////////////////////////////////////////
//	Calc
///////////////////////////////////////////////////////////////////////////////
up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands"
rsima2 = ta.sma( rsi, rsima2len )

///////////////////////////////////////////////////////////////////////////////
//	Plot
///////////////////////////////////////////////////////////////////////////////
plot( rsi, "RSI", color = color.fuchsia, linewidth = 2 ) // #7E57C2
plot( rsiMA, "RSI-based MA", color = color.orange, linewidth = 2 )

//	RSI Bands
rsiUpperBand = hline( 70, "RSI Upper Band", color = #787B86, linewidth = 1 )
hline( 50, "RSI Middle Band", color = color.new(#787B86, 50), linewidth = 1 )
rsiLowerBand = hline( 30, "RSI Lower Band", color = #787B86, linewidth = 1 )
fill( rsiUpperBand, rsiLowerBand, color = color.rgb(126, 87, 194, 90), title = "RSI Background Fill" )

//	Bollinger on RSI
bbUpperBand = plot( isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Upper Bollinger Band", color=color.green )
bbLowerBand = plot( isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Lower Bollinger Band", color=color.green )
fill( bbUpperBand, bbLowerBand, color = isBB ? color.new(color.green, 90) : na, title = "Bollinger Bands Background Fill" )

//	RSI MA 2
plot( rsima2, "RSI-based MA 2", color = color.black, linewidth = 2 )

//	RSI Support & Resistance Bands
rsiResistanceBand = hline( 55, "RSI Resistance Band", color = #787B86, linewidth = 1 )
rsiSupportBand = hline( 45, "RSI Support Band", color = #787B86, linewidth = 1 )
fill( rsiResistanceBand, rsiSupportBand, color = color.rgb(255, 255, 0, 90), title = "RSI R-S Band Fill" )

