Bollinger bands dynamic alerts
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RajeevNaik
//@version=5
indicator('Bollinger bands dynamic alerts', shorttitle='BBx3', overlay=true)

//input
length = input.int(20, minval=1)
//1 sigma
[middlea, uppera, lowera] = ta.bb(close, length, 1)
plot(middlea, color=color.new(color.purple, 0))
a = plot(uppera, color=color.new(color.blue, 0))
b = plot(lowera, color=color.new(color.blue, 0))
fill(a, b, title='Congestion', color=color.new(color.blue, 90))

//2 sigma
[middleb, upperb, lowerb] = ta.bb(close, length, 2)
//plot(middle1, color=color.orange)
a1 = plot(upperb, color=color.new(color.orange, 0))
b1 = plot(lowerb, color=color.new(color.orange, 0))
fill(a1, b1, title='Trend', color=color.new(color.orange, 90))

//3 sigma
[middlec, upperc, lowerc] = ta.bb(close, length, 3)
//plot(middle1, color=color.orange)
a2u = close > upperc
b2l = lowerc > close
a2 = plot(upperc, color=color.new(color.red, 0))
b2 = plot(lowerc, color=color.new(color.red, 0))
fill(a2, b2, title='Blowout', color=color.new(color.red, 90))

//for alerts
//BB1

src = input(close, title='Source')
mult = input.float(1.0, minval=0.001, maxval=50, title='StdDev')
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev


//BB2
mult2 = input.float(2.0, minval=0.001, maxval=50, title='StdDev')
dev2 = mult2 * ta.stdev(src, length)
upper2 = basis + dev2
lower2 = basis - dev2


mult3 = input.float(3.0, minval=0.001, maxval=50, title='StdDev')
dev3 = mult3 * ta.stdev(src, length)
upper3 = basis + dev3
lower3 = basis - dev3



// dynamic alerts  
//BB1
f_triggerBBU() =>
    _s = basis + dev
    _co = ta.crossover(close, _s)
    _cu = ta.crossunder(close, _s)
    if _co
        alert('Price (' + str.tostring(close) + ') crossing up congestion  BB1 (' + str.tostring(_s) + ')', alert.freq_once_per_bar)

f_triggerBBL() =>
    _s = basis - dev
    _co = ta.crossover(close, _s)
    _cu = ta.crossunder(close, _s)
    if _cu
        alert('Price (' + str.tostring(close) + ') crossing down congestion  BB 1(' + str.tostring(_s) + ')', alert.freq_once_per_bar)


f_triggerBBU()
f_triggerBBL()

//BB2
f_triggerBBU2() =>
    _s = basis + dev2
    _co = ta.crossover(close, _s)
    _cu = ta.crossunder(close, _s)
    if _co
        alert('Price (' + str.tostring(close) + ') crossing up  BB2 trend (' + str.tostring(_s) + ')', alert.freq_once_per_bar)

f_triggerBBL2() =>
    _s = basis - dev2
    _co = ta.crossover(close, _s)
    _cu = ta.crossunder(close, _s)
    if _cu
        alert('Price (' + str.tostring(close) + ') crossing down  BB2 trend (' + str.tostring(_s) + ')', alert.freq_once_per_bar)


f_triggerBBU2()
f_triggerBBL2()

//BB3
f_triggerBBU3() =>
    _s = basis + dev3
    _co = ta.crossover(close, _s)
    _cu = ta.crossunder(close, _s)
    if _co
        alert('Price (' + str.tostring(close) + ') crossing up  BB3 Blowout (' + str.tostring(_s) + ')', alert.freq_once_per_bar)

f_triggerBBL3() =>
    _s = basis - dev3
    _co = ta.crossover(close, _s)
    _cu = ta.crossunder(close, _s)
    if _cu
        alert('Price (' + str.tostring(close) + ') crossing down  BB3 Blowout (' + str.tostring(_s) + ')', alert.freq_once_per_bar)


f_triggerBBU3()
f_triggerBBL3()


