VWAP Standard Deviation Bands for ThinkorSwim

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:
@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!
 
@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
 
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.
 
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.
 
@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);
 
Hello, does anyone know how can I extend the timeframes? For example, add quarterly and yearly timeframes; hope that you can help me with it, have a great day.
 
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
Hello tomsk,
I just found this code for vwap standard deviation bands and I really like it. But, the cloud colors in between the 1st and 2nd standard deviation (the dark gray) seems to cover the price action. I have been trying to figure out how to either bring the price to the front (like you can in TradingView) or simply make this area transparent. I am really only interested in the clouds from the 2nd deviation to the 3rd. Is there a simple way to change the code to do this or is it something more complex? Thanks for posting this, either way I will be using it.
 
Hi, I'm not sure if this exists but I looked all over and can't find it. I'm sure it's pretty easy but I'm not a coder and can't figure it out. I just want to color the candles green when price is trading above VWAP and red below VWAP. I think I need to add AssignPriceColor function to Vwap code but can't to figure it out. If anyone can help with this I would really appreciate it. Thank you!
 
Code:
def data = VWAP();

AssignPriceColor(if close > data then color.green else if close < data then color.red else color.gray);
that should get you started. I assign VWAP() to a variable so that I don't have to call it twice (for my two comparisons)... it's a little bit faster that way -- fewer system resources on your pc. Of course, you could also change the CLOSE to be HL2 if you want the average of the candle, HLC3, or even OHLC4 depending on how you want to define price. :cool:

-mashume
 
Code:
def data = VWAP();

AssignPriceColor(if close > data then color.green else if close < data then color.red else color.gray);
that should get you started. I assign VWAP() to a variable so that I don't have to call it twice (for my two comparisons)... it's a little bit faster that way -- fewer system resources on your pc. Of course, you could also change the CLOSE to be HL2 if you want the average of the candle, HLC3, or even OHLC4 depending on how you want to define price. :cool:

-mashume
@mashume Thank you so much for your help! Do I add this to the VWAP indicator or do I create a new indicator? It's not working for some reason? I'm not sure what I'm doing wrong?
 
@mashume Thank you so much for your help! Do I add this to the VWAP indicator or do I create a new indicator? It's not working for some reason? I'm not sure what I'm doing wrong?
It is it's own indicator. Create a new one, paste the code in, should run. I think I had it as an upper study, but it shouldn't matter.

-mashume
 
unknown.png


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 dev = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

input show_cloud = yes;
input length = 20;
input numDevDn = -1.28;
input numDevUp = 1.28;
input averageType = AverageType.EXPONENTIAL;

plot UpperBand = price + numDevUp * dev;
plot MidLine = price;
plot LowerBand = price + numDevDn * dev;

UpperBand.SetDefaultColor(GetColor(5));
LowerBand.SetDefaultColor(GetColor(6));

input show_cloud2 = yes;
input length2 = 20;
input numDevDn2 = -2.01;
input numDevUp2 = 2.01;
input averageType2 = AverageType.EXPONENTIAL;

def price2 = volumeVwapSum / volumeSum;
def dev2 = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

plot UpperBand2 = price2 + numDevUp2 * dev2;

plot LowerBand2 = price2 + numDevDn2 * dev2;

UpperBand2.SetDefaultColor(GetColor(5));
LowerBand2.SetDefaultColor(GetColor(6));
# Outer bands
input show_cloud3 = yes;
input length3 = 20;
input numDevDn3 = -2.51;
input numDevUp3 = 2.51;
input averageType3 = AverageType.EXPONENTIAL;

def price3 = volumeVwapSum / volumeSum;
def dev3 = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

plot UpperBand3 = price3 + numDevUp3 * dev3;
#plot MidLineob = avgob;
plot LowerBand3 = price3 + numDevDn3 * dev3;

UpperBand3.SetDefaultColor(GetColor(5));
LowerBand3.SetDefaultColor(GetColor(6));

input show_cloud4 = yes;
input length4 = 20;
input numDevDn4 = -3.09;
input numDevUp4 = 3.09;
input averageType4 = AverageType.EXPONENTIAL;

def price4 = volumeVwapSum / volumeSum;
def dev4 = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

plot UpperBand4 = price4 + numDevUp4 * dev4;
plot LowerBand4 = price4 + numDevDn4 * dev4;

UpperBand4.SetDefaultColor(GetColor(5));
LowerBand4.SetDefaultColor(GetColor(6));

input show_cloud5 = yes;
input length5 = 20;
input numDevDn5 = -4.01;
input numDevUp5 = 4.01;
input averageType5 = AverageType.EXPONENTIAL;

def price5 = volumeVwapSum / volumeSum;
def dev5 = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

plot UpperBand5 = price5 + numDevUp5 * dev5;
plot LowerBand5 = price5 + numDevDn5 * dev5;

UpperBand5.SetDefaultColor(GetColor(5));
LowerBand5.SetDefaultColor(GetColor(6));


AddCloud( if show_cloud then midline else double.nan,upperband2, Color.lIGHT_GRAY, Color.lIGHT_GRAY);
AddCloud(if show_cloud then midline else double.nan, lowerband, Color.GRAY, Color.GRAY);

AddCloud( if show_cloud2 then upperband else double.nan,upperband2, Color.LIGHT_RED, Color.LIGHT_RED);
AddCloud(if show_cloud3 then upperband2 else double.nan,upperband3, Color.LIGHT_RED, Color.LIGHT_RED);
AddCloud( if show_cloud4 then upperband3 else double.nan,upperband4, Color.LIGHT_RED, Color.LIGHT_RED);
AddCloud( if show_cloud5 then upperband4 else double.nan,upperband5, Color.LIGHT_RED, Color.LIGHT_RED);
AddCloud(if show_cloud2 then lowerband else double.nan,lowerband2, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud(if show_cloud3 then lowerband2 else double.nan,lowerband3, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud(if show_cloud4 then lowerband3 else double.nan,lowerband4, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud( if show_cloud5 then lowerband4 else double.nan,lowerband5, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
 
Heyhey,

I wanted to share a simple indicator I sliced together showing clouds of .5, 1 and 1.5 standard deviations from our beloved Vwap.

I messed with the RGB colors until I got them to look pretty smooth against my background color which is (53,53,53)

This really helps give structure to the market and gives great ares to risk off.

Happy Trading
-G

PudUG0u.jpg




####Vwap Standard Deviation Clouds###

input devdn= -.5;
input devup = .5;
input timeFrame = {default DAY, WEEK, MONTH};

input devdn1 = - 1;
input devup1 = 1;
input timeFrame1 = {default DAY, WEEK, MONTH};

input devdn2 = -1.5;
input devup2 = 1.5;
input timeFrame2 = {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;
vwap.assignValueColor(if close>vwap then color.green else if close<vwap then color.red else color.current);

plot high= price + devup * deviation;
plot low= price + devdn * deviation;

plot highhigh = price + devup1 * deviation;
plot lowlow = price + devdn1 * deviation;

plot higherhigh= price + devup2 * deviation;
plot lowerlow = price + devdn2 * deviation;




AddCloud( highhigh, higherhigh, CreateColor(75,100,75), CreateColor(75,100,75));
AddCloud( lowlow, lowerlow, CreateColor(100,75,75), CreateColor(100,75,75));

#AddCloud(high, higherhigh, CreateColor(35,35,35), CreateColor(35, 35,35));
#AddCloud( low, lowerlow, CreateColor(55,55,55), CreateColor(55,55,55));

AddCloud( high, highhigh, CreateColor(75,80,75), CreateColor(75,80,75));

AddCloud( low, lowlow, CreateColor(80,75,75), CreateColor(80,75,75));
 
Heyhey,

I wanted to share a simple indicator I sliced together showing clouds of .5, 1 and 1.5 standard deviations from our beloved Vwap.

I messed with the RGB colors until I got them to look pretty smooth against my background color which is (53,53,53)

This really helps give structure to the market and gives great ares to risk off.

Happy Trading
-G

PudUG0u.jpg




####Vwap Standard Deviation Clouds###

input devdn= -.5;
input devup = .5;
input timeFrame = {default DAY, WEEK, MONTH};

input devdn1 = - 1;
input devup1 = 1;
input timeFrame1 = {default DAY, WEEK, MONTH};

input devdn2 = -1.5;
input devup2 = 1.5;
input timeFrame2 = {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;
vwap.assignValueColor(if close>vwap then color.green else if close<vwap then color.red else color.current);

plot high= price + devup * deviation;
plot low= price + devdn * deviation;

plot highhigh = price + devup1 * deviation;
plot lowlow = price + devdn1 * deviation;

plot higherhigh= price + devup2 * deviation;
plot lowerlow = price + devdn2 * deviation;




AddCloud( highhigh, higherhigh, CreateColor(75,100,75), CreateColor(75,100,75));
AddCloud( lowlow, lowerlow, CreateColor(100,75,75), CreateColor(100,75,75));

#AddCloud(high, higherhigh, CreateColor(35,35,35), CreateColor(35, 35,35));
#AddCloud( low, lowerlow, CreateColor(55,55,55), CreateColor(55,55,55));

AddCloud( high, highhigh, CreateColor(75,80,75), CreateColor(75,80,75));

AddCloud( low, lowlow, CreateColor(80,75,75), CreateColor(80,75,75));
Looks great! Any tips on how to use it and have you had any success with it?
 
Looks great! Any tips on how to use it and have you had any success with it?
Appreciate that! I'm always looking for price to tap the 1.5/2 stdev area.. It usually doesn't hang out there too long. I'm definitely a mean reversion type trader and always looking for price to become over extended in either direction. I like watching how price reacts to each level and just follow the volume and take scalps with the momentum.
I run several other studies not pictured that work in conjunction with the st dev levels that help the decision making process.
If you trade with the vwap clouds let me know how it works for you and if you have any ideas on how to improve or any success you find with it.
 
@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);

Is there any way to show this study on the current day only? I added the code below but it didn't seem to have any effect.

Ruby:
input ShowTodayOnly={"No", default "Yes"};
def Today = if GetLastDay() == GetDay() then 1 else 0;
 
@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);
Realtime Interactivity with my charts is primary when I trade with success: I personally use VWAP as a"faultline thread" that manifests during the intraday price discovery and formation of the price matrix. ( my share is that I find this particular VWAP Std Dev.indicator is a useful simple overview and overlay for my intraday chart portfolio. Very much appreciate the thoughtfulness and generosity of the contributors and esp Ben ( the Admin Mod). My little contribution is that if you are so inclined you might consider trying 2 things that I personally find useful and real time interactive: an intraday anchored Vwap at my entry pt immediately upon taking a position so I am able to observe my position in reference to the price discovery and react (hopefully)with precision and more deliberately in considering my risk/reward profile and importantly adjust it as I look at the unfolding development. and 2. adding to the chart (either upper price panel or a lower panel to keep the price discovery less cluttered- the Cum Tick indicator for either SPY or another Index. The Cum Tick (the slope! of the Cum Tick) acts as a correlate for the mass of overall market action and helps me personally stay focused and avoid being attached to my position as the mass of executed trades occurs. If the LinRegcurve slope on the CumTick and Vwap is directional in favor of my position it gives me empirical interactive feedback that allows me not to crash the MagicQuotes personal trading Plane.(U can configure LinRegCurve for VWAP)
 
@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);
@BenTen
How can i create a strategy and back test with this script, when price cross and close above and below 2nd deviation to go long and short, thanks in advance

# Define strategy def longEnterPrice = if price crosses above LowerBand2 then price else double.nan; def longExitPrice = if close crosses below LowerBand2 or price == VWAP then close else double.nan; AddOrder(OrderType.BUY, longEnterPrice, 100, color.green, "Long Entry"); AddOrder(OrderType.SELL, longExitPrice, 100, color.red, "Long Exit"); # Define short strategy def shortEnterPrice = if price crosses below UpperBand2 then price else double.nan; def shortExitPrice = if price crosses above UpperBand2 or price == VWAP then price else double.nan; AddOrder(OrderType.SELL_SHORT, shortEnterPrice, 100, color.red, "Short Entry"); AddOrder(OrderType.BUY_TO_COVER, shortExitPrice, 100, color.green, "Short Exit");

This is what I came up with so far, Im getting errors
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
407 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top