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

//@version=5
indicator("Zwei EMAs")

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)

rsiLengthInput = input.int(14, 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(14, title="MA Length", group="MA Settings")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")

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"

plot(rsi, "RSI", color=#7E57C2)
plot(rsiMA, "RSI-based MA", color=color.yellow)
rsiUpperBand = hline(70, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(30, "RSI Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")
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")

ma2(source2, length2, type2) =>
    switch type2
        "SMA2" => ta.sma(source2, length2)
        "Bollinger Bands2" => ta.sma(source2, length2)
        "EMA2" => ta.ema(source2, length2)
        "SMMA (RMA)2" => ta.rma(source2, length2)
        "WMA2" => ta.wma(source2, length2)
        "VWMA2" => ta.vwma(source2, length2)

rsiLengthInput2 = input.int(14, minval=1, title="RSI Length2", group="RSI Settings2")
rsiSourceInput2 = input.source(close, "Source2", group="RSI Settings2")
maTypeInput2 = input.string("SMA2", title="MA Type2", options=["SMA2", "Bollinger Bands2", "EMA2", "SMMA (RMA)2", "WMA2", "VWMA2"], group="MA Settings2")
maLengthInput2 = input.int(14, title="MA Length2", group="MA Settings2")
bbMultInput2 = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev2", group="MA Settings2")

up2 = ta.rma(math.max(ta.change(rsiSourceInput2), 0), rsiLengthInput2)
down2 = ta.rma(-math.min(ta.change(rsiSourceInput2), 0), rsiLengthInput2)
rsi2 = down2 == 0 ? 100 : up2 == 0 ? 0 : 100 - (100 / (1 + up2 / down2))
rsiMA2 = ma2(rsi2, maLengthInput2, maTypeInput2)
isBB2 = maTypeInput2 == "Bollinger Bands2"

plot(rsi2, "RSI2", color=#7E57C2)
plot(rsiMA2, "RSI-based MA2", color=color.yellow)
rsiUpperBand2 = hline(70, "RSI Upper Band2", color=#787B86)
hline(50, "RSI Middle Band2", color=color.new(#787B86, 50))
rsiLowerBand2 = hline(30, "RSI Lower Band2", color=#787B86)
fill(rsiUpperBand2, rsiLowerBand2, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill2")
bbUpperBand2 = plot(isBB2 ? rsiMA2 + ta.stdev(rsi2, maLengthInput2) * bbMultInput2 : na, title = "Upper Bollinger Band2", color=color.green)
bbLowerBand2 = plot(isBB2 ? rsiMA2 - ta.stdev(rsi2, maLengthInput2) * bbMultInput2 : na, title = "Lower Bollinger Band", color=color.green)
fill(bbUpperBand2, bbLowerBand2, color= isBB ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill2")
plot(close)
