```
# VWAP w/ MovingAvgCrossover
#Created by TradebyDay
#
input price1 = close;
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;
input length = 50;
input length2 = 50;
input length3 = 50;
input displace = 0;
assert(length > 0, "'length' must be positive: " + length);
def EMA = compoundValue(1, EMA[1] + 2 / (length + 1) * (price1[-displace] - EMA[1]), price1[-displace]);
def EMA2 = compoundValue(1, EMA2[1] + 2 / (length2 + 1) * (price1[-displace] - EMA2[1]), price1[-displace]);
def EMA3 = compoundValue(1, EMA3[1] + 2 / (length3 + 1) * (price1[-displace] - EMA3[1]), price1[-displace]);
plot LegacyEMA = EMA;
plot LegacyEMA2 = EMA2;
plot LegacyEMA3 = EMA3;
VWAP.setDefaultColor(getColor(0));
LegacyEMA.SetDefaultColor(GetColor(1));
LegacyEMA2.SetDefaultColor(GetColor(2));
LegacyEMA3.SetDefaultColor(GetColor(3));
# End Code
```