VWAP Standard Deviation Bands for ThinkorSwim

T

tomsk

Well-known member
VIP
Vide an earlier user request, I posted the following study in one of the sub forums, relocating it here for easier access/search

Code:
# VWAP Standard Deviation Bands
# lar
# 12.12.2015

# V1.0 - 12.12.2015 - lar     - Initial release of VWAP Standard Deviation bands
# V1.1 - 12.17.2019 - tomsk   - Minor edits

input timeFrame = {default Day, Week, Month, Year, Minute, Hour, Chart, "Opt Exp", Bar};
input BandType = {default Standard, "1/4 Day Range", "3x Avg Bar Range", ThinkScripter, None};
input ShowCloud = yes;
input HideSdLines = no;

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 seconds = SecondsFromTime(0);
def month = GetYear() * 12 + GetMonth();
def year = GetYear();
def day_number = DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd));
def dom = GetDayOfMonth(yyyyMmDd);
def dow = GetDayOfWeek(yyyyMmDd - dom + 1);
def expthismonth = (if dow > 5 then 27 else 20) - dow;
def exp_opt = month + (dom > expthismonth);
def periodIndx;

switch (timeFrame) {
case Chart:
    periodIndx = 0;

case Minute:
    periodIndx = Floor(seconds / 60 + day_number * 24 * 60);

case Hour:
    periodIndx = Floor(seconds / 3600 + day_number * 24);

case Day:
    periodIndx = CountTradingDays(Min(First(yyyyMmDd), yyyyMmDd), yyyyMmDd) - 1;

case Week:
    periodIndx = Floor(day_number / 7);

case Month:
    periodIndx = Floor(month - First(month));

case Year:
    periodIndx = Floor(year - First(year));

case "Opt Exp":
    periodIndx = exp_opt - First(exp_opt);

case Bar:
    periodIndx = BarNumber() - 1;
}
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;
switch (BandType) {

case Standard:
    deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

case "1/4 Day Range":
    deviation = Sqrt(AbsValue(high(Period = timeFrame) - low(Period = timeFrame)) * .25);

case "3x Avg Bar Range":
    deviation = Sqrt(Average(TrueRange(high,  close,  low),  20) * 3);

case ThinkScripter:
    deviation = Sqrt(TotalSum(Sqr(((open + high + low + close) / 4) - price) * volume) / TotalSum(volume));

case None:
    deviation = Double.NaN;
}

plot VWAP = price;
VWAP.AssignValueColor(if VWAP > VWAP[1] then Color.Cyan
                      else if VWAP < VWAP[1] then Color.Red
                      else Color.Yellow);
VWAP.SetStyle(Curve.SHORT_DASH);
VWAP.SetLineWeight(2);

# TS_CHART_VWAP_SD_BANDS
# http://www.thinkscripter.com
# [email protected]
# Last Update 03 APR 2010

input VWAPStdev1 = 1.0;
input VWAPStdev2 = 2.0;
input VWAPStdev3 = 3.0;

plot r1 = VWAP + VWAPStdev1 * deviation;
plot s1 = VWAP - VWAPStdev1 * deviation;
plot r2 = VWAP + VWAPStdev2 * deviation;
plot s2 = VWAP - VWAPStdev2 * deviation;
plot r3 = VWAP + VWAPStdev3 * deviation;
plot s3 = VWAP - VWAPStdev3 * deviation;

DefineGlobalColor("sDev1", (CreateColor(40, 40, 40)));
DefineGlobalColor("sDev2", (CreateColor(128, 128, 128)));
DefineGlobalColor("sDev3", (CreateColor(100, 100, 100)));

r1.SetDefaultColor(GlobalColor("sDev1"));
s1.SetDefaultColor(GlobalColor("sDev1"));
r2.SetDefaultColor(GlobalColor("sDev2"));
s2.SetDefaultColor(GlobalColor("sDev2"));
r3.SetDefaultColor(GlobalColor("sDev2"));
s3.SetDefaultColor(GlobalColor("sDev2"));

r1.SetStyle(Curve.SHORT_DASH);
r2.SetStyle(Curve.SHORT_DASH);
r3.SetStyle(Curve.SHORT_DASH);
s1.SetStyle(Curve.SHORT_DASH);
s2.SetStyle(Curve.SHORT_DASH);
s3.SetStyle(Curve.SHORT_DASH);

VWAP.HideBubble();
r1.HideBubble();
r2.HideBubble();
r3.HideBubble();
s1.HideBubble();
s2.HideBubble();
s3.HideBubble();

r1.SetHiding(HideSdLines);
r2.SetHiding(HideSdLines);
r3.SetHiding(HideSdLines);
s1.SetHiding(HideSdLines);
s2.SetHiding(HideSdLines);
s3.SetHiding(HideSdLines);

# CLOUD ##########################
def CloudR1 = if ShowCloud then r1 else Double.NaN;
def CloudR2 = if ShowCloud then r2 else Double.NaN;
def CloudR3 = if ShowCloud then r3 else Double.NaN;
def CloudS1 = if ShowCloud then s1 else Double.NaN;
def CloudS2 = if ShowCloud then s2 else Double.NaN;
def CloudS3 = if ShowCloud then s3 else Double.NaN;
AddCloud(CloudR1, CloudR2, GlobalColor("sDev1"), GlobalColor("sDev1"));
AddCloud(CloudS1, CloudS2, GlobalColor("sDev1"), GlobalColor("sDev1"));
AddCloud(CloudR2, CloudR3, GlobalColor("sDev2"), GlobalColor("sDev2"));
AddCloud(CloudS2, CloudS3, GlobalColor("sDev2"), GlobalColor("sDev2"));
# End VWAP Standard Deviation Bands
 
Last edited:
markos

markos

Well-known member
VIP
@tomsk thanks for posting this. Don't know how I missed this. If you run into LAR on occasion, please be sure to say hi!
 
C

cherif

Member
@tomsk . I would love to have labels displaying the vwap and stdv1/2/3 . Could you help with the coding please??thx
 
P

PaulB

New member
@tomsk . Very useful. Thank you! Can you tell me if it's possible to plot your study on a /ES chart, but displaying SPY's VWAP data? Or even just the SPY VWAP (without the standard deviation bands as that might be asking too much) on an /ES chart. Preferably on a tick chart, but 1 minute and up would be useful. Thanks! ....and sorry for the loss of your mother......paul
 
B

badger-man

New member
Hi there,

First time poster so excuse any lapse of etiquette.

Wondering if anyone has come across a custom VWAP indicator that supports more than just two standard deviations, e.g. at -2, -1, 1 and 2.

Thank you for any input you can provide.
 
markos

markos

Well-known member
VIP
Hi there,

First time poster so excuse any lapse of etiquette.

Wondering if anyone has come across a custom VWAP indicator that supports more than just two standard deviations, e.g. at -2, -1, 1 and 2.

Thank you for any input you can provide.
Have you typed in VWAP in the Search? Also, there are a number of them in the One Note located in the Tutorials Section. Good luck.
 
BenTen

BenTen

Administrative
Staff
VIP
@badger-man Here you go:

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

input numDevDn = -2.0;
input numDevUp = 2.0;
input numDevDn1 = -1.0;
input numDevUp1 = 1.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 UpperBand2 = price + numDevUp * deviation;
plot LowerBand2 = price + numDevDn * deviation;
plot UpperBand1 = price + numDevUp1 * deviation;
plot LowerBand1 = price + numDevDn1 * 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 UpperBand1 else Double.NaN, Upperband2, color.light_green, color.light_green);
AddCloud(if showCloud then LowerBand1 else Double.NaN, LowerBand2, color.light_red, color.light_red);
 

Similar threads

Top