Logarithmic Bollinger Bands
//  © 2022 KINGTHIES THIS SOURCE CODE IS SUBJECT TO TERMS OF MOZILLA PUBLIC LICENSE 2.0 (MOZILLA.ORG/MPL/2.0)
//@version=5
//## !<----------------  © KINGTHIES --------------------->
indicator('Logarithmic Bollinger Bands (kingthies)',shorttitle='LogBands_KT',overlay=true)

// { BBANDS
src = math.log(input(close,title="Source"))
lenX = input(20,title='Length')
highlights = input(false,title="Highlight Bear and Bull Expansions?")
mult = 2
bbandBasis = ta.sma(src,lenX)
dev = 2 * ta.stdev(src, 20)
upperBB = bbandBasis + dev
lowerBB = bbandBasis - dev
bbw = (upperBB-lowerBB)/bbandBasis
bbr = (src - lowerBB)/(upperBB - lowerBB)
// }

// { BBAND EXPANSIONS
bullExp= ta.rising(upperBB,1) and ta.falling(lowerBB,1) and ta.rising(bbandBasis,1) and ta.rising(bbw,1) and ta.rising(bbr,1) 
bearExp= ta.rising(upperBB,1) and ta.falling(lowerBB,1) and ta.falling(bbandBasis,1) and ta.rising(bbw,1) and ta.falling(bbr,1) 
// } 

// { COLORS 
greenBG = color.rgb(9,121,105,75), redBG = color.rgb(136,8,8,75)
bullCol = highlights and bullExp ? greenBG : na, bearCol = highlights and bearExp ? redBG : na
// } 

// { INDICATOR PLOTTING
lowBB=plot(math.exp(lowerBB),title='Low Band',color=color.aqua),plot(math.exp(bbandBasis),title='BBand Basis',color=color.red),
highBB=plot(math.exp(upperBB),title='High Band',color=color.aqua),fill(lowBB,highBB,title='Band Fill Color',color=color.rgb(0,128,128,75)) 
bgcolor(bullCol,title='Bullish Expansion Highlights'),bgcolor(bearCol,title='Bearish Expansion Highlights')
// } 

// { Alerts 
alertcondition(bullExp,title='Bullish Expansion Alert',message='Bullish Expansion on {{ticker}}')
alertcondition(bearExp,title='Bearish Expansio Alert',message='Bearish Expansion on {{ticker}}')