Stochastic RSI+ Support/Resistance (beta)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Electrified (electrifiedtrading)
// @version=4
study(title="Stochastic RSI+ Support/Resistance (beta)", shorttitle="SRSI+ S/R", overlay=true, format=format.price, precision=2, resolution="")

kcolor = #0094FF
dcolor = #FF6A00
WMA = "WMA", EMA = "EMA", SMA = "SMA", VWMA = "VWMA", VAWMA = "VAWMA"

///////////////////////////////////////////////////
// Input

k_mode      = input(SMA, "K Mode", inline="Source", options=[SMA, EMA, WMA, VWMA, VAWMA])
src         = input(close, "Source", inline="Source")
smoothK     = input(3, "K", inline="Values", minval=1)
smoothD     = input(3, "D", inline="Values", minval=1)


lengthRSI   = input(64, "RSI", group="Lengths", inline="Lengths", minval=1)
lengthStoch = input(64, "Stochastic", group="Lengths", inline="Lengths", minval=1)


lowerBand   = input(20, "Lower", group="Band", inline="Band", maxval=50, minval=0)
upperBand   = input(80, "Upper", group="Band", inline="Band", minval=50, maxval=100)

lowerReversal   = input(20, "Lower", group="Reversal", inline="Reversal", maxval=100, minval=0, tooltip="The level that if broken (down) signfies a reversal has begun/occured.\nDefault represents the lower band.")
upperReversal   = input(80, "Upper", group="Reversal", inline="Reversal", minval=0, maxval=100, tooltip="The level that if broken (up) signfies a reversal has begun/occured.\nDefault represents the upper band.")

///////////////////////////////////////////////////
// Functions
vawma(src, len) =>
    sum = 0.0
    vol = 0.0
    for m = 1 to len // m = triangular multiple
        i = len - m
        v = volume[i] * m
        vol := vol + v
        sum := sum + src[i] * v
    sum/vol
////

getMA(series, mode, len) =>
    mode==WMA ? wma(series, len) :
      mode==EMA ? ema(series, len) :
      mode==VWMA ? vwma(series, len) :
      mode==VAWMA ? vawma(series, len) :
      sma(series, len)
////

///////////////////////////////////////////////////
// Calculation
rsi1 = rsi(src, lengthRSI)
stoch = stoch(rsi1, rsi1, rsi1, lengthStoch)
k = getMA(stoch, k_mode, smoothK)
d = sma(k, smoothD)
k_c = change(k)
d_c = change(d)
kd = k - d

var hi = high
var lo = low
var phi = high
var plo = low
var state = 0

if(d<lowerBand)
    phi := high
if(d>upperBand)
    plo := low

if(high>phi)
    phi := high
if(low<plo)
    plo := low    

if(state!=-1 and d<lowerBand)
    state := -1
else if(state!=+1 and d>upperBand)
    state := +1

if(hi>phi and state==+1 and k<d and k<lowerReversal)
    hi := phi
if(lo<plo and state==-1 and k>d and k>upperReversal)
    lo := plo

if(high>hi)
    hi := high
if(low<lo)
    lo := low

ptop = plot(hi, "Resistance",color.new(color.red, 25), 3, style=plot.style_linebr)
pbot = plot(lo, "Support", color.new(color.green, 30), 3, style=plot.style_linebr)
fill(ptop, pbot, color.new(color.gray, 90), title="Range")

resistance = change(hi)
support = change(lo)

alertcondition(resistance>0 or support<0 , title="Breakout (▲▼)", message="Breakout ({{ticker}} {{interval}})")
alertcondition(resistance>0 , title="Resistance Broken ▲", message="Resistance Broken ▲ ({{ticker}} {{interval}})")
alertcondition(resistance<0 , title="Resistence Lowered (-)", message="Resistence Lowered (-) ({{ticker}} {{interval}})")
alertcondition(support<0 , title="Support Broken ▼", message="Support Broken ▼ ({{ticker}} {{interval}})")
alertcondition(support>0 , title="Support Raised (+)", message="Support Raised (+) ({{ticker}} {{interval}})")

