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

//@version=5
indicator("RSI + Moving Average","RSI+MA", timeframe="", timeframe_gaps=false)

// RSI
i_rsi_show = input.bool(true, title='Show RSI', inline='rsishow', group='RSI')
i_rsi_color = input.color(color.red, title='', inline='rsishow', group='RSI')
i_rsi_length = input.int(34, 'RSI Length', inline='rsishow', group='RSI')

// MA №1
i_ma_show = input.bool(true, title='MA №1', inline='ma1', group='Moving Averages')
i_ma_color = input.color(color.blue, title='', inline='ma1', group='Moving Averages')
i_ma_type   = input.string("HMA (Hull)", title="", inline="ma1", options=["HMA (Hull)","SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Moving Averages")
i_ma_length = input.int(55, '', inline='ma1', group='Moving Averages')

// MA №2
i_ma2_show = input.bool(false, title='MA №2', inline='ma2', group='Moving Averages')
i_ma2_color = input.color(color.white, title='', inline='ma2', group='Moving Averages')
i_ma2_type   = input.string("HMA (Hull)", title="", inline="ma2", options=["HMA (Hull)","SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Moving Averages")
i_ma2_length = input.int(110, '', inline='ma2', group='Moving Averages')

// MA №3
i_ma3_show = input.bool(false, title='MA №3', inline='ma3', group='Moving Averages')
i_ma3_color = input.color(color.rgb(186, 104, 200, 0), title='', inline='ma3', group='Moving Averages')
i_ma3_type   = input.string("VWMA", title="", inline="ma3", options=["HMA (Hull)","SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Moving Averages")
i_ma3_length = input.int(55, '', inline='ma3', group='Moving Averages')

// Horizontal Areas
i_show_center = input.bool(true, title='Show Center Line', group='Horizontal Areas')
i_show_color_fills = input.bool(true, title='Show Area Color Fills', group='Horizontal Areas')

// Over Bought
i_show_over_bought_lines = input.bool(true, title='', inline='ob', group='Horizontal Areas')
i_over_bought = input.int(55, 'Over Bought', inline='ob', group='Horizontal Areas')
i_over_bought_top = input.int(70, 'Top', inline='ob', group='Horizontal Areas')

// Over Sold
i_show_over_sold_lines = input.bool(true, title='', inline='os', group='Horizontal Areas')
i_over_sold = input.int(45, 'Over Sold', inline='os', group='Horizontal Areas')
i_over_sold_bottom = input.int(30, 'Bottom', inline='os', group='Horizontal Areas')

// Fills
// WARNING:Using Color Inputs causes the fill to not show for future bars past the current bar.
// i_show_color_fills = input.bool(true, title='Show Area Color Fills', group='Area Fills')
// i_over_bought_color = input.color(color.rgb(0, 230, 118, 80), 'Over Bought Fill', inline='fill', group='Area Fills')
// i_center_color = input.color(color.rgb(255, 235, 59, 80), 'Center Fill', inline='fill', group='Area Fills')
// i_over_sold_color = input.color(color.rgb(255, 82, 82, 80), '', inline='fill', group='Area Fills')

// Signals
i_show_breakout = input.bool(true, 'Show Breakout Signals', group='Signals')

// Breakout Signal
i_up_signal_color = input.color(color.lime, 'Up Signal', inline='upsignal', group='Signals')
i_up_signal_size = input.int(4, title='Size', minval=1, maxval=10, inline='upsignal', group='Signals')
i_dn_signal_color = input.color(color.fuchsia, 'Down Signal', inline='dnsignal', group='Signals')
i_dn_signal_size = input.int(4, title='Size', minval=1, maxval=10, inline='dnsignal', group='Signals')

// Cross MA №1
i_show_cross = input.bool(true, 'Show RSI & MA №1 Cross', group='Signals')
i_up_cross_color = input.color(color.lime, 'Up Cross', inline='upcross', group='Signals')
i_up_cross_size = input.int(4, title='Size', minval=1, maxval=10, inline='upcross', group='Signals')
i_dn_cross_color = input.color(color.fuchsia, 'Down Cross', inline='dncross', group='Signals')
i_dn_cross_size = input.int(4, title='Size', minval=1, maxval=10, inline='dncross', group='Signals')

// Cross MA №2
i_show_cross2 = input.bool(false, 'Show RSI & MA №2 Cross', group='Signals')
i_up_cross2_color = input.color(color.lime, 'Up Cross', inline='upcross2', group='Signals')
i_up_cross2_size = input.int(4, title='Size', minval=1, maxval=10, inline='upcross2', group='Signals')
i_dn_cross2_color = input.color(color.fuchsia, 'Down Cross', inline='dncross2', group='Signals')
i_dn_cross2_size = input.int(4, title='Size', minval=1, maxval=10, inline='dncross2', group='Signals')

// Cross MA №3
i_show_cross3 = input.bool(false, 'Show RSI & MA №3 Cross', group='Signals')
i_up_cross3_color = input.color(color.lime, 'Up Cross', inline='upcross3', group='Signals')
i_up_cross3_size = input.int(4, title='Size', minval=1, maxval=10, inline='upcross3', group='Signals')
i_dn_cross3_color = input.color(color.fuchsia, 'Down Cross', inline='dncross3', group='Signals')
i_dn_cross3_size = input.int(4, title='Size', minval=1, maxval=10, inline='dncross3', group='Signals')

ma(source, length, type) =>
    type == "HMA (Hull)" ? ta.hma(source, length) :
     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

rsi = ta.rsi(close, i_rsi_length)

upSignal = ta.crossover(rsi, i_over_sold) ? i_over_sold : float(na)
dnSignal = ta.crossunder(rsi, i_over_bought) ? i_over_bought : float(na)

rsi_ma = i_ma_show ? ma(rsi, i_ma_length, i_ma_type) : na
rsi_ma2 = i_ma2_show ? ma(rsi, i_ma2_length, i_ma2_type) : na
rsi_ma3 = i_ma3_show ? ma(rsi, i_ma3_length, i_ma3_type) : na

upCross = i_ma_show ? ta.crossover(rsi, rsi_ma) : na
dnCross = i_ma_show ? ta.crossunder(rsi, rsi_ma) : na

upCross2 = i_ma2_show ? ta.crossover(rsi, rsi_ma2) : na
dnCross2 = i_ma2_show ? ta.crossunder(rsi, rsi_ma2) : na

upCross3 = i_ma3_show ? ta.crossover(rsi, rsi_ma3) : na
dnCross3 = i_ma3_show ? ta.crossunder(rsi, rsi_ma3) : na

// Note: Using Display to control visibility causes shifting of the area to top of the area instead of 100 and 0.
// var hl_ob = hline(i_over_bought, title='Over Bought', color=color.yellow, display=i_show_over_bought_lines?display.all:display.none)
// var hl_obt = hline(i_over_bought_top, title='Over Bought Top', color=color.lime, display=i_show_over_bought_lines?display.all:display.none)

// var hl_ctr = hline(50, title='Center', color=color.gray, display=i_show_center?display.all:display.none)

// var hl_os = hline(i_over_sold, title='Over Sold', color=color.yellow, display=i_show_over_sold_lines?display.all:display.none)
// var hl_osb = hline(i_over_sold_bottom, title='Over Sold Bottom', color=color.red, display=i_show_over_sold_lines?display.all:display.none)

// Note: Using transparency to hide ob/os lines, otherwise scale can shift
var hl_ob = hline(i_over_bought, title='Over Bought', color=i_show_over_bought_lines ? color.yellow : color.new(color.yellow, 100))
var hl_obt = hline(i_over_bought_top, title='Over Bought Top', color=i_show_over_bought_lines ? color.lime : color.new(color.lime, 100))

var hl_ctr = hline(50, title='Center', color=i_show_center ? color.gray : color.new(color.gray,100))

var hl_os = hline(i_over_sold, title='Over Sold', color=i_show_over_sold_lines ? color.yellow : color.new(color.yellow, 100))
var hl_osb = hline(i_over_sold_bottom, title='Over Sold Bottom', color=i_show_over_sold_lines ? color.red : color.new(color.red, 100))

// fill(hl_ob, hl_obt, title='Over Bought Fill', color=i_over_bought_color, display=i_show_color_fills?display.all:display.none)
// fill(hl_ob, hl_os, title='Center Fill', color=i_center_color, display=i_show_color_fills?display.all:display.none)
// fill(hl_os, hl_osb, title='Over Sold Fill', color=i_over_sold_color, display=i_show_color_fills?display.all:display.none)

fill(hl_ob, hl_obt, title='Over Bought Fill', color=color.rgb(0, 230, 118, 80), display=i_show_color_fills?display.all:display.none)
fill(hl_ob, hl_os, title='Center Fill', color=color.rgb(255, 235, 59, 80), display=i_show_color_fills?display.all:display.none)
fill(hl_os, hl_osb, title='Over Sold Fill', color=color.rgb(255, 82, 82, 80), display=i_show_color_fills?display.all:display.none)

plot(rsi, title='RSI', color=i_rsi_color, linewidth=2, display=i_rsi_show?display.all:display.none)
plot(rsi_ma, title='RSI №1', color=i_ma_color, linewidth=2, display=i_ma_show?display.all:display.none)
plot(rsi_ma2, title='RSI №2', color=i_ma2_color, linewidth=2, display=i_ma2_show?display.all:display.none)
plot(rsi_ma3, title='RSI №3', color=i_ma3_color, linewidth=2, display=i_ma3_show?display.all:display.none)

plot(i_show_breakout and upSignal ? rsi : na, title="Up Signal", color=i_up_signal_color, style=plot.style_circles, linewidth=i_up_signal_size)
plot(i_show_breakout and dnSignal ? rsi : na, title="Dn Signal", color=i_dn_signal_color, style=plot.style_circles, linewidth=i_dn_signal_size)

plot(i_show_cross and upCross ? rsi_ma : na, title="Up Cross №1", color=i_up_cross_color, style=plot.style_cross, linewidth=i_up_cross_size)
plot(i_show_cross and dnCross ? rsi_ma : na, title="Dn Cross №1", color=i_dn_cross_color, style=plot.style_cross, linewidth=i_dn_cross_size)

plot(i_show_cross2 and upCross2 ? rsi_ma2 : na, title="Up Cross №2", color=i_up_cross2_color, style=plot.style_cross, linewidth=i_up_cross2_size)
plot(i_show_cross2 and dnCross2 ? rsi_ma2 : na, title="Dn Cross №2", color=i_dn_cross2_color, style=plot.style_cross, linewidth=i_dn_cross2_size)

plot(i_show_cross3 and upCross3 ? rsi_ma3 : na, title="Up Cross №3", color=i_up_cross3_color, style=plot.style_cross, linewidth=i_up_cross3_size)
plot(i_show_cross3 and dnCross3 ? rsi_ma3 : na, title="Dn Cross №3", color=i_dn_cross3_color, style=plot.style_cross, linewidth=i_dn_cross3_size)


