MTF VWAP & StDev Bands
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5

indicator("VWAP • Yata", 
     overlay = true)

// ----------------------------------------
groupVWAP = "Volume Weighted Average Price"
// ----------------------------------------

computeVWAP(src, isNewPeriod) =>
	var float sumSrcVol = na
	var float sumVol = na
    var float sumSrcSrcVol = na

	sumSrcVol := isNewPeriod ? src * volume : src * volume + sumSrcVol[1]
	sumVol := isNewPeriod ? volume : volume + sumVol[1]
	// sumSrcSrcVol calculates the dividend of the equation that is later used to calculate the standard deviation
	sumSrcSrcVol := isNewPeriod ? volume * math.pow(src, 2) : volume * math.pow(src, 2) + sumSrcSrcVol[1]

	_vwap = sumSrcVol / sumVol
	variance = sumSrcSrcVol / sumVol - math.pow(_vwap, 2)
	variance := variance < 0 ? 0 : variance
	stDev = math.sqrt(variance)

	[_vwap, stDev]

// ----------------------------------------

compute_stDEV(_vwap, stDev, stDevMultiplier_1, stDevMultiplier_2, stDevMultiplier_3, stDevMultiplier_4, stDevMultiplier_5, stDevMultiplier_6) =>
    upperBand_6 = _vwap + stDev * stDevMultiplier_6
    upperBand_5 = _vwap + stDev * stDevMultiplier_5
    upperBand_4 = _vwap + stDev * stDevMultiplier_4
    upperBand_3 = _vwap + stDev * stDevMultiplier_3
    upperBand_2 = _vwap + stDev * stDevMultiplier_2
    upperBand_1 = _vwap + stDev * stDevMultiplier_1
    lowerBand_1 = _vwap - stDev * stDevMultiplier_1
    lowerBand_2 = _vwap - stDev * stDevMultiplier_2
	lowerBand_3 = _vwap - stDev * stDevMultiplier_3
	lowerBand_4 = _vwap - stDev * stDevMultiplier_4
	lowerBand_5 = _vwap - stDev * stDevMultiplier_5
	lowerBand_6 = _vwap - stDev * stDevMultiplier_6
    
    [upperBand_6, upperBand_5, upperBand_4, upperBand_3, upperBand_2, upperBand_1, lowerBand_1, lowerBand_2, lowerBand_3, lowerBand_4, lowerBand_5, lowerBand_6]

// ----------------------------------------

f_drawLabel(_x, _y, _text, _textcolor, _style, _size) =>
    var _label = label.new(
     x          = _x,
     y          = _y,
     text       = _text,
     textcolor  = _textcolor,
     style      = _style,
     size       = _size,
     xloc       = xloc.bar_time
     )
    
    label.set_xy(_label, _x, _y)

// ----------------------------------------

src = input(hlc3, title="VWAP Source", inline="V0", group=groupVWAP)

vD_color = input.color(color.new(#E12D7B, 50), title=""	, inline="V1", group=groupVWAP)
vW_color = input.color(color.new(#F67B52, 50), title=""	, inline="V2", group=groupVWAP)
vM_color = input.color(color.new(#EDCD3B, 50), title=""	, inline="V3", group=groupVWAP)
vQ_color = input.color(color.new(#3BBC54, 50), title=""	, inline="V4", group=groupVWAP)
vY_color = input.color(color.new(#2665BD, 50), title=""	, inline="V5", group=groupVWAP)

plot_vD = input(true	, title="Show Daily VWAP"		, inline="V1", group=groupVWAP)
plot_vW = input(true	, title="Show Weekly VWAP"		, inline="V2", group=groupVWAP)
plot_vM = input(true	, title="Show Monthly VWAP"		, inline="V3", group=groupVWAP)
plot_vQ = input(false	, title="Show Quarterly VWAP"	, inline="V4", group=groupVWAP)
plot_vY = input(false	, title="Show Yearly VWAP"		, inline="V5", group=groupVWAP)

// ----------------------------------
groupSTD = "Standard Deviation Bands"
// ----------------------------------

Std_selection = input.string("Month", title="Timeframe", options = ["Day", "Week", "Month", "Quarter", "Year"], inline="STD0", group=groupSTD)

showStd = input(true, title="Show VWAP Bands |", inline="STD1", group=groupSTD)
showpStd = input(false, title="Show Prev. VWAP Bands |", inline="STD2", group=groupSTD)

showL = input(false, title="Lines", inline="STD1", group=groupSTD)
showpL = input(true, title="Lines", inline="STD2", group=groupSTD)

showLC = showL ? display.all : display.none
showpLC = showpL ? display.all : display.none

fill_bands	= input(true, title="Fill", inline="STD1", group=groupSTD)
fill_pbands = input(false, title="Fill", inline="STD2", group=groupSTD)

stDevMultiplier_1 = input.float(0.618	, step=0.1, title="StDev 1:", inline="StDev1", group=groupSTD)
stDevMultiplier_2 = input.float(1.0		, step=0.1, title="StDev 2:", inline="StDev2", group=groupSTD)
stDevMultiplier_3 = input.float(1.618	, step=0.1, title="StDev 3:", inline="StDev3", group=groupSTD)
stDevMultiplier_4 = input.float(2.0		, step=0.1, title="StDev 4:", inline="StDev4", group=groupSTD)
stDevMultiplier_5 = input.float(2.618	, step=0.1, title="StDev 5:", inline="StDev5", group=groupSTD)
stDevMultiplier_6 = input.float(3.0		, step=0.1, title="StDev 6:", inline="StDev6", group=groupSTD)

plot_pL1 = input(true, title="Previous StDev 1", inline="StDev1", group=groupSTD)
plot_pL2 = input(true, title="Previous StDev 2", inline="StDev2", group=groupSTD)
plot_pL3 = input(true, title="Previous StDev 3", inline="StDev3", group=groupSTD)
plot_pL4 = input(true, title="Previous StDev 4", inline="StDev4", group=groupSTD)
plot_pL5 = input(true, title="Previous StDev 5", inline="StDev5", group=groupSTD)
plot_pL6 = input(true, title="Previous StDev 6", inline="StDev6", group=groupSTD)

up_color = input.color(color.red, title="|", inline="STD0", group=groupSTD)
lw_color = input.color(color.blue, title="", inline="STD0", group=groupSTD)

// --------------
groupL = "Labels"
// --------------

show_labels = input(true, title="Show Labels |", inline="L1", group=groupL)
show_VWAPlabels = input(true, title="VWAP", inline="L1", group=groupL)
show_STDlabels = input(true, title="StDev", inline="L1", group=groupL)

show_prevlabels = input(true, title="Show Previous Labels", inline="L2", group=groupL)

off_mult = input(15, title="| Offset", inline="L2", group=groupL)
var DEFAULT_LABEL_SIZE	= size.normal
var DEFAULT_LABEL_STYLE	= label.style_none
ll_offset = timenow + math.round(ta.change(time) * off_mult)

// --------------

timeChange(period) =>
	ta.change(time(period))

newSessionD = timeChange("D")
newSessionW = timeChange("W")
newSessionM = timeChange("M")
newSessionQ = timeChange("3M")
newSessionY = timeChange("12M")

[vD, stdevD] = computeVWAP(src, newSessionD)
[vW, stdevW] = computeVWAP(src, newSessionW)
[vM, stdevM] = computeVWAP(src, newSessionM)
[vQ, stdevQ] = computeVWAP(src, newSessionQ)
[vY, stdevY] = computeVWAP(src, newSessionY)

// --------------

Vstyle = input(false, title="Circles Lines", inline="V0", group=groupVWAP)
VstyleC = Vstyle ? plot.style_circles : plot.style_line

vDplot = plot(plot_vD ? vD : na, title="VWAP - Daily", color=vD_color, style=VstyleC, linewidth=1)
f_drawLabel(ll_offset, show_labels and show_VWAPlabels and plot_vD ? vD : na, "vD", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

vWplot = plot(plot_vW ? vW : na, title="VWAP - Weekly", color=vW_color, style=VstyleC, linewidth=1)
f_drawLabel(ll_offset, show_labels and show_VWAPlabels and plot_vW ? vW : na, "vW", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

vMplot = plot(plot_vM ? vM : na, title="VWAP - Monthly", color=vM_color, style=VstyleC, linewidth=1)
f_drawLabel(ll_offset, show_labels and show_VWAPlabels and plot_vM ? vM : na, "vM", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

vQplot = plot(plot_vQ ? vQ : na, title="VWAP - Quarter", color=vQ_color, style=VstyleC, linewidth=1)
f_drawLabel(ll_offset, show_labels and show_VWAPlabels and plot_vQ ? vQ : na, "vQ", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

vYplot = plot(plot_vY ? vY : na, title="VWAP - Year", color=vY_color, style=VstyleC, linewidth=1)
f_drawLabel(ll_offset, show_labels and show_VWAPlabels and plot_vY ? vY : na, "vY", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

// --------------

stdev_sel = Std_selection == "Day" ? stdevD : 
         Std_selection == "Week" ? stdevW : 
         Std_selection == "Month" ? stdevM : 
         Std_selection == "Quarter" ? stdevQ : 
         Std_selection == "Year" ? stdevY : na

vwap_sel = Std_selection == "Day" ? vD : 
         Std_selection == "Week" ? vW : 
         Std_selection == "Month" ? vM : 
         Std_selection == "Quarter" ? vQ : 
         Std_selection == "Year" ? vY : na

prev_period = Std_selection == "Day" ? newSessionD : 
         Std_selection == "Week" ? newSessionW : 
         Std_selection == "Month" ? newSessionM : 
         Std_selection == "Quarter" ? newSessionQ : 
         Std_selection == "Year" ? newSessionY : na

[s6up, s5up, s4up, s3up, s2up, s1up, s1dn, s2dn, s3dn, s4dn, s5dn, s6dn] = compute_stDEV(vwap_sel, stdev_sel, stDevMultiplier_1, stDevMultiplier_2, stDevMultiplier_3, stDevMultiplier_4, stDevMultiplier_5, stDevMultiplier_6)

// --------------

A = plot(showStd ? s6up : na, title="VWAP - STDEV +6", color=color.new(color.silver, 75), style=plot.style_linebr, linewidth=1, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ?  s6up : na , "SD+3", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

B = plot(showStd ? s5up : na, title="VWAP - STDEV +5", color=color.new(color.silver, 75), style=plot.style_linebr, linewidth=1, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ?  s5up : na , "SD+2.5", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

C = plot(showStd ? s4up : na, title="VWAP - STDEV +4", color=color.new(color.silver, 75), style=plot.style_linebr, linewidth=1, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ?  s4up : na , "SD+2", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

D = plot(showStd ? s3up : na, title="VWAP - STDEV +3", color=color.new(color.silver, 75), style=plot.style_linebr, linewidth=1, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ?  s3up : na , "SD+1.5", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

E = plot(showStd ? s2up : na, title="VWAP - STDEV +2", color=color.new(color.silver, 75), style=plot.style_linebr, linewidth=1, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ?  s2up : na , "SD+1", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

F = plot(showStd ? s1up : na, title="VWAP - STDEV +1", color=color.new(color.silver, 75), style=plot.style_linebr, linewidth=1, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ?  s1up : na , "SD+0.5", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

G = plot(showStd ? s1dn : na, title="VWAP - STDEV -1", color=color.new(color.silver, 75), style=plot.style_linebr, linewidth=1, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ?  s1dn : na , "SD-0.5", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

H = plot(showStd ? s2dn : na, title="VWAP - STDEV -2", color=color.new(color.silver, 75), style=plot.style_linebr, linewidth=1, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ?  s2dn : na , "SD-1", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

I = plot(showStd ? s3dn : na, title="VWAP - STDEV -3", color=color.new(color.silver, 75), style=plot.style_linebr, linewidth=1, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ?  s3dn : na , "SD-1.5", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

J = plot(showStd ? s4dn : na, title="VWAP - STDEV -4", color=color.new(color.silver, 75), style=plot.style_linebr, linewidth=1, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ?  s4dn : na , "SD-2", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

K = plot(showStd ? s5dn : na, title="VWAP - STDEV -5", color=color.new(color.silver, 75), style=plot.style_linebr, linewidth=1, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ?  s5dn : na , "SD-2.5", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

L = plot(showStd ? s6dn : na, title="VWAP - STDEV -6", color=color.new(color.silver, 75), style=plot.style_linebr, linewidth=1, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ?  s6dn : na , "SD-3", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

// --------------
fb_transp = input.float(100, minval=0, maxval=100, title="Transp.", inline="STD3", group=groupSTD)
fb_step = input.float(5, minval=0, maxval=100, title="Step", inline="STD3", group=groupSTD)

fill_col_up5	= color.new(up_color, fb_transp - fb_step * 5)
fill_col_up4	= color.new(up_color, fb_transp - fb_step * 4)
fill_col_up3	= color.new(up_color, fb_transp - fb_step * 3)
fill_col_up2	= color.new(up_color, fb_transp - fb_step * 2)
fill_col_up		= color.new(up_color, fb_transp - fb_step * 1)

//fill_col_mid	= color.new(color.silver, fb_transp)

fill_col_down	= color.new(lw_color, fb_transp - fb_step * 1)
fill_col_down2	= color.new(lw_color, fb_transp - fb_step * 2)
fill_col_down3	= color.new(lw_color, fb_transp - fb_step * 3)
fill_col_down4	= color.new(lw_color, fb_transp - fb_step * 4)
fill_col_down5	= color.new(lw_color, fb_transp - fb_step * 5)

fill(A, B, fill_bands ? fill_col_up5 : na)
fill(B, C, fill_bands ? fill_col_up4 : na)
fill(C, D, fill_bands ? fill_col_up3 : na)
fill(D, E, fill_bands ? fill_col_up2 : na)
fill(E, F, fill_bands ? fill_col_up : na)

//fill(F, G, fill_bands ? fill_col_mid : na)

fill(G, H, fill_bands ? fill_col_down : na)
fill(H, I, fill_bands ? fill_col_down2 : na)
fill(I, J, fill_bands ? fill_col_down3 : na)
fill(J, K, fill_bands ? fill_col_down4 : na)
fill(K, L, fill_bands ? fill_col_down5 : na)

// --------------

previouslevels(period, VWAP, s6up, s5up, s4up, s3up, s2up, s1up, s1dn, s2dn, s3dn, s4dn, s5dn, s6dn) =>
    var float pV = na
	var float ps6up = na
	var float ps5up = na
	var float ps4up = na
	var float ps3up = na
    var float ps2up = na
    var float ps1up = na
    var float ps1dn = na
    var float ps2dn = na
	var float ps3dn = na
	var float ps4dn = na
	var float ps5dn = na
	var float ps6dn = na
    pV := period ? VWAP[1] : pV
	ps6up := period ? s6up[1] : ps6up
	ps5up := period ? s5up[1] : ps5up
    ps4up := period ? s4up[1] : ps4up
    ps3up := period ? s3up[1] : ps3up
    ps2up := period ? s2up[1] : ps2up
    ps1up := period ? s1up[1] : ps1up
    ps1dn := period ? s1dn[1] : ps1dn
    ps2dn := period ? s2dn[1] : ps2dn
    ps3dn := period ? s3dn[1] : ps3dn
    ps4dn := period ? s4dn[1] : ps4dn
    ps5dn := period ? s5dn[1] : ps5dn
    ps6dn := period ? s6dn[1] : ps6dn

    [ pV, ps6up, ps5up, ps4up, ps3up, ps2up, ps1up, ps1dn, ps2dn, ps3dn, ps4dn, ps5dn, ps6dn ]

[ pV, ps6up, ps5up, ps4up, ps3up, ps2up, ps1up, ps1dn, ps2dn, ps3dn, ps4dn, ps5dn, ps6dn ] = previouslevels(prev_period, vwap_sel, s6up, s5up, s4up, s3up, s2up, s1up, s1dn, s2dn, s3dn, s4dn, s5dn, s6dn)

// --------------

pVplot = plot(showpStd and plot_pL1 ? pV : na, title="Previous VWAP", color=color.new(color.silver, 75), style=plot.style_stepline, linewidth = 3, display=showpLC)
f_drawLabel(ll_offset, show_prevlabels and plot_pL1 and showpStd ? pV : na, "pVWAP", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

pA = plot(showpStd and plot_pL6 ? ps6up : na, title="Previous VWAP - STDEV +6", color=color.new(color.silver, 75), style=plot.style_stepline, linewidth=1, display=showpLC)
f_drawLabel(ll_offset, show_prevlabels and plot_pL6 and showpStd ?  ps6up : na , "pSD+3", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

pB = plot(showpStd and plot_pL5 ? ps5up : na, title="Previous VWAP - STDEV +5", color=color.new(color.silver, 75), style=plot.style_stepline, linewidth=1, display=showpLC)
f_drawLabel(ll_offset, show_prevlabels and plot_pL5 and showpStd ?  ps5up : na , "pSD+2.5", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

pC = plot(showpStd and plot_pL4 ? ps4up : na, title="Previous VWAP - STDEV +4", color=color.new(color.silver, 75), style=plot.style_stepline, linewidth=1, display=showpLC)
f_drawLabel(ll_offset, show_prevlabels and plot_pL4 and showpStd ?  ps4up : na , "pSD+2", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

pD = plot(showpStd and plot_pL3 ? ps3up : na, title="Previous VWAP - STDEV +3", color=color.new(color.silver, 75), style=plot.style_stepline, linewidth=1, display=showpLC)
f_drawLabel(ll_offset, show_prevlabels and plot_pL3 and showpStd ?  ps3up : na , "pSD+1.5", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

pE = plot(showpStd and plot_pL2 ? ps2up : na, title="Previous VWAP - STDEV +2", color=color.new(color.silver, 75), style=plot.style_stepline, linewidth=1, display=showpLC)
f_drawLabel(ll_offset, show_prevlabels and plot_pL2 and showpStd ?  ps2up : na , "pSD+1", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

pF = plot(showpStd and plot_pL1 ? ps1up : na, title="Previous VWAP - STDEV +1", color=color.new(color.silver, 75), style=plot.style_stepline, linewidth=1, display=showpLC)
f_drawLabel(ll_offset, show_prevlabels and plot_pL1 and showpStd ?  ps1up : na , "pSD+0.5", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

pG = plot(showpStd and plot_pL1 ? ps1dn : na, title="Previous VWAP - STDEV -1", color=color.new(color.silver, 75), style=plot.style_stepline, linewidth=1, display=showpLC)
f_drawLabel(ll_offset, show_prevlabels and plot_pL1 and showpStd ?  ps1dn : na , "pSD-0.5", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

pH = plot(showpStd and plot_pL2 ? ps2dn : na, title="Previous VWAP - STDEV -2", color=color.new(color.silver, 75), style=plot.style_stepline, linewidth=1, display=showpLC)
f_drawLabel(ll_offset, show_prevlabels and plot_pL2 and showpStd ?  ps2dn : na , "pSD-1", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

pI = plot(showpStd and plot_pL3 ? ps3dn : na, title="Previous VWAP - STDEV -3", color=color.new(color.silver, 75), style=plot.style_stepline, linewidth=1, display=showpLC)
f_drawLabel(ll_offset, show_prevlabels and plot_pL3 and showpStd ?  ps3dn : na , "pSD-1.5", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

pJ = plot(showpStd and plot_pL4 ? ps4dn : na, title="Previous VWAP - STDEV -4", color=color.new(color.silver, 75), style=plot.style_stepline, linewidth=1, display=showpLC)
f_drawLabel(ll_offset, show_prevlabels and plot_pL4 and showpStd ?  ps4dn : na , "pSD-2", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

pK = plot(showpStd and plot_pL5 ? ps5dn : na, title="Previous VWAP - STDEV -5", color=color.new(color.silver, 75), style=plot.style_stepline, linewidth=1, display=showpLC)
f_drawLabel(ll_offset, show_prevlabels and plot_pL5 and showpStd ?  ps5dn : na , "pSD-2.5", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

pL = plot(showpStd and plot_pL6 ? ps6dn : na, title="Previous VWAP - STDEV -6", color=color.new(color.silver, 75), style=plot.style_stepline, linewidth=1, display=showpLC)
f_drawLabel(ll_offset, show_prevlabels and plot_pL6 and showpStd ?  ps6dn : na , "pSD-3", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE) 

// --------------

fill(pA, pB, fill_pbands ? fill_col_up5 : na)
fill(pB, pC, fill_pbands ? fill_col_up4 : na)
fill(pC, pD, fill_pbands ? fill_col_up3 : na)
fill(pD, pE, fill_pbands ? fill_col_up2 : na)
fill(pE, pF, fill_pbands ? fill_col_up : na)

//fill(pF, pG, fill_pbands ? fill_col_mid : na)

fill(pG, pH, fill_pbands ? fill_col_down : na)
fill(pH, pI, fill_pbands ? fill_col_down2 : na)
fill(pI, pJ, fill_pbands ? fill_col_down3 : na)
fill(pJ, pK, fill_pbands ? fill_col_down4 : na)
fill(pK, pL, fill_pbands ? fill_col_down5 : na)

// --------------

vR_color		= input.color(color.new(#481899, 50), title=""					, inline="V6", group=groupVWAP)
rolling_sv		= input(false						, title="Show Rolling VWAP"	, inline="V6", group=groupVWAP)
rolling_period	= input.int(200						, title=""					, inline="V6", group=groupVWAP)

vwap_r = ta.vwma(src, rolling_period)

plot(rolling_sv == true ? vwap_r : na, title="VWAP - Rolling", color=vR_color, style=VstyleC, linewidth=1)
f_drawLabel(ll_offset, show_labels and show_VWAPlabels and rolling_sv ? vwap_r : na, "rV", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

// -----------------------------------
groupTD = "TD (Tom DeMark) Sequential"
// -----------------------------------

lastN = input.string("None", title="Show", options=["7, 8 and 9", "8 and 9", "Only 9", "None"], inline="TD1", group=groupTD)

LocBN = input.string("Below", title="Position: Buy", options=["Above", "Below"], inline="TD5", group=groupTD)
LocSN = input.string("Above", title="Sell", options=["Above", "Below"], inline="TD5", group=groupTD)

LocBNC = LocBN == "Above" ? location.abovebar : LocBN == "Below" ? location.belowbar : na
LocSNC = LocSN == "Above" ? location.abovebar : LocSN == "Below" ? location.belowbar : na

// -----------------------------------

i_style0 = "Label"
i_style1 = "Arrow"
i_style2 = "Triangle"
i_style3 = "Circle"
i_style4 = "Cross"

Bstyle = input.string(i_style3, title="Style: Buy"	, options=[i_style0, i_style1, i_style2, i_style3, i_style4], inline="TD6", group=groupTD)
Sstyle = input.string(i_style3, title="Sell"		, options=[i_style0, i_style1, i_style2, i_style3, i_style4], inline="TD6", group=groupTD)

f_getStyleB(_inputStyle) =>
    _return = _inputStyle == i_style1 ? shape.arrowup		:
              _inputStyle == i_style2 ? shape.triangleup	:
              _inputStyle == i_style3 ? shape.circle		:
              _inputStyle == i_style4 ? shape.cross			: shape.labelup
    _return

f_getStyleS(_inputStyle) =>
    _return = _inputStyle == i_style1 ? shape.arrowdown		: 
              _inputStyle == i_style2 ? shape.triangledown	:
              _inputStyle == i_style3 ? shape.circle		:
              _inputStyle == i_style4 ? shape.xcross		: shape.labeldown
    _return

// -----------------------------------

sellSetup = 0
sellSetup := close > close[4] ? sellSetup[1] == 9 ? 1 : sellSetup[1] + 1 : 0

buySetup = 0
buySetup := close < close[4] ? buySetup[1] == 9 ? 1 : buySetup[1] + 1 : 0

// -----------------------------------

plotshape(lastN == "1 to 9" or lastN == "6 to 9" or lastN == "7, 8 and 9" ? 
     buySetup == 7 : na, location=LocBNC, style=f_getStyleB(Bstyle), size=size.auto, color=color.new(color.lime, 0), text="", title="b7", textcolor=color.new(color.white, 15))
plotshape(lastN == "1 to 9" or lastN == "6 to 9" or lastN == "7, 8 and 9" or lastN == "8 and 9" ? 
     buySetup == 8 : na, location=LocBNC, style=f_getStyleB(Bstyle), size=size.auto, color=color.new(color.green, 0), text="", title="b8", textcolor=color.new(color.white, 15))
plotshape(lastN == "1 to 9" or lastN == "6 to 9" or lastN == "7, 8 and 9" or lastN == "8 and 9" or lastN == "Only 9" ? 
     buySetup == 9 : na, location=LocBNC, style=f_getStyleB(Bstyle), size=size.auto, color=color.new(color.teal, 0), text="9", title="b9", textcolor=color.new(color.white, 15))

// -----------------------------------

plotshape(lastN == "1 to 9" or lastN == "6 to 9" or lastN == "7, 8 and 9" ? 
     sellSetup == 7 : na, location=LocSNC, style=f_getStyleS(Sstyle), size=size.auto, color=color.new(color.orange, 0), text="", title="s7", textcolor=color.new(color.white, 15))
plotshape(lastN == "1 to 9" or lastN == "6 to 9" or lastN == "7, 8 and 9" or lastN == "8 and 9" ? 
     sellSetup == 8 : na, location=LocSNC, style=f_getStyleS(Sstyle), size=size.auto, color=color.new(color.red, 0), text="", title="s8", textcolor=color.new(color.white, 15))
plotshape(lastN == "1 to 9" or lastN == "6 to 9" or lastN == "7, 8 and 9" or lastN == "8 and 9" or lastN == "Only 9" ? 
     sellSetup == 9 : na, location=LocSNC, style=f_getStyleS(Sstyle), size=size.auto, color=color.new(color.maroon, 0), text="9", title="s9", textcolor=color.new(color.white, 15))