Volatility Adjusted EMA (VAEMA) 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © peacefulLizard50262

//@version=5
indicator("Volatility Adjusted EMA", "VAEMA", overlay = true)
// function to calculate the exponential moving average
ema(src, len, a) => //{
    alpha = a / (len + 1)
    sum   = 0.0
    sum  := na(sum[1]) ? src : alpha * src + (1 - alpha) * nz(sum[1])
    
// function to calculate the EMA with a variable alpha
ema_var_alpha(data, length, a, direction) =>
    // calculate the volatility of the data
    vol = ta.stdev(data, int(length))
    // set the alpha value to be inversely proportional to the volatility
    alpha = direction ? a / vol : a * vol
    // calculate the EMA using the alpha value
    ema(data, length, alpha)

source = input.source(close, "Source")
length = input.int(50, "Length")
alpha = input.float(1, "Alpha", 0.1, step = 0.05)
direction = input.bool(false, "Invert Alpha")

plot(ema_var_alpha(source, length, alpha, direction))
