Volume + Volatility
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sueun123

// @version=5

indicator(title = "Volume + Volatility", shorttitle = "Vo & Va", overlay = false, format = format.inherit, precision = 2, scale = scale.right)

//---Input Section---
// Input H, L, C, O 

h = input(high,  title = "Line 1")
l = input(low,   title = "Line 2")
c = input(close, title = "Line 3")
o = input(open,  title = "Line 4")

// Input WMA
wma_length = input.int(defval = 7, minval = 1, title = "WMA Length")
wma_source = input(close, title = "WMA Source")

//---Calculation Section---
// Calculate H, L, C, O
hlco_formula = (h+l)/2

// Calculate WMA
wma_formula = ta.wma(2*ta.wma(wma_source, wma_length/2) - ta.wma(wma_source, wma_length), ((wma_length)))

// Calculate BB
multiply__BB = input(defval = 1.0, title = "BB Deviation")

upperbb_formula = ta.wma(c, wma_length)
lowerbb_formula = ta.wma(o, wma_length)

upper_stdev = multiply__BB * ta.stdev(h, wma_length)
lower_stdev = multiply__BB * ta.stdev(l, wma_length)

upperbb = upperbb_formula + upper_stdev
lowerbb = lowerbb_formula - lower_stdev

// Calculate CCI
cci = ta.cci(hlco_formula, wma_length)
cci_formula = ta.wma(cci, wma_length)

c1 = cci > upperbb
c2 = cci < upperbb and cci > cci_formula
c3 = cci > lowerbb and cci < cci_formula
c4 = cci < lowerbb

// Calculate WMA & BB Crosses
signal_1 = ta.crossover(c, upperbb) or ta.crossunder(c, upperbb) 
signal_2 = ta.crossover(c, lowerbb) or ta.crossunder(c, lowerbb) 
signal_3 = ta.crossunder(o, upperbb) or ta.crossunder(o, upperbb)
signal_4 = ta.crossunder(o, lowerbb) or ta.crossunder(o, lowerbb)

//---Plot Section---
// Plot H, L, C, O 
p1 = plot(c, title = "Line 3", color = color.green, transp = 0, linewidth = 2, style = plot.style_line, linewidth = 2, display = display.all, editable = true) //Only Close
p2 = plot(o, title = "Line 4", color = color.blue,  transp = 0, linewidth = 2, style = plot.style_line, linewidth = 2, display = display.all, editable = true) //Only Open

// Plot WMA & BB
plot(c, color = signal_1 ? color.red    : na, style = plot.style_circles, title = "Line 3 outside Upper BB", linewidth = 3, display = display.none)
plot(c, color = signal_2 ? color.blue   : na, style = plot.style_circles, title = "Line 3 outside Upper BB", linewidth = 3, display = display.none)
plot(o, color = signal_3 ? color.green  : na, style = plot.style_circles, title = "Line 4 outside Upper BB", linewidth = 3, display = display.none)
plot(o, color = signal_4 ? color.orange : na, style = plot.style_circles, title = "Line 4 outside Lower BB", linewidth = 3, display = display.none)

// Plot BB
p3 = plot(upperbb, title = "Upper BB", color = color.yellow, transp = 45, linewidth = 1, editable = true)
p4 = plot(lowerbb, title = "Lower BB", color = color.yellow, transp = 45, linewidth = 1, editable = true)

//---Fill Section---
// Fill H, L, C, O
fillColor1 = c > upperbb ? color.new(color.yellow, transp = 45) : na
fill(p3, p1, fillColor1, title = "Line 3 Volatile Up")

fillColor2 = o > upperbb ? color.new(color.yellow, transp = 45) : na
fill(p3, p2, fillColor2, title = "Line 4 Volatile Up")

fillColor3 = c < lowerbb ? color.new(color.yellow, transp = 45) : na
fill(p4, p1, fillColor3, title = "Line 3 Volatile Down")

fillColor4 = o < lowerbb ? color.new(color.yellow, transp = 45) : na
fill(p4, p2, fillColor4, title = "Line 4 Volatile Down")

// Fill BB
fill_bb_color = c1 ? color.new(color.green, transp = 75) : c2 ? color.new(color.green, transp = 75) : c3 ? color.new(color.red, transp = 75) : color.new(color.red, transp = 75)
fill(p3, p4, color = fill_bb_color, title = "BB Fill")

//---END---