RSI Moving Average Crossovers
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © noop42

//@version=5
indicator(title="RSI Moving Average Crossovers", shorttitle="RSIMAC", overlay=false)

ma(source, length, type) =>
    type == "SMA" ? ta.sma(source, length) :
     type == "EMA" ? ta.ema(source, length) :
     type == "SMMA (RMA)" ? ta.rma(source, length) :
     type == "WMA" ? ta.wma(source, length) :
     type == "VWMA" ? ta.vwma(source, length) :
     na

// Options
fastMA_len = input.int(20, "Fast Moving average Length", group="Moving averages")
slowMA_len = input.int(50, "Slow Moving average Length", group="Moving averages")
ma_type = input.string("EMA", title="MA Type", inline="MA #1", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Moving averages")
ma_source = input.source(close, "MA Source", group="Moving averages")

rsi_src = input.source(close, "RSI source", group="RSI")
rsi_len = input.int(14, "RSI Length", group="RSI")
rsi_ma_len = input.int(20, "Moving Average Length", group="RSI")
rsi_ma_type   = input.string("SMMA (RMA)", title="RSI MA Type", inline="MA #1", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="RSI")
ob_level = input.int(70, "Overbought level", group="RSI")
os_level = input.int(30, "Oversold level", group="RSI")

show_rsi_crossovers = input.bool(true, "Plot RSI/MA crossovers", group="Options")
show_ma_crossovers = input.bool(true, "Plot Trend MA crossovers", group="Options")
show_screener = input.bool(true, "Show screener", group="Options")

// RSI
rsi = ta.rsi(rsi_src, rsi_len)
rsi_ma = ma(rsi, rsi_ma_len, rsi_ma_type)

// Moving Averages
s = ma(ma_source, fastMA_len,  ma_type)
s2 = ma(ma_source, slowMA_len, ma_type)

// Colors
uncertain_color=#bababa50
bull_color=#0676cc40
bear_color=#aa000040
bg_bear_trend = #aa000040
bg_bear_correct = #aa000020
bg_bull_trend = #0560f540
bg_bull_correct = #0560f520
up_shape_col=#069ecc
down_shape_col=#aa0000

// Conditions
bull = s > s2 and close > s2
bear = s < s2 and close < s2
col = (bull or bear) ? (rsi > rsi_ma ? bull_color : bear_color) : uncertain_color

// Plots
rsilinecol = (bull or bear) ? (rsi > rsi_ma ? #0676cc : color.red): color.black
malinecol = (bull or bear) ? (rsi < rsi_ma ? #0676cc : color.red): uncertain_color
rsi_line = plot(rsi, color=rsilinecol, title="RSI")
ma_line = plot(rsi_ma, color=malinecol, title="RSI MA")

fill(rsi_line, ma_line, color=col, title="RSI Moving average fill color")
plotshape(show_rsi_crossovers and ta.crossover(rsi, rsi_ma) ? 1 : 0, color=up_shape_col, style=shape.triangleup, location=location.bottom, offset=-1, title="RSI/MA Crossup")
plotshape(show_rsi_crossovers and ta.crossunder(rsi, rsi_ma) ? 1 : 0, color=down_shape_col, style=shape.triangledown, location=location.top, offset=-1, title="RSI/MA Crossdown")

plotshape(show_ma_crossovers and ta.crossover(s, s2) ? 1 : 0, color=up_shape_col, style=shape.xcross, location=location.bottom, offset=-1, title="MA Crossup")
plotshape(show_ma_crossovers and ta.crossunder(s, s2) ? 1 : 0, color=down_shape_col, style=shape.xcross, location=location.top, offset=-1, title="MA Crossdown")

// Table Data
trend = bull ? "Bullish" : bear ? "Bearish": "Uncertain"
rsi_cell_col = rsi > rsi_ma ? bull_color : bear_color
rsi_ma_cell_color = rsi < rsi_ma ? bull_color : bear_color
trend_cell_col = bull ? bull_color : bear ? bear_color : uncertain_color
status = trend != "Uncertain" ? (bull and rsi < rsi_ma and rsi[1] < rsi_ma[1]) or (bear and rsi > rsi_ma and rsi[1] > rsi_ma[1]) ? "Correction" : "Impulse" : trend
status_col = trend == "Uncertain" ? uncertain_color : (bull and rsi < rsi_ma) or (bear and rsi > rsi_ma) ? bear_color : bull_color

var tbl = table.new(position.bottom_left, 5, 5)
if show_screener and barstate.islast
    table.cell(tbl, 0, 0, "RSI",  bgcolor = #cccccc)
    table.cell(tbl, 1, 0, "RSI MA",  bgcolor = #cccccc)
    table.cell(tbl, 2, 0, "MA Trend", bgcolor = #cccccc)
    table.cell(tbl, 3, 0, "Status", bgcolor = #cccccc)

    table.cell(tbl, 0, 1, str.tostring(rsi, "#.##"),  bgcolor = rsi_cell_col, text_color=color.white)
    table.cell(tbl, 1, 1, str.tostring(rsi_ma, "#.##"), bgcolor = rsi_ma_cell_color, text_color=color.white)
    table.cell(tbl, 2, 1, trend, bgcolor = trend_cell_col, text_color=color.white)
    table.cell(tbl, 3, 1, status, bgcolor = status_col, text_color=color.white)

// Background Color
bgcol = (trend == "Bullish" ? (status == "Impulse" ? bg_bull_trend : bg_bull_correct) : trend == "Bearish" ? (status == "Impulse" ? bg_bear_trend : bg_bear_correct) : na)

// Overbought & Oversold levels
h = hline(ob_level, title="Overbought level", color=color.red)
l = hline(os_level, title="Oversold level", color=#0676cc)
m = hline(50, title="middle level", color=color.gray)

// Fill normal zone with trend color
fill(h, l, color=bgcol, title="Trend background color")
