[DIP] Inverse BB/Bollinger highlight for bars
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Yubu

//@version=5
indicator(title='[DIP] Inverse BB Highlight', shorttitle='[DIP] Bar Highlight (v1.0)', overlay=true, max_labels_count = 500)

// Functions
bb(float source, simple int period, int offset = 0) =>

    // BB Calc
    ma = ta.sma(source, period)
    std = ta.stdev(source, period)

    std2_lower = ma + (-2 + offset) * std
    std2_upper = ma + (2 + offset) * std
    std3_lower = ma + (-3 + offset) * std
    std3_upper = ma + (3 + offset) * std
    
    // Output
    [ma, std2_lower, std2_upper, std3_lower, std3_upper]

// Inputs
timeframe = input.timeframe(title='Timeframe', defval='', group='General', inline='1')
source = input.source(title='', defval=close, group='General', inline='1')

// BB Input
bb_group = 'Bollinger Band'
bool bb_enabled = input.bool(defval = false, title='Show BB', group=bb_group, inline = '10', tooltip = 'Enable Bollinger Band Visualization')
color bb_color = input.color(defval = color.rgb(0, 255, 255, 66), title='', group=bb_group, inline = '10')
int bb_period = input.int(defval=110, title='BB Period', group=bb_group)
int bb_offset = input.int(defval=0, minval=-5, maxval=5, title='BB Offset', group=bb_group, tooltip = 'Add or subtract a deviation from the bollinger band, this can help visualize a directional bias on markets with a strong trend.' )

// Color Input
color_group = 'Style Settings'
override = input.bool(defval = true, inline = '30', title = '', group = color_group)
override_color = input.color(defval = color.rgb(255, 255, 255, 90), inline = '30', title = 'Override Color', tooltip = 'If enabled, overrides the default bar color inside the bollinger bands.', group = color_group)
aboveBB_color = input.color(defval = color.red, title = 'Above BB Color', group = color_group)
belowBB_color = input.color(defval = color.green, title = 'Below BB Color', group = color_group)
transBB_color = input.color(defval = color.rgb(255, 255, 255, 70), title = 'Cross BB Color', group = color_group)

// BB Calc
[bb_ma, bb2_low, bb2_up, bb3_low, bb3_up] = request.security(syminfo.tickerid, timeframe, bb(source, bb_period, bb_offset))

// BB Output
// plot_base_ma = plot(bb_ma, color=color.new(bb_color, 50), linewidth=2, title='BB Base MA')
plot_upper_std_2 = plot(bb_enabled ? bb2_up : na, color=color.new(bb_color, 70), title='BB Upper')
plot_lower_std_2 = plot(bb_enabled ? bb2_low : na, color=color.new(bb_color, 70), title='BB Lower')

fill(plot_upper_std_2, plot_lower_std_2, color = color.new(bb_color, 96), fillgaps=true)

// Bar Color
aboveBB = close > bb2_up and open > bb2_up
belowBB = close < bb2_low and open < bb2_low
transBB = (open < bb2_up and close > bb2_up) or (open > bb2_up and close < bb2_up) or (open > bb2_low and close < bb2_low) or (open < bb2_low and close > bb2_low)

BC = aboveBB ? aboveBB_color : belowBB ? belowBB_color : transBB ? transBB_color : override ? override_color : na

barcolor(color = BC, display = display.all)