Webby's Quick & Grateful Dead RS
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Amphibiantrading

//@version=5
indicator("Webby's Quick & Grateful Dead RS", overlay = true, shorttitle = 'QGDRS')

// inputs
var g1      = 'RS Line Options'
index       = input.symbol(title='Index to Compare', defval='SPX', group = g1)
lineCol     = input.color(color.blue, 'RS Line Color', group = g1)
rsWidth     = input.int(title='RS Line Width', defval=2, minval=1, maxval=5, group = g1)

var g2      = 'Moving Average Options'
ma1Len      =  input.int(8, 'Quick Moving Average', minval = 3, maxval = 50, step = 1, group = g2, inline = '2')
ma2Len      =  input.int(21, 'Slow Moving Average', minval = 3, maxval = 200, step = 1, group = g2, inline = '3')
ma1Color    =  input.color(color.red, ' ', inline ='2', group = g2)
ma2Color    =  input.color(color.yellow, ' ', inline ='3', group = g2)
ma1Type     =  input.string('SMA', ' ', options = ['EMA', 'SMA'], inline = '2', group = g2)
ma2Type     =  input.string('SMA', ' ', options = ['EMA', 'SMA'], inline = '3', group = g2)
maWidth     =  input.int(title='MA Line Width', defval=1, minval=1, maxval=5, inline = '4', group = g2)

var g3      = 'Display Settings'
multi       = input.int(title='Vertical offset of RS Line', defval=15, minval=1, maxval=500, step=1, group = g3)
qbCol       = input.color(color.new(color.fuchsia,70), 'Quick Break Color', group = g3)
qsCol       = input.color(color.new(color.orange,70), 'Quicksand Color', group = g3)
gdbCol      = input.color(color.new(color.maroon,70), 'Grateful Dead Cross Color', group = g3)

//variables
var qsLev  = 0.0
var qsBool = false

//calculations
spx     = request.security(index, timeframe.period, close)
rs      = close / spx * 100
ma1     = ma1Type == 'EMA' ? ta.ema(rs,ma1Len) : ta.sma(rs, ma1Len)
ma2     = ma2Type == 'EMA' ? ta.ema(rs,ma2Len) : ta.sma(rs, ma2Len)

// conditions
quickBreak  = ta.crossunder(rs, ma1)
quickSand   = rs < ma1 and rs < qsLev and not qsBool
gdb         = ta.crossunder(rs, ma2)
reset       = ta.crossover(rs, ma1)

if quickBreak
    label.new(bar_index, ma1 * multi, ' ', style = label.style_circle, color = qbCol, size = size.tiny)
    alert('Quick Break - RS Line Crossing Under Fast MA', alert.freq_once_per_bar_close)
    qsLev := rs
if quickSand
    label.new(bar_index, rs * multi, ' ', style = label.style_circle, color = qsCol, size = size.tiny)
    alert('Quicksand - RS Line Moving Lower than Quick Break Level', alert.freq_once_per_bar_close)
    qsBool := true
if gdb
    label.new(bar_index, ma2 * multi, ' ', style = label.style_circle, color = gdbCol, size = size.tiny)
    alert('Grateful Dead Break - RS Line Crossing Under Slow MA', alert.freq_once_per_bar_close)
if reset
    qsLev  := na
    qsBool := false

plot(rs * multi, 'RS', lineCol, rsWidth)
plot(ma1 * multi, 'Fast MA', ma1Color, maWidth)
plot(ma2 * multi, 'Slow MA', ma2Color, maWidth)
