Variety RSI w/ Dynamic Zones [Loxx]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © loxx

//@version=5
indicator("Variety RSI w/ Dynamic Zones [Loxx]", 
     shorttitle='VRSIDZ [Loxx]', 
     overlay = false, 
     timeframe="", 
     timeframe_gaps = true)

import loxx/loxxexpandedsourcetypes/4
import loxx/loxxvarietyrsi/1
import loxx/loxxdynamiczone/3

greencolor = #2DD204
redcolor = #D2042D 

lightgreencolor = #96E881 
lightredcolor =  #DF4F6C
lightbluecolor =  #4f6cdf

SM02 = 'Slope'
SM03 = 'Middle Crossover'
SM04 = 'Inner Crossover'
SM05 = 'Outer Crossover'

variant(type, src, len) =>
    sig = 0.0
    if type == "SMA"
        sig := ta.sma(src, len) 
    else if type == "EMA"
        sig := ta.ema(src, len) 
    else if type == "WMA"
        sig := ta.wma(src, len)   
    else if type == "RMA"
        sig := ta.rma(src, len)  
    else if type == "VWMA"
        sig := ta.rma(src, len)  
    sig

smthtype = input.string("Kaufman", "Heikin-Ashi Better Caculation Type", options = ["AMA", "T3", "Kaufman"], group = "Basic Settings")

srcin = input.string("Close", "Source", group= "Basic Settings", 
     options = 
     ["Close", "Open", "High", "Low", "Median", "Typical", "Weighted", "Average", "Average Median Body", "Trend Biased", "Trend Biased (Extreme)", 
     "HA Close", "HA Open", "HA High", "HA Low", "HA Median", "HA Typical", "HA Weighted", "HA Average", "HA Average Median Body", "HA Trend Biased", "HA Trend Biased (Extreme)",
     "HAB Close", "HAB Open", "HAB High", "HAB Low", "HAB Median", "HAB Typical", "HAB Weighted", "HAB Average", "HAB Average Median Body", "HAB Trend Biased", "HAB Trend Biased (Extreme)"])

rsiper = input.int(15, "Period", group = "Basic Settings")
rsitype = input.string("Regular", "RSI type", options = ["RSX", "Regular", "Slow", "Rapid", "Harris", "Cuttler", "Ehlers Smoothed"], group = "Basic Settings")
smthper = input.int(15, "Smooth Period", group = "Basic Settings")
type = input.string("SMA", "RSI Smoothing Type", options = ["EMA", "WMA", "RMA", "SMA", "VWMA"], group = "Basic Settings")

dzper = input.int(70, "Dynamic Zone Period", group = "Levels Settings") 
buy1  = input.float(0.2, "Dynamic Zone Buy Probability Level 1", group = "Levels Settings", maxval = 0.49, step = 0.01)
buy2  = input.float(0.06, "Dynamic Zone Buy Probability Level 2", group = "Levels Settings", maxval = 0.49, step = 0.01)
sell1 = input.float(0.2, "Dynamic Zone Sell Probability Level 1", group = "Levels Settings", maxval = 0.49, step = 0.01)
sell2 = input.float(0.06, "Dynamic Zone Sell Probability Level 2", group = "Levels Settings", maxval = 0.49, step = 0.01)

sigtype = input.string(SM03, "Signal type", options = [SM02, SM03, SM04, SM05], group = "Signal Settings")

colorbars = input.bool(false, "Color bars?", group = "UI Options")
showsignals = input.bool(false, "Show signals?", group = "UI Options")


kfl=input.float(0.666, title="* Kaufman's Adaptive MA (KAMA) Only - Fast End", group = "Moving Average Inputs")
ksl=input.float(0.0645, title="* Kaufman's Adaptive MA (KAMA) Only - Slow End", group = "Moving Average Inputs")
amafl = input.int(2, title="* Adaptive Moving Average (AMA) Only - Fast", group = "Moving Average Inputs")
amasl = input.int(30, title="* Adaptive Moving Average (AMA) Only - Slow", group = "Moving Average Inputs")

haclose = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close)
haopen = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, open)
hahigh = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, high)
halow = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, low)
hamedian = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hl2)
hatypical = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hlc3)
haweighted = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, hlcc4)
haaverage = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, ohlc4)

src = switch srcin
	"Close" => loxxexpandedsourcetypes.rclose()
	"Open" => loxxexpandedsourcetypes.ropen()
	"High" => loxxexpandedsourcetypes.rhigh()
	"Low" => loxxexpandedsourcetypes.rlow()
	"Median" => loxxexpandedsourcetypes.rmedian()
	"Typical" => loxxexpandedsourcetypes.rtypical()
	"Weighted" => loxxexpandedsourcetypes.rweighted()
	"Average" => loxxexpandedsourcetypes.raverage()
    "Average Median Body" => loxxexpandedsourcetypes.ravemedbody()
	"Trend Biased" => loxxexpandedsourcetypes.rtrendb()
	"Trend Biased (Extreme)" => loxxexpandedsourcetypes.rtrendbext()
	"HA Close" => loxxexpandedsourcetypes.haclose(haclose)
	"HA Open" => loxxexpandedsourcetypes.haopen(haopen)
	"HA High" => loxxexpandedsourcetypes.hahigh(hahigh)
	"HA Low" => loxxexpandedsourcetypes.halow(halow)
	"HA Median" => loxxexpandedsourcetypes.hamedian(hamedian)
	"HA Typical" => loxxexpandedsourcetypes.hatypical(hatypical)
	"HA Weighted" => loxxexpandedsourcetypes.haweighted(haweighted)
	"HA Average" => loxxexpandedsourcetypes.haaverage(haaverage)
    "HA Average Median Body" => loxxexpandedsourcetypes.haavemedbody(haclose, haopen)
	"HA Trend Biased" => loxxexpandedsourcetypes.hatrendb(haclose, haopen, hahigh, halow)
	"HA Trend Biased (Extreme)" => loxxexpandedsourcetypes.hatrendbext(haclose, haopen, hahigh, halow)
	"HAB Close" => loxxexpandedsourcetypes.habclose(smthtype, amafl, amasl, kfl, ksl)
	"HAB Open" => loxxexpandedsourcetypes.habopen(smthtype, amafl, amasl, kfl, ksl)
	"HAB High" => loxxexpandedsourcetypes.habhigh(smthtype, amafl, amasl, kfl, ksl)
	"HAB Low" => loxxexpandedsourcetypes.hablow(smthtype, amafl, amasl, kfl, ksl)
	"HAB Median" => loxxexpandedsourcetypes.habmedian(smthtype, amafl, amasl, kfl, ksl)
	"HAB Typical" => loxxexpandedsourcetypes.habtypical(smthtype, amafl, amasl, kfl, ksl)
	"HAB Weighted" => loxxexpandedsourcetypes.habweighted(smthtype, amafl, amasl, kfl, ksl)
	"HAB Average" => loxxexpandedsourcetypes.habaverage(smthtype, amafl, amasl, kfl, ksl)
    "HAB Average Median Body" => loxxexpandedsourcetypes.habavemedbody(smthtype, amafl, amasl, kfl, ksl)
	"HAB Trend Biased" => loxxexpandedsourcetypes.habtrendb(smthtype, amafl, amasl, kfl, ksl)
	"HAB Trend Biased (Extreme)" => loxxexpandedsourcetypes.habtrendbext(smthtype, amafl, amasl, kfl, ksl)
	=> haclose
	
rsimode = switch rsitype
	"RSX" => "rsi_rsx"
	"Regular" => "rsi_rsi"
	"Slow" => "rsi_slo"
	"Rapid" => "rsi_rap"
	"Harris" => "rsi_har"
	"Cuttler" => "rsi_cut"
	"Ehlers Smoothed" => "rsi_ehl"
	=> "rsi_rsi"


srcout = variant(type, src, smthper)
rsi = loxxvarietyrsi.rsiVariety(rsimode, srcout, rsiper)
sig = nz(rsi[1])

bl1 = loxxdynamiczone.dZone("buy", rsi, buy1,  dzper)
bl2 = loxxdynamiczone.dZone("buy", rsi, buy2,  dzper)

sl1 = loxxdynamiczone.dZone("sell", rsi, sell1, dzper)
sl2 = loxxdynamiczone.dZone("sell", rsi, sell2, dzper)

zli = loxxdynamiczone.dZone("sell", rsi, 0.5 , dzper)

state = 0.

if sigtype == SM02
    if (rsi < sig) 
        state :=-1
    if (rsi > sig) 
        state := 1
else if sigtype == SM03
    if (rsi < zli) 
        state :=-1
    if (rsi > zli)
        state := 1
else if sigtype == SM04
    if (rsi < bl1) 
        state :=-1
    if (rsi > sl1)
        state := 1
else if sigtype == SM05
    if (rsi < bl2) 
        state :=-1
    if (rsi > sl2)
        state := 1

colorout = state == -1 ? redcolor : state == 1 ? greencolor : color.gray

plot(rsi, "RSI", color = colorout, linewidth = 3)
plot(bl1, "buy lvl 1", color = lightgreencolor)
plot(bl2, "buy lvl 2", color = greencolor)
plot(sl1, "sell lvl 1", color = lightredcolor)
plot(sl2, "sell lvl 1", color = redcolor)
plot(zli, "mid", color = color.silver)

barcolor(colorbars ? colorout : na)

goLong = colorout == greencolor and colorout[1] != greencolor
goShort = colorout == redcolor and colorout[1] != redcolor
plotshape(goLong and showsignals, title = "Long", color = greencolor, textcolor = greencolor, text = "L", style = shape.triangleup, location = location.bottom, size = size.auto)
plotshape(goShort and showsignals, title = "Short", color = redcolor, textcolor = redcolor, text = "S", style = shape.triangledown, location = location.top, size = size.auto)

alertcondition(goLong, title="Long", message="Variety RSI w/ Dynamic Zones [Loxx]: Long\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(goShort, title="Short", message="Variety RSI w/ Dynamic Zones [Loxx]: Short\nSymbol: {{ticker}}\nPrice: {{close}}")

hline(-10, color = color.new(color.white, 100), editable = false) // to force signal to not be covered
hline(110, color = color.new(color.white, 100), editable = false) // to force signal to not be covered



