VWAP STD Bands for Thinkorswim

iMauiC

Member
I need a VWAP with multiple standard deviations. Where the level turns yellow when price is equal to that level and reverts back to grey whenever price leaves away from it?
 
Last edited by a moderator:

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

I have this VWAP with multiple standard deviations. Any way I can have someone help me code it to where the level turns yellow when price is equal to that level and reverts back to grey whenever price leaves away from it?

Code:
def numDevDn = -.618;
def numDevUp = .618;
def numDevDn1 = -1;
def numDevUp1 = 1;
def numDevDn2 = -1.5;
def numDevUp2 = 1.5;
def numDevDn3 = -2;
def numDevUp3 = 2;
def numDevDn4 = -2.5;
def numDevUp4 = 2.5;


input timeFrame = {default DAY, WEEK, MONTH};
input show_VWAP_label = Yes;
input VWAP_Label_Color_Choice = {default "magenta", "Green", "pink", "cyan", "orange", "red", "blue", "gray", "violet"};
input show_VWAP_bands_label = Yes;
input Bands_Label_Color_Choice = {default "magenta", "green", "pink", "cyan", "orange", "red", "blue", "gray", "violet"};


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.hide();

plot UpperBand = price + numDevUp * deviation;
UpperBand.hide();
plot LowerBand = price + numDevDn * deviation;
LowerBand.hide();
plot UpperBand1 = price + numDevUp1 * deviation;
UpperBand1.hide();
plot LowerBand1 = price + numDevDn1 * deviation;
LowerBand1.hide();
plot UpperBand2 = price + numDevUp2 * deviation;
UpperBand2.hide();
plot LowerBand2 = price + numDevDn2 * deviation;
LowerBand2.hide();
plot UpperBand3 = price + numDevUp3 * deviation;
UpperBand3.hide();
plot LowerBand3 = price + numDevDn3 * deviation;
LowerBand3.hide();
plot UpperBand4 = price + numDevUp4 * deviation;
UpperBand4.hide();
plot LowerBand4 = price + numDevDn4 * deviation;
LowerBand4.hide();


VWAP.setDefaultColor(getColor(0));

UpperBand.setDefaultColor(getColor(7));
LowerBand.setDefaultColor(getColor(7));
UpperBand1.setDefaultColor(getColor(7));
LowerBand1.setDefaultColor(getColor(7));
UpperBand2.setDefaultColor(getColor(7));
LowerBand2.setDefaultColor(getColor(7));
UpperBand3.setDefaultColor(getColor(7));
LowerBand3.setDefaultColor(getColor(7));
UpperBand4.setDefaultColor(getColor(7));
LowerBand4.setDefaultColor(getColor(7));




AddLabel(Show_VWAP_label, " VWAP = " + Round(VWAP, 2), GetColor(VWAP_Label_Color_Choice));
AddLabel(Show_VWAP_bands_label, " Upper Band = " + round(UpperBand3, 2) + "  /  " + " Lower Band = " + round(LowerBand3, 2), GetColor(Bands_Label_Color_Choice));

As using close equal to the bands will not likely last long, I have made an option to use a 'near' range around the band, where you input the near amount.

Once the close moves out of the near or equal options, the color will revert to gray.

To test the code, an input is added to hide_lines = yes; Set this to no, to hide the lines as in your original code.

Screenshot 2023-10-23 004324.png
Code:
input hide_lines = yes;

def numDevDn = -.618;
def numDevUp = .618;
def numDevDn1 = -1;
def numDevUp1 = 1;
def numDevDn2 = -1.5;
def numDevUp2 = 1.5;
def numDevDn3 = -2;
def numDevUp3 = 2;
def numDevDn4 = -2.5;
def numDevUp4 = 2.5;


input timeFrame = {default DAY, WEEK, MONTH};
input show_VWAP_label = Yes;
input VWAP_Label_Color_Choice = {default "magenta", "Green", "pink", "cyan", "orange", "red", "blue", "gray", "violet"};
input show_VWAP_bands_label = Yes;
input Bands_Label_Color_Choice = {"magenta", "green", "pink", "cyan", "orange", "red", "blue", default "gray", "violet"};


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.SetHiding(hide_lines);

plot UpperBand = price + numDevUp * deviation;
UpperBand.SetHiding(hide_lines);
plot LowerBand = price + numDevDn * deviation;
LowerBand.SetHiding(hide_lines);
plot UpperBand1 = price + numDevUp1 * deviation;
UpperBand1.SetHiding(hide_lines);
plot LowerBand1 = price + numDevDn1 * deviation;
LowerBand1.SetHiding(hide_lines);
plot UpperBand2 = price + numDevUp2 * deviation;
UpperBand2.SetHiding(hide_lines);
plot LowerBand2 = price + numDevDn2 * deviation;
LowerBand2.SetHiding(hide_lines);
plot UpperBand3 = price + numDevUp3 * deviation;
UpperBand3.SetHiding(hide_lines);
plot LowerBand3 = price + numDevDn3 * deviation;
LowerBand3.SetHiding(hide_lines);
plot UpperBand4 = price + numDevUp4 * deviation;
UpperBand4.SetHiding(hide_lines);
plot LowerBand4 = price + numDevDn4 * deviation;
LowerBand4.SetHiding(hide_lines);


VWAP.SetDefaultColor(GetColor(0));

UpperBand.SetDefaultColor(GetColor(7));
LowerBand.SetDefaultColor(GetColor(7));
UpperBand1.SetDefaultColor(GetColor(7));
LowerBand1.SetDefaultColor(GetColor(7));
UpperBand2.SetDefaultColor(GetColor(7));
LowerBand2.SetDefaultColor(GetColor(7));
UpperBand3.SetDefaultColor(GetColor(7));
LowerBand3.SetDefaultColor(GetColor(7));
UpperBand4.SetDefaultColor(GetColor(7));
LowerBand4.SetDefaultColor(GetColor(7));



input yellow_coloring = {default equal, near};
input near = .05;
AddLabel(show_VWAP_label, " VWAP = " + Round(VWAP, 2), GetColor(VWAP_Label_Color_Choice));

AddLabel(show_VWAP_bands_label, " Upper Band = " + Round(UpperBand3, 2) ,
if
    if yellow_coloring == yellow_coloring.near
    then Between(Round(close, 2), Round(UpperBand3, 2) - near, Round(UpperBand3, 2) + near)
    else Round(close, 2) == Round(UpperBand3, 2)
then Color.YELLOW
else GetColor(Bands_Label_Color_Choice));

AddLabel(show_VWAP_bands_label,  "  " + " Lower Band = " + Round(LowerBand3, 2),
if
    if yellow_coloring == yellow_coloring.near
    then Between(Round(close, 2), Round(LowerBand3, 2) - near, Round(LowerBand3, 2) + near)
    else  Round(close, 2) == Round(LowerBand3, 2)
then Color.YELLOW
else GetColor(Bands_Label_Color_Choice));

#
 
Last edited by a moderator:
As using close equal to the bands will not likely last long, I have made an option to use a 'near' range around the band, where you input the near amount.

Once the close moves out of the near or equal options, the color will revert to gray.

To test the code, an input is added to hide_lines = yes; Set this to no, to hide the lines as in your original code.
Wonderful Thank you! @SleepyZ. I appreciate it.
 
I have this VWAP with multiple standard deviations. Any way I can have someone help me code it to where the level turns yellow when price is equal to that level and reverts back to grey whenever price leaves away from it?

Code:
def numDevDn = -.618;
def numDevUp = .618;
def numDevDn1 = -1;
def numDevUp1 = 1;
def numDevDn2 = -1.5;
def numDevUp2 = 1.5;
def numDevDn3 = -2;
def numDevUp3 = 2;
def numDevDn4 = -2.5;
def numDevUp4 = 2.5;


input timeFrame = {default DAY, WEEK, MONTH};
input show_VWAP_label = Yes;
input VWAP_Label_Color_Choice = {default "magenta", "Green", "pink", "cyan", "orange", "red", "blue", "gray", "violet"};
input show_VWAP_bands_label = Yes;
input Bands_Label_Color_Choice = {default "magenta", "green", "pink", "cyan", "orange", "red", "blue", "gray", "violet"};


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.hide();

plot UpperBand = price + numDevUp * deviation;
UpperBand.hide();
plot LowerBand = price + numDevDn * deviation;
LowerBand.hide();
plot UpperBand1 = price + numDevUp1 * deviation;
UpperBand1.hide();
plot LowerBand1 = price + numDevDn1 * deviation;
LowerBand1.hide();
plot UpperBand2 = price + numDevUp2 * deviation;
UpperBand2.hide();
plot LowerBand2 = price + numDevDn2 * deviation;
LowerBand2.hide();
plot UpperBand3 = price + numDevUp3 * deviation;
UpperBand3.hide();
plot LowerBand3 = price + numDevDn3 * deviation;
LowerBand3.hide();
plot UpperBand4 = price + numDevUp4 * deviation;
UpperBand4.hide();
plot LowerBand4 = price + numDevDn4 * deviation;
LowerBand4.hide();


VWAP.setDefaultColor(getColor(0));

UpperBand.setDefaultColor(getColor(7));
LowerBand.setDefaultColor(getColor(7));
UpperBand1.setDefaultColor(getColor(7));
LowerBand1.setDefaultColor(getColor(7));
UpperBand2.setDefaultColor(getColor(7));
LowerBand2.setDefaultColor(getColor(7));
UpperBand3.setDefaultColor(getColor(7));
LowerBand3.setDefaultColor(getColor(7));
UpperBand4.setDefaultColor(getColor(7));
LowerBand4.setDefaultColor(getColor(7));




AddLabel(Show_VWAP_label, " VWAP = " + Round(VWAP, 2), GetColor(VWAP_Label_Color_Choice));
AddLabel(Show_VWAP_bands_label, " Upper Band = " + round(UpperBand3, 2) + "  /  " + " Lower Band = " + round(LowerBand3, 2), GetColor(Bands_Label_Color_Choice));
Do you use this on intraday timeframe (10 or 15 mnutes or hourly ? )
 
I use them on the 2min and 5min with the setting set to monthly.
I use them for levels and paired with fractals. Its a clean way to catch some reversals.

Here's this link. I did the whole workspace
https://tos.mx/moz83va
 
Last edited by a moderator:
@SleepyZ i tried creating a simple scan with this code but its not fetching any results . here is the condition igave on 10 minutes chart

close crosses below vwapSTD."LowerBand3" - Expecting it to bring the stocks which hit band 3 below vwap /

Added the highlighted code in the image to the script, saved as newstudy3338.

Then used a custom study to scan. It seemed to work with currently set using lowerband3
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
460 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