VWAP & Previous VWAP - MTF
// 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 m-tf • Yata", 
     overlay = true)

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

computeVWAP(src, isNewPeriod) =>
	var float sumSrcVol = na
	var float sumVol = na
    var float sumSrcSrcVol = na
    var float _lvwap = 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)
	_lvwap := isNewPeriod ? _vwap[1] : _lvwap[1]

	[_vwap, stDev, _lvwap]

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

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)

pvD_color = input.color(color.new(#E12D7B, 75), title="", inline="V1", group=groupVWAP)
pvW_color = input.color(color.new(#F67B52, 75), title="", inline="V2", group=groupVWAP)
pvM_color = input.color(color.new(#EDCD3B, 75), title="", inline="V3", group=groupVWAP)
pvQ_color = input.color(color.new(#3BBC54, 75), title="", inline="V4", group=groupVWAP)
pvY_color = input.color(color.new(#2665BD, 75), title="", inline="V5", group=groupVWAP)

plot_pvD = input(true, title="Prev."					, inline="V1", group=groupVWAP)
plot_pvW = input(true, title="Prev."					, inline="V2", group=groupVWAP)
plot_pvM = input(true, title="Prev."					, inline="V3", group=groupVWAP)
plot_pvQ = input(true, title="Prev."					, inline="V4", group=groupVWAP)
plot_pvY = input(true, title="Prev."					, inline="V5", 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(true, title="Show Quarterly VWAP"		, inline="V4", group=groupVWAP)
plot_vY = input(true, title="Show Yearly VWAP"			, inline="V5", group=groupVWAP)

vR_color		= input.color(color.new(#481899, 50)	, title=""					, inline="V6", group=groupVWAP)
rolling_sv		= input(true							, 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)

line_width_VWAP = input.int(1, minval=0, title="Lines Width", inline="V0A", group=groupVWAP)
line_width_pVWAP = line_width_VWAP

Vstyle = input(true, title="Circles Style", inline="V0A", group=groupVWAP)

VstyleC = Vstyle ? plot.style_circles : plot.style_line
VstylepC = Vstyle ? plot.style_circles : plot.style_stepline

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

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

off_mult = input(15, title="Labels 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, pvD] = computeVWAP(src, newSessionD)
[vW, stdevW, pvW] = computeVWAP(src, newSessionW)
[vM, stdevM, pvM] = computeVWAP(src, newSessionM)
[vQ, stdevQ, pvQ] = computeVWAP(src, newSessionQ)
[vY, stdevY, pvY] = computeVWAP(src, newSessionY)

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

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

vDplot = plot(plot_vD ? vD : na, title="VWAP - Daily"	, color=vD_color, style=VstyleC, linewidth=line_width_VWAP)
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=line_width_VWAP)
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=line_width_VWAP)
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=line_width_VWAP)
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=line_width_VWAP)
f_drawLabel(ll_offset, show_labels and show_VWAPlabels and plot_vY ? vY : na, "vY", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

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

pvDplot = plot(plot_pvD ? pvD : na, title="pVWAP - Daily"	, color=pvD_color, style=VstylepC, linewidth=line_width_pVWAP)
f_drawLabel(ll_offset, show_labels and show_pVWAPlabels and plot_pvD ? pvD : na, "pvD", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

pvWplot = plot(plot_pvW ? pvW : na, title="pVWAP - Weekly"	, color=pvW_color, style=VstylepC, linewidth=line_width_pVWAP)
f_drawLabel(ll_offset, show_labels and show_pVWAPlabels and plot_pvW ? pvW : na, "pvW", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

pvMplot = plot(plot_pvM ? pvM : na, title="pVWAP - Monthly"	, color=pvM_color, style=VstylepC, linewidth=line_width_pVWAP)
f_drawLabel(ll_offset, show_labels and show_pVWAPlabels and plot_pvM ? pvM : na, "pvM", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

pvQplot = plot(plot_pvQ ? pvQ : na, title="pVWAP - Quarter"	, color=pvQ_color, style=VstylepC, linewidth=line_width_pVWAP)
f_drawLabel(ll_offset, show_labels and show_pVWAPlabels and plot_pvQ ? pvQ : na, "pvQ", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

pvYplot = plot(plot_pvY ? pvY : na, title="pVWAP - Year"	, color=pvY_color, style=VstylepC, linewidth=line_width_pVWAP)
f_drawLabel(ll_offset, show_labels and show_pVWAPlabels and plot_pvY ? pvY : na, "pvY", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)