Color Agreement Aggregate (CAA)
// 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 = "Color Agreement Aggregate", shorttitle = "CAA", precision = 3)

G1 = input.color(color.rgb(219, 250, 210), "", inline = "Higher Colors")
G2 = input.color(color.rgb(140, 228, 180), "", inline = "Higher Colors")
G3 = input.color(color.rgb(129, 199, 132), "", inline = "Higher Colors")
G4 = input.color(color.rgb( 56, 142,  60), "", inline = "Higher Colors")
G5 = input.color(color.rgb(  5, 102,  86), "", inline = "Higher Colors")

R1 = input.color(color.rgb(255, 231, 217), "", inline = "Lower Colors")
R2 = input.color(color.rgb(252, 192, 195), "", inline = "Lower Colors")
R3 = input.color(color.rgb(247, 124, 128), "", inline = "Lower Colors")
R4 = input.color(color.rgb(194,  24,  91), "", inline = "Lower Colors")
R5 = input.color(color.rgb(128,  25,  34), "", inline = "Lower Colors")

Inverse_Colors   = input.bool(false, "Inverse Colors", "Toggle on to perceive the levels as buy/sell pressure. Toggle off to perceive the levels as momentum.")
Background_On    = input.bool(false, "Background", inline = "Background")
Background_Color = input.color(color.white, "", inline = "Background")

float KPAM = 0.0
float CCI  = ta.cci(hlc3, 20) * 0.01
float RSI  = ta.rsi(close, 14)

Rsi = RSI * 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)

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)

///// Reach /////
R_Grow = KPAM >  KPAM[1]
R_Stay = KPAM == KPAM[1]

R_Up1 = KPAM >=  0 and KPAM <  1
R_Up2 = KPAM >=  1 and KPAM <  2
R_Up3 = KPAM >=  2 and KPAM <  3
R_Up4 = KPAM >=  3 and KPAM <  4
R_Up5 = KPAM >=  4

R_Dn1 = KPAM <   0 and KPAM > -1
R_Dn2 = KPAM <= -1 and KPAM > -2
R_Dn3 = KPAM <= -2 and KPAM > -3
R_Dn4 = KPAM <= -3 and KPAM > -4
R_Dn5 = KPAM <= -4

Precise_Reach =
 KPAM >=  0 and KPAM <  0.5 or
 KPAM >=  1 and KPAM <  1.5 or
 KPAM >=  2 and KPAM <  2.5 or
 KPAM >=  3 and KPAM <  3.5 or
 KPAM >=  4 and KPAM <  4.5 or

 KPAM <   0 and KPAM > -0.5 or
 KPAM <= -1 and KPAM > -1.5 or
 KPAM <= -2 and KPAM > -2.5 or
 KPAM <= -3 and KPAM > -3.5 or
 KPAM <= -4 and KPAM > -4.5

///// Energy /////
E_Grow = CCI >  CCI[1]
E_Stay = CCI == CCI[1]

E_Up1 = CCI >=  0 and CCI <  1
E_Up2 = CCI >=  1 and CCI <  2
E_Up3 = CCI >=  2 and CCI <  3
E_Up4 = CCI >=  3 and CCI <  4
E_Up5 = CCI >=  4

E_Dn1 = CCI <   0 and CCI > -1
E_Dn2 = CCI <= -1 and CCI > -2
E_Dn3 = CCI <= -2 and CCI > -3
E_Dn4 = CCI <= -3 and CCI > -4
E_Dn5 = CCI <= -4

Precise_Energy =
 CCI >=  0 and CCI <  0.5 or
 CCI >=  1 and CCI <  1.5 or
 CCI >=  2 and CCI <  2.5 or
 CCI >=  3 and CCI <  3.5 or
 CCI >=  4 and CCI <  4.5 or

 CCI <   0 and CCI > -0.5 or
 CCI <= -1 and CCI > -1.5 or
 CCI <= -2 and CCI > -2.5 or
 CCI <= -3 and CCI > -3.5 or
 CCI <= -4 and CCI > -4.5

///// Basis /////
B_Grow = RSI >  RSI[1]
B_Stay = RSI == RSI[1]

B_Up1 = RSI >= 50 and RSI < 55
B_Up2 = RSI >= 55 and RSI < 60
B_Up3 = RSI >= 60 and RSI < 65
B_Up4 = RSI >= 65 and RSI < 70
B_Up5 = RSI >= 70

B_Dn1 = RSI <  50 and RSI > 45
B_Dn2 = RSI <= 45 and RSI > 40
B_Dn3 = RSI <= 40 and RSI > 35
B_Dn4 = RSI <= 35 and RSI > 30
B_Dn5 = RSI <= 30

Precise_Basis =
 RSI >= 50 and RSI < 52.5 or
 RSI >= 55 and RSI < 57.5 or
 RSI >= 60 and RSI < 62.5 or
 RSI >= 65 and RSI < 67.5 or
 RSI >= 70 and RSI < 72.5 or

 RSI <  50 and RSI > 47.5 or
 RSI <= 45 and RSI > 42.5 or
 RSI <= 40 and RSI > 37.5 or
 RSI <= 35 and RSI > 32.5 or
 RSI <= 30 and RSI > 27.5

/////// Plots & Fills ///////
plot(Background_On == false ? na : 6.7, "Background", Background_Color, style = plot.style_area, offset = 1, editable = false)
hline(0, color = color.new(color.black, 100), editable = false) // Base line to add some spacing on the bottom for the up/down of Basis (see last line)

Reach_Label  = label.new(bar_index + 5, 5.5, "← Reach (fast)",    color = color.blue, style = label.style_label_left, textcolor = color.white, size = size.small)
Energy_Label = label.new(bar_index + 5, 3.5, "← Energy (medium)", color = color.blue, style = label.style_label_left, textcolor = color.white, size = size.small)
Basis_Label  = label.new(bar_index + 5, 1.5, "← Basis (slow)",    color = color.blue, style = label.style_label_left, textcolor = color.white, size = size.small)

label.delete(Reach_Label[1])
label.delete(Energy_Label[1])
label.delete(Basis_Label[1])

///// Reach /////
plot(6.0, "Reach: Red Highs",
 color = R_Up1 ? R1 : R_Up2 ? R2 : R_Up3 ? R3 : R_Up4 ? R4 : R_Up5 ? R5 : R_Dn1 ? G1 : R_Dn2 ? G2 : R_Dn3 ? G3 : R_Dn4 ? G4 : R_Dn5 ? G5 : na,
 style = plot.style_columns,
 histbase = 5.0,
 editable = false,
 display  = Inverse_Colors ? display.all : display.none)

plot(6.0, "Reach: Green Highs",
 color = R_Up1 ? G1 : R_Up2 ? G2 : R_Up3 ? G3 : R_Up4 ? G4 : R_Up5 ? G5 : R_Dn1 ? R1 : R_Dn2 ? R2 : R_Dn3 ? R3 : R_Dn4 ? R4 : R_Dn5 ? R5 : na,
 style = plot.style_columns,
 histbase = 5.0,
 editable = false,
 display  = Inverse_Colors ? display.none : display.all)

plot(Precise_Reach ? 5.5 : na, "Precise Reach", color = color.white, style = plot.style_columns, histbase = 6.0, display = display.none)
plot(4.8, "Reach Up/Down", R_Grow ? color.lime : R_Stay ? color.white : color.red, linewidth = 3, style = plot.style_histogram, histbase = 4.7)

///// Energy /////
plot(4.0, "Energy: Red Highs",
  color = E_Up1 ? R1 : E_Up2 ? R2 : E_Up3 ? R3 : E_Up4 ? R4 : E_Up5 ? R5 : E_Dn1 ? G1 : E_Dn2 ? G2 : E_Dn3 ? G3 : E_Dn4 ? G4 : E_Dn5 ? G5 : na,
  style = plot.style_columns,
  histbase = 3.0,
  editable = false,
  display  = Inverse_Colors ? display.all : display.none)

plot(4.0, "Energy: Green Highs",
  color = E_Up1 ? G1 : E_Up2 ? G2 : E_Up3 ? G3 : E_Up4 ? G4 : E_Up5 ? G5 : E_Dn1 ? R1 : E_Dn2 ? R2 : E_Dn3 ? R3 : E_Dn4 ? R4 : E_Dn5 ? R5 : na,
  style = plot.style_columns,
  histbase = 3.0,
  editable = false,
  display  = Inverse_Colors ? display.none : display.all)

plot(Precise_Energy ? 3.5 : na, "Precise Energy", color = color.white, style = plot.style_columns, histbase = 4.0, display = display.none)
plot(2.8, "Energy Up/Down", E_Grow ? color.lime : E_Stay ? color.white : color.red, linewidth = 3, style = plot.style_histogram, histbase = 2.7)

///// Basis /////
plot(2.0, "Basis: Red Highs",
  color = B_Up1 ? R1 : B_Up2 ? R2 : B_Up3 ? R3 : B_Up4 ? R4 : B_Up5 ? R5 : B_Dn1 ? G1 : B_Dn2 ? G2 : B_Dn3 ? G3 : B_Dn4 ? G4 : B_Dn5 ? G5 : na,
  style = plot.style_columns,
  histbase = 1.0,
  editable = false,
  display  = Inverse_Colors ? display.all : display.none)

plot(2.0, "Basis: Green Highs",
  color = B_Up1 ? G1 : B_Up2 ? G2 : B_Up3 ? G3 : B_Up4 ? G4 : B_Up5 ? G5 : B_Dn1 ? R1 : B_Dn2 ? R2 : B_Dn3 ? R3 : B_Dn4 ? R4 : B_Dn5 ? R5 : na,
  style = plot.style_columns,
  histbase = 1.0,
  editable = false,
  display  = Inverse_Colors ? display.none : display.all)

plot(Precise_Basis ? 1.5 : na, "Precise Basis", color = color.white, style = plot.style_columns, histbase = 2.0, display = display.none)
plot(0.8, "Basis Up/Down", B_Grow ? color.lime : B_Stay ? color.white : color.red, linewidth = 3, style = plot.style_histogram, histbase = 0.7)