Corrected RSI w/ Floating Levels [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("Corrected RSI w/ Floating Levels [Loxx]", 
     shorttitle='CRSIFL [Loxx]', 
     overlay = false, 
     timeframe="", 
     timeframe_gaps = true)
     
import loxx/loxxexpandedsourcetypes/3
import loxx/loxxvarietyrsi/1

greencolor = #2DD204
redcolor = #D2042D

SM02 = 'Slope'
SM03 = 'Middle Crossover'
SM04 = 'Levels Crossover'

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(32, "Period", group = "RSI Settings")
rsitype = input.string("RSX", "RSI type", options = ["RSX", "Regular", "Slow", "Rapid", "Harris", "Cuttler", "Ehlers Smoothed"], group = "RSI Settings")
crctper =  input.int(0, "Correction Period", group = "RSI Settings")

lvlper = input.int(50, "Levels Period", group = "Levels Settings") 
lvlup = input.int(90, "Level Up", group = "Levels Settings") 
lvldn = input.int(10, "Level Down", group = "Levels Settings")

sigtype = input.string(SM03, "Signal type", options = [SM02, SM03, SM04], 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"

devper = (crctper>0) ? crctper : (crctper<0) ? 0 : rsiper
lvlperout = (lvlper>1) ? lvlper : rsiper

val = loxxvarietyrsi.rsiVariety(rsimode, src, rsiper)
cor = 0.

v1 = math.pow(ta.stdev(val, devper), 2)
v2 = math.pow(nz(cor[1]) - val, 2) 
c  = (v2 < v1 or v2 == 0) ? 0 : 1 - v1 / v2
cor :=  nz(cor[1]) + c * (val -  nz(cor[1])) 
sig = nz(cor[1])

min   = ta.lowest(cor, lvlperout)
max   = ta.highest(cor, lvlperout)
rng = max - min
levelUp = min + lvlup * rng / 100.0
levelDn = min + lvldn * rng / 100.0
levelMi = (levelUp + levelDn) * 0.5

state = 0.
if sigtype == SM02
    if (cor < sig) 
        state :=-1
    if (cor > sig) 
        state := 1
else if sigtype == SM03
    if (cor < levelMi) 
        state :=-1
    if (cor  > levelMi)
        state := 1
else if sigtype == SM04
    if (cor < levelDn) 
        state :=-1
    if (cor > levelUp)
        state := 1

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

plot(val, color = color.yellow, title= "RSI", linewidth = 1)

plot(cor, color = colorcor, title= "Corrected RSI", linewidth = 3)

plot(levelUp, "High Level", color = color.gray)
plot(levelDn, "Low Level" ,color =  color.gray )
plot(levelMi, "Middle", color =  bar_index % 2 ? color.white : na)

barcolor(colorbars ? colorcor : na)

goLong = sigtype == SM02 ? ta.crossover(cor, sig) : sigtype == SM03 ? ta.crossover(cor, levelMi) : ta.crossover(cor, levelUp)
goShort = sigtype == SM02 ? ta.crossunder(cor, sig) : sigtype == SM03 ? ta.crossunder(cor, levelMi) :  ta.crossunder(cor, levelDn)

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="Corrected RSI w/ Floating Levels [Loxx]: Long\nSymbol: {{ticker}}\nsrc: {{close}}")
alertcondition(goShort, title="Short", message="Corrected RSI w/ Floating Levels [Loxx]: Short\nSymbol: {{ticker}}\nsrc: {{close}}")



