Key Points of Adjoining Median (KPAM)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © More-Than-Enough

//@version=5
indicator(title = "Key Points of Adjoining Median", shorttitle = "KPAM", precision = 3)

RSI = ta.rsi(close, 14) * 0.1 - 5
float Stoch = ta.sma(ta.stoch(close, high, low, 14) * 0.1 - 5, 1)
float Stoch_RSI = ta.sma(ta.stoch(RSI, RSI, RSI, 14) * 0.1 - 5, 3)
float KPAM = 0.0

if Stoch_RSI >= 0 and Stoch >= 0 and Stoch_RSI >= Stoch
    KPAM := Stoch_RSI - ((Stoch_RSI - Stoch) / 2)

if Stoch_RSI >= 0 and Stoch >= 0 and Stoch_RSI < Stoch
    KPAM := Stoch_RSI + ((Stoch - Stoch_RSI) / 2)

if Stoch_RSI < 0 and Stoch < 0 and Stoch_RSI >= Stoch
    KPAM := Stoch_RSI + ((Stoch - Stoch_RSI) / 2)

if Stoch_RSI < 0 and Stoch < 0 and Stoch_RSI < Stoch
    KPAM := Stoch_RSI - ((Stoch_RSI - Stoch) / 2)

if Stoch_RSI >= 0 and Stoch < 0
    KPAM := Stoch_RSI - ((Stoch_RSI - Stoch) / 2)

if Stoch_RSI < 0 and Stoch >= 0
    KPAM := Stoch_RSI + ((Stoch - Stoch_RSI) / 2)

Smoothing_Amount = input(5, "KPAM Smoothing")
Smooth_KPAM = ta.ema(KPAM, Smoothing_Amount)

/////////////// Thresholds ///////////////
SOLID = "Solid"
DASHED = "Dashed"
DOTTED =  "Dotted"

Top_Color = input.color(color.rgb(255, 245, 157, 50), "", inline = "Top Line", group = "Top Line")
Top_Style = input.string(DOTTED, "", [SOLID, DASHED, DOTTED], inline = "Top Line", group = "Top Line")
Top_Width = input.int(1, "", [1, 2, 3, 4], inline = "Top Line", group = "Top Line")
Top = input.float(4.0, "", minval = 0.0, step = 0.1, inline = "Top Line", group = "Top Line")

High_Color = input.color(color.rgb(187, 217, 251, 30), "", inline = "High Line", group = "High Line")
High_Style = input.string(DOTTED, "", [SOLID, DASHED, DOTTED], inline = "High Line", group = "High Line")
High_Width = input.int(1, "", [1, 2, 3, 4], inline = "High Line", group = "High Line")

Mid = 0
Mid_Color = input.color(color.rgb(255, 245, 157, 70), "", inline = "Middle Line", group = "Middle Line")
Mid_Style = input.string(SOLID, "", [SOLID, DASHED, DOTTED], inline = "Middle Line", group = "Middle Line")
Mid_Width = input.int(1, "", [1, 2, 3, 4], inline = "Middle Line", group = "Middle Line")

Low_Color = input.color(color.rgb(187, 217, 251, 30), "", inline = "Low Line", group = "Low Line")
Low_Style = input.string(DOTTED, "", [SOLID, DASHED, DOTTED], inline = "Low Line", group = "Low Line")
Low_Width = input.int(1, "", [1, 2, 3, 4], inline = "Low Line", group = "Low Line")

Bottom_Color = input.color(color.rgb(255, 245, 157, 50), "", inline = "Bottom Line", group = "Bottom Line")
Bottom_Style = input.string(DOTTED, "", [SOLID, DASHED, DOTTED], inline = "Bottom Line", group = "Bottom Line")
Bottom_Width = input.int(1, "", [1, 2, 3, 4], inline = "Bottom Line", group = "Bottom Line")
Bottom = input.float(-4.0, "", maxval = 0.0, step = 0.1, inline = "Bottom Line", group = "Bottom Line")

Set_Style(style) =>
    Output = switch style
        SOLID  => hline.style_solid 
        DASHED => hline.style_dashed
        DOTTED => hline.style_dotted

Set_Width(width) =>
    Output = switch width
        1 => 1
        2 => 2
        3 => 3
        4 => 4

/////////////// Plots ///////////////
Top_Line = hline(Top, "Top Line", Top_Color, Set_Style(Top_Style), Set_Width(Top_Width), false)
High_Line = hline(Mid + ((math.abs(Top) - math.abs(Mid)) / 2), "High Line", High_Color, Set_Style(High_Style), Set_Width(High_Width), false)
Mid_Line = hline(Mid, "Middle Line", Mid_Color, Set_Style(Mid_Style), Set_Width(Mid_Width), false)
Low_Line = hline(Mid - ((math.abs(Mid) + math.abs(Bottom)) / 2), "Low Line", Low_Color, Set_Style(Low_Style), Set_Width(Low_Width), false)
Bottom_Line = hline(Bottom, "Bottom Line", Bottom_Color, Set_Style(Bottom_Style), Set_Width(Bottom_Width), false)

fill(High_Line, Top_Line, color.new(color.aqua, 85), "Top Zone")
fill(Mid_Line, High_Line, color.rgb(33, 150, 243, 90), "High Zone")
fill(Mid_Line, Low_Line, color.rgb(33, 150, 243, 90), "Low Zone")
fill(Low_Line, Bottom_Line, color.new(color.aqua, 85), "Bottom Zone")

plot(RSI, "RSI", color = color.rgb(206, 147, 216), linewidth = 2)
S = plot(Stoch, "Stoch", color = color.aqua, display = display.none)
SR = plot(Stoch_RSI, "Stoch RSI", color = color.orange, display = display.none)
plot(KPAM, "KPAM", color.rgb(255, 249, 196))
plot(Smooth_KPAM, "Smooth KPAM", color.white, 2, display = display.none)
fill(S, SR, color.rgb(255, 249, 196, 75), title = "Discordance")