Absolute KRI [vnhilton]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vnhilton

//@version=5
indicator("Absolute KRI [vnhilton]", "Absolute KRI", true, timeframe="", timeframe_gaps=true)

//Main Configuration
enableCandleColors = input.bool(true, "Enable Candle Colors?", "Candle colors for when candle close & candle ohlc is X standard deviations of the distance between close & moving average, away from the moving average")

src = input(close, "Source", tooltip="This will be used in calculating the KRI")
len = input.int(20, "MA Length", 1)

maType = input.string("SMA", "MA Type", ["ALMA", "EMA", "HMA", "RMA", "SMA", "SWMA", "VWMA", "WMA"])

mult = input.float(2.0, "Std. Dev", 0.001, 50, tooltip="Standard Deviations")

kRIStdDevUpBGColor = color.rgb(0, 165, 49, 90)
kRIStdDevDownBGColor = color.rgb(255, 109, 90, 90)

//Moving Average (MA) Selection
ma(src, len, type) =>
    switch type
        "EMA" => ta.ema(src, len)
        "HMA" => ta.hma(src, len)
        "RMA" => ta.rma(src, len)
        "SMA" => ta.sma(src, len)
        "VWMA" => ta.vwma(src, len)
        "WMA" => ta.wma(src, len)

//MA Assignment
mA = ma(src, len, maType)

//Calculations
kRI = src - mA

stdDev = mult * ta.stdev(math.abs(kRI), len)

kRIUpColor = math.abs(kRI) > stdDev ? color.rgb(0, 255, 0, 0) : color.rgb(0, 165, 49, 60)
kRIDownColor =  math.abs(kRI) > stdDev ? color.rgb(255, 0, 0, 0) : color.rgb(255, 109, 90, 60)

fullyOutsideColor = (open > mA + stdDev and high > mA + stdDev and low > mA + stdDev and close > mA + stdDev) or (open < mA - stdDev and high < mA - stdDev and low < mA - stdDev and close < mA - stdDev) ? close >= open ? color.rgb(0, 0, 0, 1) : color.rgb(0, 0, 0, 0) : na
outsideColor = close > mA + stdDev or close < mA - stdDev ? close >= open ? color.rgb(0, 75, 22, 0) : color.rgb(103, 45, 37, 0) : na

//Plots
plot(stdDev, "Standard Deviation Band", color.rgb(255, 255, 255, 0))
kRILine = plot(math.abs(kRI), "Kairi Relative Index", kRI >= 0 ? kRIUpColor : kRIDownColor, 2)
base = plot(0, "Base", color.rgb(128, 128, 128, 0), style=plot.style_line)
fill(kRILine, base, kRI >= 0 ? kRIStdDevUpBGColor : kRIStdDevDownBGColor, "KRI Background Plot")

barcolor(enableCandleColors ? fullyOutsideColor : na)
barcolor(enableCandleColors ? outsideColor : na)


plotshape(kRI >= 0 and math.abs(kRI) > stdDev and math.abs(kRI[1]) <= stdDev[1], "Uptrend Outside STDDEV", shape.triangleup, location.top, color.rgb(0, 255, 0, 0), size=size.auto)
plotshape(kRI >= 0 and math.abs(kRI) <= stdDev and math.abs(kRI[1]) > stdDev[1], "Uptrend Inside STDDEV", shape.xcross, location.top, color.rgb(0, 165, 49, 60), size=size.auto)

plotshape(kRI < 0 and math.abs(kRI) > stdDev and math.abs(kRI[1]) <= stdDev[1], "Downtrend Outside STDDEV", shape.triangledown, location.top, color.rgb(255, 0, 0, 0), size=size.auto)
plotshape(kRI < 0 and math.abs(kRI) <= stdDev and math.abs(kRI[1]) > stdDev[1], "Downtrend Inside STDDEV", shape.xcross, location.top, color.rgb(255, 109, 90, 60), size=size.auto)