Weighted Bollinger Band (+ Logarithmic)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Dicargo_Beam

//@version=5
indicator("Weighted Bollinger Band (+ Logarithmic)", overlay=true)

length = input.int(20, minval=1)
_src = input(close, title="Source")
mult = input.float(2.0, minval=0.001, maxval=50, title="Mult")

Type = input.string("Logarithmic", "Weighted BB type", options = ["Standard", "Logarithmic"])

float src = switch Type
	"Logarithmic" => math.log10(_src)
	=> _src

_basis = ta.wma(src, length)


weighted_stdev(src, length) =>
    sumOfSquareDeviations = 0.0
    norm = 0.0
    for i = 0 to length - 1
        sum = src[i] - _basis
        w = length - i
        norm := norm + w
        sumOfSquareDeviations := sumOfSquareDeviations + sum * sum * w
    stdev = math.sqrt(sumOfSquareDeviations / norm)



dev = mult * weighted_stdev(src, length)

[basis, upper, lower] = switch Type
	"Logarithmic" => [math.pow(10,_basis), math.pow(10,_basis + dev), math.pow(10,_basis - dev)]
	=> [_basis, _basis + dev, _basis - dev]


plot(basis, "Basis", color=#FF6D00)
p1 = plot(upper, "Upper", color=#29bbff)
p2 = plot(lower, "Lower", color=#29bbff)
fill(p1, p2, title = "Background", color=color.rgb(33, 124, 243, 90))



