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

source = input.source(close, "Source")
length = input.int(100, "Length", 1)
multiplier = input.float(2, "Multiplier", 0)

// Define the average() function
average() =>
    // Initialize the moving average to the first element of the source
    float average = source[0]
    // Initialize the count to 1
    int count = 1

    // Calculate the moving average using the Welford method
    for i = 1 to length
        count := count + 1
        delta = source[i] - average
        average := average + delta / count

    // Return the moving average
    average

// Define the stdev() function
stdev() =>
    // Initialize the moving average and variance to the first element of the source
    float average = source[0]
    float variance = 0.0
    // Initialize the count to 1
    int count = 1

    // Calculate the moving average and variance using the Welford method
    for i = 1 to length
        count := count + 1
        delta = source[i] - average
        average := average + delta / count
        variance := variance + delta * (source[i] - average)

    // Return the moving standard deviation
    math.sqrt(variance / (count - 1)) * multiplier

plot(average(), "Average", color.orange)
p1 = plot(average() + stdev(), "Upper")
p2 = plot(average() - stdev(), "Lower")
fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))