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

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

@costagmbr This will display the Daily VWAP as a label.

Duplicate it and change the timeframe to Weekly and Monthly.

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2011-2021
#

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;
AddLabel(yes, Concat("VWAP = ", VWAP), color.orange);
 
I was able to simply copy the VWAP code and create a new VWAP indicator and add a label to the bottom of the code for that indicator and it works fine. Oddly enough I can't use the round function to round it to 2 decimal places. I can round it to 1, 3 or 4 but when I try to round to 2, it rounds it to 1 decimal place.
 
@brad10281 The Round() function is working as designed... Round() will round to a maximum of n decimals but trims off trailing zeros... While it may seem quirky, that's how it works... Only AsDollars() forces two decimal places...
 
Last edited:
I have the following code to put a VWAP label on my chart but it doesn't seem to work on an intraday timeframe. Any ideas on how to fix this?

Code:
AddLabel(yes, "VWAP: " + Round(VWAP (period = AggregationPeriod.DAY),2),CreateColor(255, 102, 255));
Brad, your original code above was the price function VWAP. It appears you wanted the study VWAP, which requires reference VWAP. so Thinkscript can differentiate the two. Try the following to see if it works for you:

Code:
AddLabel(1, "VWAP: " + AsText(reference VWAP()), CreateColor(255, 102, 255));
The astext function should also help with your rounding concern as shown in the image
Capture.jpg
 
Hello Forum...

So today I was screwing around and trying to clean up my chart/window and it happened! I had an awesome custom label that I can only presume that I got it here somewhere (I only use this site to get ideas, indicators and labels)...I looked everywhere to retrieve it....I searched the forum here, my latest opened shared items on my platform, I went 1 by 1 unlocked indicators in the studies, I checked maybe it was saved under previous workspace etc....I spent close to 3 hours and I am exactly where I was....I'm so ****ed!

Anyways....the label was:

Green and said current price "above VWAP" and it changed to red when price went or was "Below VWAP".

I do have the watchlist indicator/scanner but this label was very accurate.....sometimes when you open a chart, you have to spent time to actually see or find the VWAP and this was in your face! I miss it....I hope someone here has it or can direct me to the exact chat where it was posted? I soooo appreciate it.....it was an awesome tool!!!!!
 
Code:
def VWAP = reference VWAP()."VWAP"  ;
DefineGlobalColor("LabelRed",  CreateColor(225, 0, 0)) ;
DefineGlobalColor("LabelGreen",  CreateColor(0, 165, 0)) ;
AddLabel(yes, " VWAP = " + Round(VWAP, 2), if VWAP > close then GlobalColor("LabelRed") else GlobalColor("LabelGreen"));
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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