Logarithmic Bollinger Bands [MisterMoTA]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @author  = MisterMoTA
// © MisterMoTA

//@version=5
indicator(title="Logarithmic Bollinger Bands [MisterMoTA]", shorttitle = "LBBMO", overlay=true)

hideLabels = input.bool(false, title="Hide Labels", group = "Indicator Settings")

styleOption = input.string("small", title="Label Size",
     options=["normal", "large", 
     "small"], group = "Indicator Settings")

labelSize = styleOption == "normal" ? size.normal : styleOption == "large" ? size.large :  styleOption == "small" ? size.small :  size.normal

std = input.float(2, "Standard deviation", group = "Indicator Settings")
src = input.source(close,"Source", group = "Indicator Settings")

decimals = input.int(2, "Decimals for price displayed in Labels", maxval = 8, group = "Indicator Settings")
labelDistance = input.int(10, "Labels Distance From Price", group = "Indicator Settings")

mainPivotColor = input.color(#3288bd, "SMA 20 Color",  group = "Indicator Settings")

line0382Color = input.color(#66c2a5, "0.382 Color", group = "Indicator Settings")
line0618Color = input.color(#abdda4, "0.618 Color", group = "Indicator Settings")
lineT1Color = input.color(#e6f598, " MAIN BB BANDS Color", group = "Indicator Settings")
lineee0382Color = input.color(#fee08b, "0.382 Expansion Color",  group = "Indicator Settings")
lineee0618Color = input.color(#fdae61, "0.618 Expansion Color", group = "Indicator Settings")
lineee1Color = input.color(#f46d43, "1 Expansion Color", group = "Indicator Settings")
lineee1618Color = input.color(#d53e4f, "1.618 Color", group = "Indicator Settings")

var string decs =na
if decimals == 0
    decs := "#"
else if decimals == 1
    decs :="#.#"
else if decimals == 2
    decs := "#.##"
else if decimals == 3
    decs :="#.###"
else if decimals == 4
    decs :="#.####"
else if decimals == 5
    decs :="#.#####"
else if decimals == 6
    decs :="#.######"
else if decimals == 7
    decs :="#.#######"
else if decimals == 8
    decs :="#.########"


var lm = label (na)
var l1 = label (na)
var l2 = label (na)
var l3 = label (na)
var l4 = label (na)
var l5 = label (na)
var l6 = label (na)
var l7 = label (na)


var r11 = label (na)
var r22 = label (na)
var r33 = label (na)
var r44 = label (na)
var r55 = label (na)
var r66 = label (na)
var r77 = label (na)

float p0382 = na 
float p0618 = na 
float Top1 = na 
float pp0382 = na 
float pp0618 = na 
float pp1 = na 
float pp1618 = na 

float m0382 = na
float m0618 = na
float Bottom1 = na
float mm0382 = na
float mm0618 = na
float mm1 = na
float mm1618 = na

smma = ta.sma(src, 20)

dev = std * ta.stdev(src, 20)

upper = smma + dev
lower = smma - dev

p0382 := smma * math.pow(smma/lower, 0.382)
p0618 := smma * math.pow(smma/lower, 0.618)
Top1 :=upper
pp0382 := upper * math.pow(upper/smma, 0.382)
pp0618 := upper * math.pow(upper/smma, 0.618)
pp1 :=  upper * math.pow(upper/smma, 1)
pp1618  :=  upper * math.pow(upper/smma, 1.618)

m0382 := smma * math.pow(smma/upper, 0.382)
m0618 := smma * math.pow(smma/upper, 0.618)
Bottom1 := lower
mm0382 := lower * math.pow(lower/smma, 0.382)
mm0618 := lower * math.pow(lower/smma, 0.618)
mm1 := lower * math.pow(lower/smma, 1)
mm1618 := lower * math.pow(lower/smma, 1.618)

plot( smma, color = mainPivotColor, linewidth = 2, style = plot.style_line, title="SMA 20", display = display.pane)

plot( p0382, color = line0382Color, linewidth = 1, style = plot.style_line , title="+ 0.382 Pivot", display = display.pane)
plot( p0618, color = line0618Color, linewidth = 1, style = plot.style_line, title="+ 0.618 Pivot", display = display.pane)
topBB = plot( Top1, color = lineT1Color, linewidth = 2, style = plot.style_line, title="Main Top Bollinger Band", display = display.pane)
plot( pp0382, color = lineee0382Color, linewidth = 1, style = plot.style_line, title="+ 0.382 Expansion Pivot", display = display.pane)
plot( pp0618, color = lineee0618Color, linewidth = 1, style = plot.style_line, title="+ 0.618 Expansion Pivot", display = display.pane)
plot( pp1, color = lineee1Color, linewidth = 1, style = plot.style_line, title="+ 1 Expansion Pivot", display = display.pane)
plot( pp1618,  color = lineee1618Color, linewidth = 1, style = plot.style_line, title="+ 1.618 Expansion Pivot", display = display.pane)

plot( m0382, color = line0382Color, linewidth = 1, style = plot.style_line, title="- 0.382 Pivot", display = display.pane)
plot( m0618, color = line0618Color, linewidth = 1, style = plot.style_line, title="- 0.618 Pivot", display = display.pane)
bottomBB = plot( Bottom1, color = lineT1Color, linewidth = 2, style = plot.style_line, title="Main Bottom Bollinger Band", display = display.pane)
plot( mm0382, color = lineee0382Color, linewidth = 1, style = plot.style_line, title="- 0.382 Expansion Pivot", display = display.pane)
plot( mm0618, color = lineee0618Color, linewidth = 1, style = plot.style_line, title="- 0.618 Expansion Pivot", display = display.pane)
plot( mm1, color = lineee1Color, linewidth = 1, style = plot.style_line, title=" - 1 Expansion Pivot", display = display.pane)
plot( mm1618, color = lineee1618Color, linewidth = 1, style = plot.style_line, title="- 1.618 Expansion Pivot", display = display.pane)

fill(topBB, bottomBB, title = "Background", color=color.rgb(33, 100, 243, 95))

if hideLabels == false    

    lm := label.new(bar_index+labelDistance, smma, str.tostring(smma, decs) + ' is 20 SMA', color=#00000000, textcolor=mainPivotColor, style=label.style_label_left, size=labelSize)

    l1 := label.new(bar_index+labelDistance, p0382, str.tostring(p0382, decs), color=#00000000, textcolor=line0382Color, style=label.style_label_left, size=labelSize)
    l2 := label.new(bar_index+labelDistance, p0618, str.tostring(p0618, decs), color=#00000000, textcolor=line0618Color, style=label.style_label_left, size=labelSize)
    l3 := label.new(bar_index+labelDistance, Top1,  str.tostring(Top1, decs) + " is Top Bollindger Band", color=#00000000, textcolor=lineT1Color, style=label.style_label_left, size=labelSize)
    l4 := label.new(bar_index+labelDistance, pp0382, str.tostring(pp0382, decs), color=#00000000, textcolor=lineee0382Color, style=label.style_label_left, size=labelSize)
    l5 := label.new(bar_index+labelDistance, pp0618, str.tostring(pp0618, decs), color=#00000000, textcolor=lineee0618Color, style=label.style_label_left, size=labelSize)                          
    l6 := label.new(bar_index+labelDistance, pp1,  str.tostring(pp1, decs), color=#00000000, textcolor=lineee1Color, style=label.style_label_left, size=labelSize)
    l7 := label.new(bar_index+labelDistance, pp1618, str.tostring(pp1618, decs), color=#00000000, textcolor=lineee1618Color, style=label.style_label_left, size=labelSize)   

    r11 := label.new(bar_index+labelDistance, m0382, str.tostring(m0382, decs), color=#00000000, textcolor=line0382Color, style=label.style_label_left,size=labelSize)
    r22 := label.new(bar_index+labelDistance, m0618, str.tostring(m0618, decs), color=#00000000, textcolor=line0618Color, style=label.style_label_left, size=labelSize)
    r33 := label.new(bar_index+labelDistance, Bottom1, str.tostring(Bottom1, decs) + " is Bottom Bollindger Band", color=#00000000, textcolor=lineT1Color, style=label.style_label_left, size=labelSize)
    r44 := label.new(bar_index+labelDistance, mm0382, str.tostring(mm0382, decs) , color=#00000000, textcolor=lineee0382Color, style=label.style_label_left, size=labelSize)
    r55 := label.new(bar_index+labelDistance, mm0618, str.tostring(mm0618, decs), color=#00000000, textcolor=lineee0618Color, style=label.style_label_left, size=labelSize)
    r66 := label.new(bar_index+labelDistance, mm1, str.tostring(mm1, decs), color=#00000000, textcolor=lineee1Color, style=label.style_label_left, size=labelSize)
    r77 := label.new(bar_index+labelDistance, mm1618, str.tostring(mm1618, decs), color=#00000000, textcolor=lineee1618Color, style=label.style_label_left, size=labelSize)

    label.delete(lm[1])     

    label.delete(l1[1])
    label.delete(l2[1])
    label.delete(l3[1])
    label.delete(l4[1])
    label.delete(l5[1])
    label.delete(l6[1])
    label.delete(l7[1])

    label.delete(r11[1])
    label.delete(r22[1])
    label.delete(r33[1])
    label.delete(r44[1])
    label.delete(r55[1])
    label.delete(r66[1])
    label.delete(r77[1])