Exponential Bollinger Bands (EBB)
// 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("Exponential Bollinger Bands", "EBB", overlay = true)

import Electrified/Interpolation/1 as i

sma(source, length)=>
    var float sum = na
    sum := nz(sum[1]) + close
    if length % 1 != 0
        sma = (sum -  sum[int(length)] + (i.linear(length % 1, sum[int(length)] - sum[int(length)+1], 0, false)))/length
    else
        sma = (sum - sum[length])/length

stdev(source, length)=>
    var float sum = na
    average = sma(source, length)
    sum := nz(sum[1]) + math.pow(source - average, 2)

    if length % 1 != 0
        stdev = math.sqrt((sum -  sum[int(length)] + (i.linear(length % 1, sum[int(length)] - sum[int(length)+1], 0, false)))/(length - 1))
    else
        stdev = math.sqrt((sum - sum[length])/(length - 1))

bes(float source = close, float length = 9)=>
    alpha = 2 / (length + 1)
    var float smoothed = na
    smoothed := alpha * source + (1 - alpha) * nz(smoothed[1])

estdev(source, length)=>
    average = bes(source, length)
    alpha = 2 / (length + 1)
    var float smoothed = na
    smoothed := alpha * math.pow(source-average, 2) + (1 - alpha) * nz(smoothed[1])
    estdev = math.sqrt(smoothed)

average(source, length, select)=>
    switch select
        "SMA" => sma(source, length)
        "EMA" => bes(source, length)
        => na

standard_deviation(source, length, select)=>
    switch select
        "SMA" => stdev(source, length)
        "EMA" => estdev(source, length)
        => na

select = input.string("EMA", "Style Select", ["SMA","EMA"])
source = input.source(close, "Source")
length = input.float(20.25, "Length", 1, 2000, 0.25)
multiplier = input.float(2, "Multiplier", 0, 20, 0.25)

average = average(source, length, select)
stdev   = standard_deviation(source, length, select)

top = average + stdev * multiplier
bot = average - stdev * multiplier

plot(top)
plot(average)
plot(bot)
