Adaptive Gaussian Moving Average
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LeafAlgo

//@version=5
indicator("Adaptive Gaussian Moving Average", overlay=true)

length = input.int(14, minval=1, title="Length")
adaptive = input.bool(true, title="Adaptive Parameters")
volatilityPeriod = input.int(20, minval=1, title="Volatility Period")

// Calculate Gaussian Moving Average
gma = 0.0
sumOfWeights = 0.0
sigma = adaptive ? ta.stdev(close, volatilityPeriod) : input.float(1.0, minval=0.1, title="Standard Deviation")

for i = 0 to length - 1
    weight = math.exp(-math.pow(((i - (length - 1)) / (2 * sigma)), 2) / 2)
    value = ta.highest(close, i + 1) + ta.lowest(close, i + 1)
    gma := gma + (value * weight)
    sumOfWeights := sumOfWeights + weight

gma := (gma / sumOfWeights) / 2

gmaColor = close >= gma ? color.lime : color.fuchsia

plot(gma, color=gmaColor, linewidth=2, title="Gaussian Moving Average")
barcolor(gmaColor)