Variety RSI w/ Fibonacci Auto Channel [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/ Fibonacci Auto Channel [Loxx]", 
     shorttitle='VRSIFAC [Loxx]', 
     overlay = false, 
     timeframe="", 
     timeframe_gaps = true)

import loxx/loxxexpandedsourcetypes/3
import loxx/loxxvarietyrsi/1

greencolor = #2DD204
redcolor = #D2042D 
bluecolor = #042dd2

SM02 = 'Signal'
SM03 = 'Middle Crossover'
SM04 = 'Fib Level 1 Crossover'
SM05 = 'Fib Level 2 Crossover'
SM06 = 'Fib Level 3 Crossover'
SM07 = 'Fib Level 4 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")
hlper = input.int(25, "Hilo Period", group = "Basic Settings")
type = input.string("SMA", "RSI Smoothing Type", options = ["EMA", "WMA", "RMA", "SMA", "VWMA"], group = "Basic Settings")

lev1in = input.float(0.236, "Fib Level 1", group = "Fibonacci Levels Settings")
lev2in = input.float(0.382, "Fib Level 2", group = "Fibonacci Levels Settings")
lev3in = input.float(0.618, "Fib Level 3", group = "Fibonacci Levels Settings")
lev4in = input.float(0.764, "Fib Level 4", group = "Fibonacci Levels Settings")

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

colorbars = input.bool(false, "Color bars?", group = "UI Options")
showsignals = input.bool(false, "Show signals?", group = "UI Options")
fillgradient = input.bool(false, "Show gradient fill?", 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])

ll = ta.lowest(rsi, hlper)
hh = ta.highest(rsi, hlper)
rng = hh-ll

trdUp  = hh
trdDn  = ll
mid  = (hh+ll)/2

fupd = mid 
fupu = trdUp 
fdnu = mid 
fdnd = trdDn 

lev1 = ll + lev1in * rng
lev2 = ll + lev2in * rng
lev3 = ll + lev3in * rng
lev4 = ll + lev4in * rng

rsic = 0.
trdUpc = 0.
trdDnc = 0.

rsic := (rsi == hh and hh != ll) ? 1 : (rsi == ll and hh != ll) ? 2 : nz(rsic[1])
trdUpc := (trdUp > nz(trdUp[1])) ? 1 : (trdUp< nz(trdUp[1])) ? 2 : nz(trdUpc[1]) 
trdDnc := (trdDn > nz(trdDn[1])) ? 1 : (trdDn< nz(trdDn[1])) ? 2 : nz(trdDnc[1])

state = 0.
if sigtype == SM02
    if (rsi < sig) 
        state :=-1
    if (rsi > sig) 
        state := 1
else if sigtype == SM03
    if (rsi < mid) 
        state :=-1
    if (rsi  > mid)
        state := 1
else if sigtype == SM04
    if (rsi < lev1) 
        state :=-1
    if (rsi  > lev1)
        state := 1
else if sigtype == SM05
    if (rsi < lev2) 
        state :=-1
    if (rsi  > lev2)
        state := 1
else if sigtype == SM06
    if (rsi < lev3) 
        state :=-1
    if (rsi  > lev3)
        state := 1
else if sigtype == SM07
    if (rsi < lev4) 
        state :=-1
    if (rsi  > lev4)
        state := 1

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

lvllo = plot(lev1, color =  bar_index % 2 ? greencolor : na)
plot(lev2, color =  bar_index % 2 ? color.yellow : na)
plot(lev3, color =  bar_index % 2 ? color.orange : na)
lvlhi =plot(lev4, color =  bar_index % 2 ? redcolor : na)
plot(rsi, linewidth = 3, color = colorout)

goLong = 
     sigtype == SM02 ? ta.crossover(rsi, sig) : 
     sigtype == SM03 ? ta.crossover(rsi, mid) : 
     sigtype == SM04 ? ta.crossover(rsi, lev1) : 
     sigtype == SM05 ? ta.crossover(rsi, lev2) : 
     sigtype == SM06 ? ta.crossover(rsi, lev3) : 
     ta.crossover(rsi, lev4) 
     
goShort = 
     sigtype == SM02 ? ta.crossunder(rsi, sig) : 
     sigtype == SM03 ? ta.crossunder(rsi, mid) : 
     sigtype == SM04 ? ta.crossunder(rsi, lev1) : 
     sigtype == SM05 ? ta.crossunder(rsi, lev2) : 
     sigtype == SM06 ? ta.crossunder(rsi, lev3) : 
     ta.crossunder(rsi, lev4) 

plotshape(goLong and showsignals, title = "Long", color = color.yellow, textcolor = color.yellow, text = "L", style = shape.triangleup, location = location.bottom, size = size.tiny)
plotshape(goShort and showsignals, title = "Short", color = color.fuchsia, textcolor = color.fuchsia, text = "S", style = shape.triangledown, location = location.top, size = size.tiny)

alertcondition(goLong, title="Long", message="Variety RSI w/ Fibonacci Auto Channel [Loxx]: Long\nSymbol: {{ticker}}\nsrc: {{close}}")
alertcondition(goShort, title="Short", message="Variety RSI w/ Fibonacci Auto Channel [Loxx]: Short\nSymbol: {{ticker}}\nsrc: {{close}}")

lo = math.min(lev1, lev4)
hi = math.max(lev1, lev4)

color2 = color.from_gradient(rsi,lo, hi, redcolor, greencolor)
fill(lvllo, lvlhi, color = fillgradient ? hi != lo ? color.new(color2, 90) :  color.new(colorout, 90)  : na )
barcolor(colorbars ?  hi != lo ? color2 :  colorout: na)





