Advanced Exponential Smoothing Indicator (AESI) [AstrideUnicorn]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AstrideUnicorn

//@version=5
indicator(title='Advanced Exponential Smoothing Indicator (AESI)', shorttitle='AESI', overlay=true)

// Input parameters
length = input.int(14, minval=1, title='Length')
alpha = input.float(0.5, minval=0, maxval=1, title='Alpha')
cloud_mode = input.bool(true, title='Cloud Mode')

// Calculate Exponential Moving Average (EMA)
ema = ta.ema(close, length)

// Calculate the total price-ema difference
sad = math.sum(close - ema, length) 

// Calculate the Smoothing Factor
smoothing_factor = alpha * (sad / length)

// Calculate AESI
aesi =ema + smoothing_factor

// Plotting AESI
plot(aesi, color = not cloud_mode? color.new(color.green, 0): color.new(color.green, 100) , title='AESI')


// Plot AESI Cloud
cloud_color = aesi > ema ? color.green : color.red
cloud_color := cloud_mode ? cloud_color : color.new(color.black,100) 
c1 = plot(ema, color = cloud_color)
c2 = plot(aesi, color = cloud_color)
fill(c1,c2, color = cloud_color)
