```
# triggeronce_vwapx_00
# halcyonguy
# 21-07-01
# check if price crosses vwap
# on the 1st crossing, change variable trig1, from 0 to 1, and keep it set to 1
# IBM 5min
input showband_lines = no;
# vwap
# TD Ameritrade IP Company, Inc. (c) 2011-2021
input numDevDn = -2.0;
input numDevUp = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};
def cap = getAggregationPeriod();
def errorInAggregation =
timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
def yyyyMmDd = getYyyyMmDd();
def periodIndx;
switch (timeFrame) {
case DAY:
periodIndx = yyyyMmDd;
case WEEK:
periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
case MONTH:
periodIndx = roundDown(yyyyMmDd / 100, 0);
}
def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);
def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;
if (isPeriodRolled) {
volumeSum = volume;
volumeVwapSum = volume * vwap;
volumeVwap2Sum = volume * Sqr(vwap);
} else {
volumeSum = compoundValue(1, volumeSum[1] + volume, volume);
volumeVwapSum = compoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));
plot VWAP = price;
plot UpperBand = if showband_lines then (price + numDevUp * deviation) else double.nan;
plot LowerBand = if showband_lines then (price + numDevDn * deviation) else double.nan;
VWAP.setDefaultColor(getColor(0));
UpperBand.setDefaultColor(getColor(2));
LowerBand.setDefaultColor(getColor(4));
# -------------
# add code to keep a var set after a trigger
def bn = barnumber();
def levelbefore = if bn == 1 then close else levelbefore[1];
def x = if close crosses vwap then 1 else 0;
# draw an arrow on every crossing
plot arrows = x;
arrows.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
arrows.setDefaultColor(color.cyan);
arrows.setlineweight(4);
arrows.hidebubble();
# on the 1st crossing, chg a var and keep it changed
def trig1 = if bn == 1 then 0
else if (bn > 1 and trig1[1] == 1) then 1
else if x then 1
else trig1[1];
# draw a line to visualize trig1
input factor = 1.01;
plot stepline = if trig1 == 0 then levelbefore else ( levelbefore * factor);
stepline.setDefaultColor(color.cyan);
stepline.setlineweight(2);
#
```