RSI MA Cross
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PtGambler
// Building onto standard RSI indicator with RSI MA crossover signals and added alert conditions


//@version=5
indicator(title="RSI MA Cross", shorttitle="RSI-X[Pt]", format=format.price, precision=2, timeframe="", timeframe_gaps=true)

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)

len = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
src = input.source(close, "Source", group="RSI Settings")
show_smooth = input.bool(true, title="Use smoothed RSI instead", group="RSI Settings")
len2 = input.int(10, title="Smooth period", group="RSI Settings")
col1 = input.color(color.purple, "Color", group = "RSI Settings")

upper_band = input.int(70, "Upper Band (Overbought level)", minval = 0, maxval = 100, group="RSI Settings")
lower_band = input.int(30, "Lower Band (Oversold level)", minval = 0, maxval = 100, group="RSI Settings")

maType = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
malen = input.int(100, title="MA Length", group="MA Settings")
show_X = input.bool(true, title="Show RSI cross overs", group="MA Settings")
col2 = input.color(color.yellow, "Color", group = "MA Settings")

show_trend = input.bool(true, "Show Trend color", inline = "trend", group = "Extra Settings")
bull_col = input.color(color.green, "↑", inline = "trend", group = "Extra Settings")
bear_col = input.color(color.red, "↓", inline = "trend", group = "Extra Settings")
transp = input.int(90, "Transp.",minval = 0, maxval = 100, inline = "trend", group = "Extra Settings")
OSOB_cross_only = input.bool(false, "Shows crossover signals only in OB / OS region", group = "Extra Settings")

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))
rsiSM = ta.sma(rsi,len2)
rsiMA = ma(rsi, malen, maType)

bullX = show_smooth ? ta.crossover(rsiSM, rsiMA) : ta.crossover(rsi, rsiMA)
bearX = show_smooth ? ta.crossunder(rsiSM, rsiMA) : ta.crossunder(rsi, rsiMA)

bullX := OSOB_cross_only ? bullX and rsiMA < lower_band : bullX
bearX := OSOB_cross_only ? bearX and rsiMA > upper_band : bearX

plotshape(show_X and bullX, style=shape.triangleup, location=location.bottom, color=color.green, title="Cross Up", size = size.tiny)
plotshape(show_X and bearX, style=shape.triangledown, location=location.top, color=color.red, title="Cross Down", size = size.tiny)

p1 = plot(show_smooth ? rsiSM : rsi, "RSI", color=color.purple, linewidth=3)
p2 = plot(rsiMA, "RSI-based MA", color=color.yellow, linewidth=2)

trend_color = show_smooth ? (rsiSM > rsiMA ? bull_col : bear_col) : (rsi > rsiMA ? bull_col : bear_col)
fill(p1,p2, color = show_trend ? color.new(trend_color,transp) : na)

rsiUpperBand = hline(upper_band, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(color.white, 50))
rsiLowerBand = hline(lower_band, "RSI Lower Band", color=#787B86)

fill(rsiUpperBand, rsiLowerBand, color=color.new(color.gray,90), title="RSI Background Fill")

alertcondition(bullX, title='Bullish Crossover', message='RSI Cross, Bullish Signal')
alertcondition(bearX, title='Bearish Crossover', message='RSI Cross, Bearish Signal')
