Triple VWAP Indicator for ThinkorSwim

BenTen

BenTen

Administrative
Staff
VIP
Regular VWAP indicator with additional standard deviations added. Now you can use the 1 and 3 Stdev Bands along with the default standard deviation bands (set to 2). Requested by a member on the forum.

There is also an advanced version called Ultimate VWAP by @Welkin. You should check it out here.



thinkScript Code

Code:
# VWAP(3)
# Assembled by BenTen at UseThinkScript.com

input numDevDn1 = -1.0;
input numDevUp1 = 1.0;
input numDevDn2 = -2.0;
input numDevUp2 = 2.0;
input numDevDn3 = -3.0;
input numDevUp3 = 3.0;
input showCloud = yes;

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 UpperBand1 = price + numDevUp1 * deviation;
plot LowerBand1 = price + numDevDn1 * deviation;
plot UpperBand2 = price + numDevUp2 * deviation;
plot LowerBand2 = price + numDevDn2 * deviation;
plot Upperband3 = price + numDevUp3 * deviation;
plot LowerBand3 = price + numDevDn3 * deviation;

VWAP.setDefaultColor(getColor(6));
UpperBand1.setDefaultColor(getColor(9));
UpperBand1.setStyle(Curve.SHORT_DASH);
LowerBand1.setDefaultColor(getColor(9));
LowerBand1.setStyle(Curve.SHORT_DASH);
UpperBand2.setDefaultColor(getColor(9));
LowerBand2.setDefaultColor(getColor(9));

AddCloud(if showCloud then UpperBand2 else Double.NaN, Upperband3, color.light_green, color.light_green);
AddCloud(if showCloud then LowerBand2 else Double.NaN, LowerBand3, color.light_red, color.light_red);
 
Last edited:

Similar threads

Top