VWAP Balance Zones
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SamRecio

//@version=5
indicator("VWAP Balance Zones", shorttitle = "VBZ", overlay = true)

d_tog = not input.bool(true, title = "", inline = "1")
dc = input.color(color.rgb(93, 63, 211), title = "Daily                    ", inline = "1") 
dc_shade = input.color(color.rgb(93, 63, 211, 75), title = "", inline = "1") 
w_tog = not input.bool(true, title = "", inline = "2")
wc = input.color(color.rgb(63, 211, 70), title = "Weekly                 ", inline = "2") 
wc_shade = input.color(color.rgb(63, 211, 70, 75), title = "", inline = "2") 
m_tog = not input.bool(true, title = "", inline = "3")
mc = input.color(color.rgb(63, 189, 211), title = "Monthly                ", inline = "3") 
mc_shade = input.color(color.rgb(63, 189, 211, 75), title = "", inline = "3") 

dash() => (bar_index/2 - math.floor(bar_index/2)) > 0

get_st(_src,_func,_reset) =>
    var _val = _src
    if _reset
        _val := _src
    condition = _func == ">"?_src>_val:_func == "<"?_src<_val:false
    if condition
        _val := _src
    _val

no_show(_cond1,_cond2) =>
    var output = true
    if _cond1 and _cond2
        output := true
    if _cond2 and not _cond1
        output := false
    output

nd = timeframe.change("D")
nw = timeframe.change("W")
nm = timeframe.change("M")

no_show_week = no_show(nw,nd)
no_show_month = no_show(nm,nw)

high_vwap = ta.vwap(high,nd)
close_vwap = ta.vwap(close,nd)
low_vwap = ta.vwap(low,nd)

w_vwap = ta.vwap(hlc3,nw)
m_vwap = ta.vwap(hlc3,nm)

plot(nd or d_tog?na:high_vwap, style = plot.style_linebr, color = dc, title = "High VWAP")
plot(nd or d_tog?na:close_vwap, style = plot.style_linebr, color = dash()?color.rgb(0,0,0,100):dc, title = "Close VWAP")
plot(nd or d_tog?na:low_vwap, style = plot.style_linebr, color = dc, title = "Low VWAP")

plot(nw or (no_show_week and not d_tog) or w_tog?na:w_vwap, style = plot.style_linebr, color =dash()?color.rgb(0,0,0,100):wc, linewidth = 2, title = "Weekly VWAP")

plot(nm or (no_show_month and not w_tog) or m_tog?na:m_vwap, style = plot.style_linebr, color =dash()?color.rgb(0,0,0,100):mc, linewidth = 3, title = "Monthly VWAP")

h_hi = get_st(high,">",nd)
h_clo = get_st(close,">",nd)
h_lo = get_st(low,">",nd)
l_lo = get_st(low,"<",nd)
l_clo = get_st(close,"<",nd)
l_hi = get_st(high,"<",nd)

w_lo = get_st(low,"<",nw)
w_hi = get_st(high,">",nw)
 
m_lo = get_st(low,"<",nm)
m_hi = get_st(high,">",nm)

low_50 = math.avg(close_vwap,l_clo)
high_50 = math.avg(close_vwap,h_clo)
h_vwap_h = math.avg(high_vwap,h_hi)
h_vwap_l = math.avg(low_vwap,h_lo)
l_vwap_h = math.avg(high_vwap,l_hi)
l_vwap_l = math.avg(low_vwap,l_lo)

w_low_50 = math.avg(w_vwap,w_lo)
w_high_50 = math.avg(w_vwap,w_hi)

m_low_50 = math.avg(m_vwap,m_lo)
m_high_50 = math.avg(m_vwap,m_hi)

l_50 = plot(nd or d_tog?na:low_50, style = plot.style_linebr, color = dc, title = "Lo-Close 50%")
h_50 = plot(nd or d_tog?na:high_50, style = plot.style_linebr, color = dc, title = "Hi-Close 50%")

hh_50 = plot(nd or d_tog?na:h_vwap_h, style = plot.style_linebr, color = dc, title = "Hi-High 50%")
hl_50 = plot(nd or d_tog?na:h_vwap_l, style = plot.style_linebr, color = dc, title = "Hi-Low 50%")

lh_50 = plot(nd or d_tog?na:l_vwap_h, style = plot.style_linebr, color = dc, title = "Lo-High 50%")
ll_50 = plot(nd or d_tog?na:l_vwap_l, style = plot.style_linebr, color = dc, title = "Lo-Low 50%")

fill(hh_50,hl_50,nd or d_tog?color.rgb(0,0,0,100):dc_shade, title = "Daily Hi 50% Zone")
fill(lh_50,ll_50,nd or d_tog?color.rgb(0,0,0,100):dc_shade, title = "Daily Lo 50% Zone")

plot(nw or (no_show_week and not d_tog) or w_tog?na:w_low_50, style = plot.style_linebr, color = wc, linewidth = 2, title = "Week High 50%")
plot(nw or (no_show_week and not d_tog) or w_tog?na:w_high_50, style = plot.style_linebr, color = wc, linewidth = 2, title = "Week Low 50%")

plot(nm or (no_show_month and not w_tog) or m_tog?na:m_low_50, style = plot.style_linebr, color = mc, linewidth = 3, title = "Month High 50%")
plot(nm or (no_show_month and not w_tog) or m_tog?na:m_high_50, style = plot.style_linebr, color = mc, linewidth = 3, title = "Month Low 50%")



