VWAP Label with Upper/Lower Bands for ThinkorSwim

J007RMC

Well-known member
2019 Donor
I want to display the VWAP line and its label to show the current VWAP value on my chart. Is it possible to add VWAP as a label in ThinkorSwim?

Code:
#HINT: The Volume-Weighted Average Price (VWAP) is calculated where size x is the volume traded at price x.
# The VWAP plot is accompanied with two bands serving as overbought and oversold levels. The Upper band (overbought level) is plotted a specified number of standard deviations above the VWAP, and the Lower band (oversold level) is plotted similarly below the VWAP. Standard deviations are based upon the difference between the price and VWAP.
input numDevDn = -2.0;
input numDevUp = 2.0;
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 = no;
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();
 
Is this what you wanted?

Code:
#HINT: The Volume-Weighted Average Price (VWAP) is calculated where size x is the volume traded at price x.

# The VWAP plot is accompanied with two bands serving as overbought and oversold levels. The Upper band (overbought level) is plotted a specified number of standard deviations above the VWAP, and the Lower band (oversold level) is plotted similarly below the VWAP. Standard deviations are based upon the difference between the price and VWAP.

input numDevDn = -2.0;
input numDevUp = 2.0;
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 = no;
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();

VWAP.setDefaultColor(getColor(0));
UpperBand.setDefaultColor(getColor(2));
LowerBand.setDefaultColor(getColor(4));

AddLabel(Show_VWAP_label, " VWAP = " + Round(VWAP, 2), GetColor(VWAP_Label_Color_Choice));
AddLabel(Show_VWAP_bands_label, " VWAP Upper Band = " + round(UpperBand, 2) + " ... " + " VWAP Lower Band = " + round(LowerBand, 2), GetColor(Bands_Label_Color_Choice));
 

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

Hello, I'm looking for some help with a VWAP AddLabel indicator that will change colors once outside the bands. I have duplicated the original VWAP code and built on this from another script that was working similarly. However, I have messed something up. Help please. Thank you.

Code:
VWAP.SetDefaultColor(GetColor(0));
UpperBand.SetDefaultColor(GetColor(2));
LowerBand.SetDefaultColor(GetColor(4));

# My piecemeal code begins here #
def VWAP_Above_Upper = if price + numDevUp * deviation > UpperBand then 1 else 0;
def VWAP_Below_Lower = if price + numDevUp * deviation < LowerBand then 1 else 0;
def VWAP_Between = If price >= VWAP_Above_Upper and price <= VWAP_Below_Lower then 1 else 0;
AddLabel(VWAP_Above_Upper, "VWAP HI:" + price + numDevUp * deviation , COLOR. RED);
AddLabel(VWAP_Below_Lower,"VWAP LO:" + price + numDevUp * deviation, COLOR. LIGHT_RED);
AddLabel(VWAP_Between,"VWAP IN:" + price + numDevUp * deviation, COLOR. GREEN);
 
Hello, I'm looking for some help with a VWOP AddLabel indicator that will change colors once outside the bands. I have duplicated the original VWAP code and built on this from another script that was working similarly. However, I have messed something up. Help please. Thank you.

@Gogat - Based on the info you gave, I re-duplicated the entire built-in Thinkorswim VWAP and added a label. The label shows the values of the VWAP, the UpperBand, and the LowerBand. When price moves above or below the upper or lower bands, the label will change colors and display "Price Is Above UpperBand" or "Price Is Below LowerBand". I did not test the label to make sure it displays correctly. If it does'nt, or if this not is what you wanted, please let me know. :)


Code:
# VWAP
# TD Ameritrade IP Company, Inc. (c) 2011-2020
# Added labels to show the VWAP values and change colors when price is above or below the upper or lower bands

input numDevDn = -2.0;
input numDevUp = 2.0;
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 UpperBand = price + numDevUp * deviation;
plot LowerBand = price + numDevDn * deviation;

VWAP.setDefaultColor(getColor(0));
UpperBand.setDefaultColor(getColor(2));
LowerBand.setDefaultColor(getColor(4));

#Labels for showing where price is in relation to the VWAP lines
#---------------------------------------------------------------
input vwap_values_label = yes;
input price_location_label = yes;

DefineGlobalColor("Above UpperBand", color.red);     
DefineGlobalColor("Between Bands", color.green);
DefineGlobalColor("Below LowerBand", color.light_red);

AddLabel(vwap_values_label, if close > UpperBand then "VWAP HI: " + Round(UpperBand)
    else if close >= LowerBand then "VWAP IN: " + Round(price) else "VWAP LO: " + Round(LowerBand),
    (if close > UpperBand then GlobalColor("Above UpperBand") else if close >=
    LowerBand then GlobalColor("Between Bands") else GlobalColor("Below LowerBand")));

AddLabel(price_location_label, if close > UpperBand then "Price Is Above UpperBand"
    else if close >= LowerBand then "Price Is Between Bands" else "Price Is Below LowerBand",
    (if close > UpperBand then GlobalColor("Above UpperBand") else if close >=
    LowerBand then GlobalColor("Between Bands") else GlobalColor("Below LowerBand")));
 
#end code
 
Last edited:
It changes color perfectly! Small adjustments for what I hope to accomplish. I want to be able to see the color change at candle close, or have the option to see live vs. candle close. Also hoping to use it as a snapshot indicator, only having one of the text indicators showing. So if the price were above VWAP on candle close it would be red and show the designated text for above VWAP only. If it were within VWAP range it would show as Green with designated text only, etc. Opposed showing the UpperBand, Mid VWAP, and LowerBand levels.

What you made is amazing! Thank you. I appreciate your help.
 
@Gogat, I have updated the above code to now plot two labels so you can display whichever you prefer, you can turn them on/off in the user settings.

I want to be able to see the color change at candle close, or have the option to see live vs. candle close.

I'm not certain what this means. In thinkscript, current (live) price is called "close". Do you mean that you only want the label color to change if the candle closes above or below the bands, and not change color until then?
 
This is extremely close to my final vision for the indicator! Amazing, Thank you so much. Okay, I clearly do not understand how the code works for TOS. I have tried to adjust a few things to allow me to select options from the settings menu with no avail. I tried to create a menu option to adjust if the indicator shows the Mid VWAP level, or the to the band price closest to where the price action is out of the bands. Attaching my last bit of code, hoping you might be able to help me solve the problems.

Code:
#Labels for showing where price is in relation to the VWAP lines
#---------------------------------------------------------------
input VWAP_Values_Label = no;
input Price_Color_Label = yes;
input VWAP_Price_Label = {VWAP_Price, Band_Prices};

def VWAP_Price;
switch (diffType) 
{
case VWAP_Price: 
    VWAP_Price_In = Round(price);
    VWAP_Price_Upper = Round(UpperBand);
    VWAP_Price_Lower = Round(LowerBand);
case Band_Prices:
   VWAP_Price_In = Round(price);
    VWAP_Price_Upper = Round(price);
    VWAP_Price_Lower = Round(price);
}

DefineGlobalColor("Above UpperBand", Color.RED);
DefineGlobalColor("Between Bands", Color.GREEN);
DefineGlobalColor("Below LowerBand", Color.LIGHT_RED);

AddLabel(VWAP_Values_Label, "UpperBand: " + Round(UpperBand) + "  ---  " +
    "VWAP: " + Round(price) + "  ---  " + "LowerBand: " + Round(LowerBand),
    (if close > UpperBand then GlobalColor("Above UpperBand") else if close >=
    LowerBand then GlobalColor("Between Bands") else GlobalColor("Below LowerBand")));

AddLabel(Price_Color_Label, if close > UpperBand then "VWAP HI: " + VWAP_Price_Upper
    else if close >= LowerBand then "VWAP IN: " + VWAP_Price_In else "VWAP LO: " + VWAP_Price_Lower,
    (if close > UpperBand then GlobalColor("Above UpperBand") else if close >=
    LowerBand then GlobalColor("Between Bands") else GlobalColor("Below LowerBand")));

#end code

Thanks again for all your help on this @Pensar! It is really looking great for what I wanted.

Yes, initially I was thinking it would only change color if it closed above or below, as to give me more confirmation. But I think live is works just as well.
 
@Gogat, I have again changed the above code in post #2, hopefully it now shows what you want. :) The label will now display the upper VWAP value if price is outside the upper VWAP band, or the lower VWAP value if price is outside the lower VWAP band. When inside the upper or lower VWAP bands, the label will show the mid VWAP value.
 
I am looking to use addLabel to show the price difference of the current price - the center line vwap value. I would like it to show current price - vwap rounded to 2 decimals. I am really struggling to find how to define the current price of a stock in real time when market is open. I also am unsure how to round to 2 decimals. This is probably way off but here is what i have so far.....

Code:
def currentPrice = ??
def vwapValue = VWAP();
def stop = currentPrice - vwapValue;

AddLabel(yes, Concat("Stop: ", stop), color.magenta);

Sorry I am very new to this but have been scouring strategies code and this forum for a while now.....Thank you!!
 
This is what you need.

Code:
def currentPrice = close;

And then, to round:

Code:
AddLabel(yes, Concat("Stop: ", Round(stop)), color.magenta);
 
Sweet thank you!

Say I am on a 5m chart, using "close" to define currentPrice, will that update in realtime? As the price ticks will the "close" value change? Or does this only output the value of the close of the last 5m bar?
 
It seems to now be changing live as the price changes on my 5m chart, but the math seems to be off. It looks like it thinks that vwap is much higher than it shows on my chart. Do I need to specify the paramaters for vwap somehow? I am looking for it to show currentPrice - the value of the center line vwap. This is what i have so far....

Code:
def currentPrice = close;
def vwapValue = VWAP();
def stop = currentPrice - vwapValue;
AddLabel(yes, Concat("Stop: ", Round(stop)), color.magenta);

thanks!

I think I figured it out?? Please correct me if I am wrong. I am using a 5m chart.

Code:
def currentPrice = close;
def vwapValue = VWAP(AggregationPeriod.FIVE_MIN);
def stop = currentPrice - vwapValue;
AddLabel(yes, Concat("Stop: ", Round(stop)), color.magenta);
 
Hello,

I would like to add label on top left of the chart to show monthly, daily, and weekly VWAP , upper, and lower band in numeric value instead of showing plots on candle stick chart. I prefer to keep my chart clean so prefer to display vwap numbers on top left. I created duplicate of VWAP script and replaced plot with def now I need help to create label which will show monthly, weekly, and daily vwap (including upper and lower band value) on top left as label.

EDIT: Also, If I can have Yellow for VWAP center line, Green for Lower band and RED for Upper band highlighted that would be great!

Any help is appreciated.

Here is code:

Code:
input numDevDn = -2.0;
input numDevUp = 2.0;
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));

def VWAP = price;
def UpperBand = price + numDevUp * deviation;
def LowerBand = price + numDevDn * deviation;

VWAP.setDefaultColor(getColor(0));
UpperBand.setDefaultColor(getColor(2));
LowerBand.setDefaultColor(getColor(4));

# Could you verify if this looks good and make changes accordingly ?

Code:
input numDevDn = -2.0;
input numDevUp = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};
input time = yes;
input time_color = {default "magenta", "green", "pink", "cyan", "orange", "red", "blue", "gray", "violet"};
input VWAP_label = yes;
input VWAP_Color = {default "magenta", "green", "pink", "cyan", "orange", "red", "blue", "gray", "violet"};
input Upperbands_label = Yes;
input UpperBand_Color = {default "magenta", "green", "pink", "cyan", "orange", "red", "blue", "gray", "violet"};
input Lowerbands_label = Yes;
input LowerBand_color = {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();

VWAP.setDefaultColor(getColor(0));  
UpperBand.setDefaultColor(getColor(2));  
LowerBand.setDefaultColor(getColor(4));

AddLabel(time,  "Daily", GetColor(time_Color));
AddLabel(VWAP_label, " VWAP = " + Round(VWAP, 2), GetColor(VWAP_Color));
AddLabel(Upperbands_label, " VWAP UpperBand = " + round(UpperBand, 2), GetColor(UpperBand_Color));
AddLabel(Lowerbands_label, " VWAP LowerBand = " + round(LowerBand, 2), GetColor(LowerBand_Color));
 
Last edited:
Thread starter Similar threads Forum Replies Date
Clib vwap label Questions 1
T Label that shows % of 9ema from the VWAP. Questions 1
J Above VWAP label Questions 1
T SPY VWAP LABEL ON SPX CHART Questions 1
germanburrito Twap-Vwap Difference Questions 1

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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