Fibonacci Bollinger Bands (FBB)
// 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("Fibonacci Moving Average", "FMA", true)
metallic_mean(source)=>
    (source+math.sqrt(math.pow(source, 2) + 4))/2

fma(source, length)=>
    float src = metallic_mean(source)
    var float weight_sum = na
    var float sum        = na
    for i = 0 to length - 1
        weight = metallic_mean(i)
        weight_sum := nz(weight_sum[1]) + weight
        sum := nz(sum[1]) + src * weight
    out = (sum - nz(sum[length]))/(weight_sum - nz(weight_sum[length]))

fstdev(source, length)=>
    math.sqrt(math.sum(math.pow(metallic_mean(source) - fma(source, length), 2), length)/(length-1))

source = input.source(close, "Source")
length = input.int(20, "Length", 2) 


up_two = input.bool(false, "High | 0.236", group = "High")
up_three = input.bool(false, "High | 0.382", group = "High")
up_four = input.bool(false, "High | 0.5", group = "High")
up_five = input.bool(false, "High | 0.618", group = "High")
up_six = input.bool(true, "High | 0.764", group = "High")
up_one = input.bool(false, "High | 1", group = "High")
up_seven = input.bool(true, "High | 1.618", group = "High")
up_eight = input.bool(true, "High | 2.618", group = "High")


down_two = input.bool(false, "Low | 0.236", group = "Low")
down_three = input.bool(false, "Low | 0.382", group = "Low")
down_four = input.bool(false, "Low | 0.5", group = "Low")
down_five = input.bool(false, "Low | 0.618", group = "Low")
down_six = input.bool(true, "Low | 0.764", group = "Low")
down_one = input.bool(false, "Low | 1", group = "Low")
down_seven = input.bool(true, "Low | 1.618", group = "Low")
down_eight = input.bool(true, "Low | 2.618", group = "Low")


avg = fma(source, length)
dev = fstdev(source, length)
up  = up_one ? avg + dev : na
up_0 = (up_two ? avg : na) + (0.236*dev)
up_1 = (up_three ? avg : na) + (0.382*dev)
up_2 = (up_four ? avg : na) + (0.5*dev)
up_3 = (up_five ? avg : na) + (0.618*dev)
up_4 = (up_six ? avg : na) + (0.786*dev)
up_5 = (up_seven ? avg : na) + (1.618*dev)
up_6 = (up_eight ? avg : na) + (2.618*dev)
dn   = down_one ? avg - dev : na
dn_0 = (down_two ? avg : na) - (0.236*dev)
dn_1 = (down_three ? avg : na) - (0.382*dev)
dn_2 = (down_four ? avg : na) - (0.5*dev)
dn_3 = (down_five ? avg : na) - (0.618*dev)
dn_4 = (down_six ? avg : na) - (0.786*dev)
dn_5 = (down_seven ? avg : na) - (1.618*dev)
dn_6 = (down_eight ? avg : na) - (2.618*dev)

plot(avg, "Center", #787b86)
plot(up, "1.0 Up", #787b86)
plot(dn, "1.0 Down", #787b86)
plot(up_0, "0.236 Up", #f44336)
plot(up_1, "0.382 Up", #81c784)
plot(up_2, "0.5 Up", #4caf50)
plot(up_3, "0.618 Up", #009688)
plot(up_4, "0.786 Up", #64b5f6)
plot(up_5, "1.618 Up", #2962ff)
plot(up_6, "2.618 Up", #f44336)
plot(dn_0, "0.236 Down", #f44336)
plot(dn_1, "0.382 Down", #81c784)
plot(dn_2, "0.5 Down", #4caf50)
plot(dn_3, "0.618 Down", #009688)
plot(dn_4, "0.786 Down", #64b5f6)
plot(dn_5, "1.618 Down", #2962ff)
plot(dn_6, "2.618 Down", #f44336)