Trending Bollinger Bands by SiddWolf
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SiddWolf

//@version=5
indicator(title="Trending Bollinger Bands by SiddWolf", shorttitle="TBB [SiddWolf]", overlay=true, timeframe="", timeframe_gaps=false)

calc_ma(source, length, type) =>
    type == "SMA" ? ta.sma(source, length) :
     type == "EMA" ? ta.ema(source, length) :
     type == "SMMA (RMA)" ? ta.rma(source, length) :
     type == "WMA" ? ta.wma(source, length) :
     type == "VWMA" ? ta.vwma(source, length) :
     type == "HMA" ? ta.hma(source, length) :
     na


mee_ma_type = input.string(title="Moving Average: ", defval="SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA"], inline="MA")
mee_ma_length = input.int(title="Length", defval=50, minval=1, maxval=300, inline="MA")
mee_ma_src_basis = input.source(title="Basis Source", defval=hl2, inline="src")
mee_ma_src_band = input.string(title="Band Source", defval="Close", options=["Close", "High/Low"], inline="src")
mult_a = input.float(1.618, minval=0.001, maxval=50, title="StdDev Inner", inline="mult")
mult_b = input.float(2.618, minval=0.001, maxval=50, title="StdDev Outer", inline="mult")

basis = calc_ma(mee_ma_src_basis, mee_ma_length, mee_ma_type)

basis_top_for_aa = calc_ma(close , mee_ma_length, mee_ma_type)
basis_bottom_for_aa = calc_ma(close , mee_ma_length, mee_ma_type)

basis_top_for_bb = calc_ma(mee_ma_src_band == "Close" ? close : high , mee_ma_length, mee_ma_type)
basis_bottom_for_bb = calc_ma(mee_ma_src_band == "Close" ? close : low , mee_ma_length, mee_ma_type)

dev_a = mult_a * ta.stdev(close, mee_ma_length)
dev_b = mult_b * ta.stdev(close, mee_ma_length)

upper_a = basis_top_for_aa + dev_a
lower_a = basis_bottom_for_aa - dev_a

upper_b = basis_top_for_bb + dev_b
lower_b = basis_bottom_for_bb - dev_b

upper_b_plus_atr = upper_b + ta.atr(math.floor(mee_ma_length/2))
lower_b_plus_atr = lower_b - ta.atr(math.floor(mee_ma_length/2))

upper_a_minus_atr = upper_a - ta.atr(math.floor(mee_ma_length/2))
lower_a_minus_atr = lower_a + ta.atr(math.floor(mee_ma_length/2))

plot(basis, "Basis", color=#FF6D00)


p_upper_a_minus_atr =  plot(upper_a_minus_atr, "Upper_AA", color=color.new(#0000ff, 50))
p_upper_b_plus_atr = plot(upper_b_plus_atr, "Upper_BB", color=color.new(#0000ff, 50))
p_lower_a_minus_atr =  plot(lower_a_minus_atr, "Lower_AA", color=color.new(#0000ff, 50))
p_lower_b_plus_atr = plot(lower_b_plus_atr, "Lower_BB", color=color.new(#0000ff, 50))

p_upper_a = plot(upper_a, "Upper_aa", color=color.new(#2962FF, 90), display=display.none)
p_upper_b = plot(upper_b, "Upper_bb", color=color.new(#2962FF, 90), display=display.none)
p_lower_a = plot(lower_a, "Lower_aa", color=color.new(#2962FF, 90), display=display.none)
p_lower_b = plot(lower_b, "Lower_bb", color=color.new(#2962FF, 90), display=display.none)

fill(p_upper_a, p_upper_b, title = "Background Green Inner", color=color.new(#00ff00, 95))
fill(p_lower_a, p_lower_b, title = "Background Red Inner", color=color.new(#ff0000, 95))
fill(p_upper_a_minus_atr, p_upper_b_plus_atr, title = "Background Green Outer", color=color.new(#00ff00, 95))
fill(p_lower_a_minus_atr, p_lower_b_plus_atr, title = "Background Red Outer ", color=color.new(#ff0000, 95))

