Relative Strength Index w/ STARC Bands and Pivots
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jsalemfinancial

//@version=5
indicator(title="Relative Strength Index w/ STARC Bands and Pivots", shorttitle="RSI w/ STARC & Pivots", format=format.price, precision=2)
//import jsalemfinancial/Joe_Library/3 as jlib

band_bool = input.bool(defval=false, title="STARC Band")
atr_len = input.int(defval=15, title="ATR Length")
fast_len = input.int(defval=10, title="Fast EMA Length")
slow_len = input.int(defval=20, title="Slow EMA Length")
mav_len = input.int(defval=6, title="STARC MA Length")
local_minmax_len = input.int(defval=14, title="Local Min/Max Length")
factor1 = input.float(defval=2.3, title="Top Factor")
factor2 = input.float(defval=2.1, title="Bot Factor")
col = input.color(defval=color.yellow, title="Band Color")

label_bool = input.bool(defval=false, title="Pivot HL Labels")
len_high = input.int(title="Pivot High", defval=10, minval=1, inline="Pivot High")
len_low = input.int(title="Pivot Low", defval=10, minval=1, inline="Pivot Low")

rsi_len = input.int(defval = 14, minval = 5, title="RSI Length")

rsi = ta.rsi(close, rsi_len)

band1 = hline(85, "Upper Bull Res Band", color=#787B86)
band2 = hline(75, "Lower Bull Res Band", color=#787B86)
band3 = hline(50, "Upper Bull Sup Band", color=#787B86)
band4 = hline(40, "Lower Bull Sup Band", color=#787B86)
band5 = hline(65, "Upper Bear Res Band", color=#787B86)
band6 = hline(55, "Lower Bear Res Band", color=#787B86)
fill(band6, band5, color=color.rgb(0, 0, 180, 80), title="Background")
fill(band4, band3, color=color.rgb(180, 0, 0, 80), title="Background")
fill(band2, band1, color=color.rgb(0, 180, 0, 80), title="Background")

plot(ta.ema(rsi, fast_len), color=color.red, linewidth=1)
plot(ta.ema(rsi, slow_len), color=color.blue, linewidth=1)

rsi_tr = math.max(ta.highest(rsi, local_minmax_len) - ta.lowest(rsi, local_minmax_len), 
     math.abs(ta.highest(rsi, local_minmax_len) - rsi[1]), math.abs(ta.lowest(rsi, local_minmax_len) - rsi[1]))
rsi_atr = ta.sma(rsi_tr, atr_len)
mav = ta.sma(rsi, mav_len)

top = mav + (factor1 * rsi_atr)
bot = mav - (factor2 * rsi_atr)

if (top <= rsi or band_bool == false)
    top := na
if (bot >= rsi or band_bool == false)
    bot := na

ph = ta.pivothigh(rsi, len_high, len_high)
pl = ta.pivotlow(rsi, len_low, len_low)

drawLabel(_offset, _pivot, _style, _color, _textColor) =>
    if not na(_pivot)
        label.new(bar_index[_offset], _pivot, str.tostring(_pivot, format.mintick), style=_style, color=_color, textcolor=_textColor)


plot(top, title="stoller_hi", color=col)
plot(bot, title="stoller_lo", color=col)
plot(rsi, "RSI", color=color.white, linewidth=2)

if (label_bool == true)
    drawLabel(len_high, ph, label.style_label_down, color.green, color.white)
    drawLabel(len_low, pl, label.style_label_up, color.maroon, color.white)