```
declare hide_on_daily;
input numDevDn = -2.0;
input numDevUp = 2.0;
input shouldPlotDevDn = yes;
input shouldPlotDevUp = yes;
# The default is TOS' vwap, but could use CLOSE, OHLC4, or some other price.
input vwap_Type = vwap;
input anchorTimeEastern = 2100;
# This specifies whether the current time is after the anchor time in
# the current calendar day (not after the anchor time within
# the current trading day).
def afterAnchorTime = SecondsFromTime(anchorTimeEastern) >= 0;
# Counts the number of bars since the previous trading day.
# The first bar of the new day will equal 1.
# Iterates from the current bar backward up to 1600 bars (more
# than then number of minutes in a day) counting each bar until
# the current bar is a different trading day.
def barCountFromPreviousTradingDay =
fold relativeBarIndex = 0 to 1600
with barCount = 1
while GetValue(GetYYYYMMDD(), relativeBarIndex) == GetValue(GetYYYYMMDD(), relativeBarIndex + 1)
do barCount + 1;
# Counts the number of bars since the anchor time.
# The first bar whose start time is greater than or equal
# to the anchor time will equal zero.
def barCountFromAnchorTime =
fold relativeBarIndex_at = 0 to 1600
with barCount_fromAnchorTime
while GetValue(SecondsFromTime(anchorTimeEastern), relativeBarIndex_at) > 0
do barCount_fromAnchorTime + 1;
# Since afterAnchorTime only checks to see if the current bar is after the anchor
# time of the current calendar day, this variable is needed to track whether after
# the anchor time within the current trading day (which includes regular and
# extended hours). To be true, the bar has to be after the anchor time of the current
# calendar day and that anchor time cannot be in the previous trading day.
# To account for anchor times in the previous calendar day that are not in the
# previous trading day (i.e. valid to plot), the second line will continue plotting
# once started until the trading day changes.
def shouldPlot_VWAP =
(afterAnchorTime and (barCountFromPreviousTradingDay > barCountFromAnchorTime)) or
(shouldPlot_VWAP[1] and barCountFromPreviousTradingDay != 1);
# If previous bar didn't plot VWAP, but current bar should, then this is the
# first bar after a valid anchor time. The OR conditional is needed to handle
# the situation where the anchor time is the first bar of the trading day,
# because the previous bar would have been plotted as well in that scenario.
def isFirstBarAfterAnchorTime =
CompoundValue(
1,
(shouldPlot_VWAP != shouldPlot_VWAP[1]) or (shouldPlot_VWAP and barCountFromPreviousTradingDay == 1),
yes);
def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;
# Basically TOS' VWAP study calculation below
if (isFirstBarAfterAnchorTime) {
volumeSum = volume;
volumeVwapSum = volume * vwap_Type;
volumeVwap2Sum = volume * Sqr(vwap_Type);
} else {
volumeSum = CompoundValue(1, volumeSum[1] + volume, volume);
volumeVwapSum = CompoundValue(1, volumeVwapSum[1] + volume * vwap_Type, volume * vwap_Type);
volumeVwap2Sum = CompoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap_Type), volume * Sqr(vwap_Type));
}
plot VWAP = if shouldPlot_VWAP then (volumeVwapSum / volumeSum) else Double.NaN;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(VWAP), 0));
#plot VWAP = price;
plot UpperBand = if shouldPlotDevUp then VWAP + numDevUp * deviation else Double.NaN;
plot LowerBand = if shouldPlotDevDn then VWAP + numDevDn * deviation else Double.NaN;
VWAP.SetDefaultColor(Color.CYAN);
UpperBand.SetDefaultColor(Color.GRAY);
LowerBand.SetDefaultColor(Color.GRAY);
VWAP.SetLineWeight(3);
UpperBand.SetLineWeight(1);
LowerBand.SetLineWeight(1);
```