Expansion/Contraction Indicator (ECI) [Angel Algo]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

//@version=5
indicator("Expansion/Contraction Indicator (ECI) [Angel Algo]", shorttitle = "ECI [Angel Algo]", overlay=false)

// User-defined input for the indicator period

Period = input.int(14, title="Period", minval=1, maxval=200)


// Calculate the range between open and close of each bar

ocRange = math.abs(close - open)


// Calculate the exponential moving average (EMA) of the range
emaRange = ta.ema(ocRange, Period)


// Calculate the ECI value

ocroValue = ocRange / emaRange


// Create and plot the threshold lines for identifying contractions/expansions

// User-defined input for the upper and lower threshold levels

upperThreshold = input(3.0, title="Upper Threshold")

lowerThreshold = input(0.5, title="Lower Threshold")


// Plot upper, lower and middle threshold levels

mL = hline(1.0, title="Middle Line", linestyle=hline.style_dashed, color=color.green)

uT = hline(upperThreshold, title="Upper Threshold", linestyle=hline.style_dotted, 
   color=color.green)

lT = hline(lowerThreshold, title="Lower Threshold", linestyle=hline.style_dotted, 
   color=color.green)


// Plot zero level

zero = hline(0.0, title="Zero Line", linestyle=hline.style_dotted, color=color.gray)


// Fill areas between the thresholds for better visualization

fill(uT, mL, color=color.rgb(41, 179, 73, 70), title="Expansion Area 1")

fill(mL, lT, color= color.rgb(41, 179, 73, 90), title="Expansion Area 2")


// Define the conditional coloring for the ECI dots

ECI_dots_color = ocroValue < lowerThreshold ? color.red : color.green


// Plot the OCRO as a line

plot(ocroValue, title="ECI", color= color.gray, linewidth=2)

plot( ocroValue, title="ECI dots", 
   color =  ECI_dots_color, 
   style = plot.style_circles, 
   linewidth = 3 )


// Alert conditions for breakouts and contractions

alertcondition(ta.crossover(ocroValue, upperThreshold), title="Expansion", 
   message="ECI is above the upper threshold, indicating an expansion.")

alertcondition(ta.crossunder(ocroValue, lowerThreshold), title="Contraction", 
   message="ECI is below the lower threshold, indicating a contraction.")
