Concentration of CAA Totalized (CCAAT)
// 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("Concentration of CAA Totalized", "CCAAT")

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 /////
/////////////////

Reach_Level = 0

if KPAM >= 0 and KPAM < 1
    Reach_Level := 1
if KPAM >= 1 and KPAM < 2
    Reach_Level := 2
if KPAM >= 2 and KPAM < 3
    Reach_Level := 3
if KPAM >= 3 and KPAM < 4
    Reach_Level := 4
if KPAM >= 4
    Reach_Level := 5

if KPAM <   0 and KPAM > -1
    Reach_Level := -1
if KPAM <= -1 and KPAM > -2
    Reach_Level := -2
if KPAM <= -2 and KPAM > -3
    Reach_Level := -3
if KPAM <= -3 and KPAM > -4
    Reach_Level := -4
if KPAM <= -4
    Reach_Level := -5

//////////////////
///// Energy /////
//////////////////

Energy_Level = 0

if CCI >=  0 and CCI <  1
    Energy_Level := 1
if CCI >=  1 and CCI <  2
    Energy_Level := 2
if CCI >=  2 and CCI <  3
    Energy_Level := 3
if CCI >=  3 and CCI <  4
    Energy_Level := 4
if CCI >=  4
    Energy_Level := 5

if CCI <   0 and CCI > -1
    Energy_Level := -1
if CCI <= -1 and CCI > -2
    Energy_Level := -2
if CCI <= -2 and CCI > -3
    Energy_Level := -3
if CCI <= -3 and CCI > -4
    Energy_Level := -4
if CCI <= -4
    Energy_Level := -5

/////////////////
///// Basis /////
/////////////////

Basis_Level = 0

if RSI >= 50 and RSI < 55
    Basis_Level := 1
if RSI >= 55 and RSI < 60
    Basis_Level := 2
if RSI >= 60 and RSI < 65
    Basis_Level := 3
if RSI >= 65 and RSI < 70
    Basis_Level := 4
if RSI >= 70
    Basis_Level := 5

if RSI <  50 and RSI > 45
    Basis_Level := -1
if RSI <= 45 and RSI > 40
    Basis_Level := -2
if RSI <= 40 and RSI > 35
    Basis_Level := -3
if RSI <= 35 and RSI > 30
    Basis_Level := -4
if RSI <= 30
    Basis_Level := -5

/////////////////////////////////
///// Total & Concentration /////
/////////////////////////////////

Extra_Sensitive = input.bool(false, "Extra Sensitive")
Sensitivity = 10
if Extra_Sensitive
    Sensitivity := 9

Total = Reach_Level + Energy_Level + Basis_Level
color Total_Color = color.rgb(178, 235, 242)

if (Total >= 5 and Total < Sensitivity) or (Total <= -5 and Total > (Sensitivity * -1))
    Total_Color := color.rgb(38, 198, 218)

if (Total >= Sensitivity and Total < 12) or (Total <= (Sensitivity * -1) and Total > -12)
    Total_Color := color.rgb(255, 249, 196)

if Total >= 12 or Total <= -12
    Total_Color := color.red

Length = input(20, "Length")
Inner_Deviation = input.float(2.0, "Inner", step = 0.1)
Outer_Deviation = input.float(3.0, "Outer", step = 0.1)
Deviation_Base  = ta.stdev(Total, Length)

T_Line = 0 + (Deviation_Base * Outer_Deviation)
H_Line = 0 + (Deviation_Base * Inner_Deviation)
L_Line = 0 - (Deviation_Base * Inner_Deviation)
B_Line = 0 - (Deviation_Base * Outer_Deviation)

/////////////////
///// Plots /////
/////////////////

plot(Total, "Total", Total_Color, 2, plot.style_columns)

plot(T_Line,    "Top", color.orange, 2)
plot(H_Line,   "High", color.orange, 2)
plot(L_Line,    "Low", color.orange, 2, display = display.all - display.status_line)
plot(B_Line, "Bottom", color.orange, 2, display = display.all - display.status_line)

hline( 15,  "Lv 3", color.new(color.white, 65), hline.style_dotted)
hline( 10,  "Lv 2", color.new(color.white, 50), hline.style_dotted)
hline(  5,  "Lv 1", color.new(color.white, 30), hline.style_dotted)
hline(  0,     "0", color.new(color.aqua , 70), hline.style_solid, display = display.none)
hline( -5, "Lv -1", color.new(color.white, 30), hline.style_dotted)
hline(-10, "Lv -2", color.new(color.white, 50), hline.style_dotted)
hline(-15, "Lv -3", color.new(color.white, 65), hline.style_dotted)