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();
 
I used one of the VWAP codes above and it works great. does anyone know how to add a line so it turns r/g when above or below? and also i cannot seem to get the timeframe in any minutes form with it working.

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

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

@jakeyxjakey you stated:


What is r/g (i would assume red and green) and above or below what?


Add a line where and to what and above and below what?
yes r/g as in red to green I apologize. and by main line I ment the middle (main) VWAP band. sorry I was pretty brief.

I just want it to turn green when it's above the middle VWAP line, and turn red if the price is below it, and green if it's above.

thank you!!
 
@jakeyxjakey Price above Vwap turns Vwap Line Green and Price Below Vwap turns Vwap Line Red

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;

plot UpperBand = price + numDevUp * deviation;
UpperBand.hide();
plot LowerBand = price + numDevDn * deviation;
LowerBand.hide();


vwap.DefineColor("UP", Color.GREEN);
vwap.DefineColor("DOWN", Color.LIGHT_RED);
vwap.AssignValueColor( if close> vwap then vwap.Color("UP") else vwap.Color("DOWN"));
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));
 
@XeoNoX that still doesn't change the color for the VWAP label box, just the VWAP line. I appreciate you're input.

I just want to have the VWAP label change red/green like the MA labels do.

zOCUrTd.png
 
@jakeyxjakey You didn't say you wanted to change the label, you stated the line.

I added the code for the label to change to green if price is higher than VWAP and red if price is less than VWAP.

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;

plot UpperBand = price + numDevUp * deviation;
UpperBand.hide();
plot LowerBand = price + numDevDn * deviation;
LowerBand.hide();


vwap.DefineColor("UP", Color.GREEN);
vwap.DefineColor("DOWN", Color.LIGHT_RED);
vwap.AssignValueColor( if close> vwap then vwap.Color("UP") else vwap.Color("DOWN"));
UpperBand.setDefaultColor(getColor(2));
LowerBand.setDefaultColor(getColor(4));


AddLabel(VWAP_label, " VWAP = " + Round(VWAP, 2), if vwap<close then color.green else color.red);
AddLabel(Upperbands_label, " VWAP UpperBand = " + round(UpperBand, 2), GetColor(UpperBand_Color));
AddLabel(Lowerbands_label, " VWAP LowerBand = " + round(LowerBand, 2), GetColor(LowerBand_Color));
 
Last edited:
@XeoNoX, awesome work, Man. One thing I noticed in the code is that the time doesn't switch to the right setting when flipped. For instance, if I set it for "Weekly," while the line adjusts accordingly, the label still displays "Daily," as opposed to "Weekly," or "Monthly," setting. Can it be fixe?

Thank you!
 
@XeoNoX, awesome work, Man. One thing I noticed in the code is that the time doesn't switch to the right setting when flipped. For instance, if I set it for "Weekly," while the line adjusts accordingly, the label still displays "Daily," as opposed to "Weekly," or "Monthly," setting. Can it be fixe?

Thank you!

someone added that, i fixed it, it wasnt dynamically set. just delete
" AddLabel(time, "Daily", GetColor(time_Color)); "
 
Hey dude, is there anyway you can have the previous close label turn red/green as well?

Here's the current script for it.

Code:
declare hide_on_daily;

def PrevClose = close(period = AggregationPeriod.Day)[1];

plot level1 = PrevClose * 1.9;
plot level2 = PrevClose * 2.2;
plot level3 = PrevClose * 2.8;
plot level4 = PrevClose * 3.4;
plot level5 = PrevClose * 4.6;   

level1.SetDefaultColor(Color.Yellow);
level2.SetDefaultColor(Color.Yellow);
level3.SetDefaultColor(Color.Yellow);
level4.SetDefaultColor(Color.Yellow);
level5.SetDefaultColor(Color.Yellow);

AddLabel(1, "PrevClose: " + PrevClose, Color.gray);


# End Levels Based on Previous Daily Close

@Jakey - LFG! haha nice!
haha I love it. this guy from the Discord im in Atlas made it, his name is Dark Elvis. feel free to add me if you're interested in the indicator or just talking stocks! slim trady#5764
 
Last edited by a moderator:
@jakeyxjakey

Code:
AddLabel(1, "PrevClose: " + PrevClose,  if vwap<close then color.green else color.red);
I was looking to have it green if the current price is above yesterdays previous close, and red if it's below the previous day close's price. not the VWAP. I couldn't find a way to get it in the script properly. I really appreciate your help!

I basically want it to be a red/green indicator for the day (prev day close price)

thanks again m8!
 
I was also trying to make these %gain labels turn green when the price is above them. they're 90% 120% 180% and so on (fib levels). I almoooost have the labels so they turn red/green accordingly. I hope you can see what I'm goin for. so far the VWAP and EMA labels change red/green properly now thanks to you. Just need the prev close then the fib %. here is the script for the fib level labels

unknown.png


Code:
declare hide_on_daily;

def PrevClose = close(period = AggregationPeriod.Day)[1];

plot level1 = PrevClose * 1.9;
plot level2 = PrevClose * 2.2;
plot level3 = PrevClose * 2.8;
plot level4 = PrevClose * 3.4;
plot level5 = PrevClose * 4.6;    




AddLabel(1, "90% = " + Round(level1,2), Color.White);
AddLabel(1, "120% = " + Round(level2,2), Color.White);
AddLabel(1, "180% = " + Round(level3,2), Color.White);
AddLabel(1, "240% = " + Round(level4,2), Color.White);
AddLabel(1, "360% = " + Round(level5,2), Color.White);

# End Levels Based on Previous Daily Close

thank you so much again dude you have nooo idea
 
You stated "yesterdays previous close" so you want the close from 2 days ago? I assume you meant the prior days close, here you go.

Previous Day Close Label with red/green color

Code:
def PriorDayClose = if GetDay() == GetLastDay() then PriorDayClose[1] else close;
AddLabel(1, "PriorDayClose : " + PriorDayClose ,  if PriorDayClose <close then color.green else color.red);
 
@XeoNoX I just wanted to thank you again dude. it looks great and functions even better. I appreciate you taking your time. I believe heavily in karma and helping people when I can, so I like to think it gets paid back to me as well.

If you're Disc or Twitter add me we can talk about some Finance ****. slim trady#5764. Have a great weekend dude!

unknown.png
 
@XeoNoX prior day close ahhh. still don't think I would have gotten the getday last day part. I greatly appreciate it brotha!
hey dude, I have these labels that indicate different fibonacci price levels. do you think you could implement a way so they once that price is hit, they turn yellow?

loook how good the rest of the labels look. thank you so much dude you have no idea


""



declare hide_on_daily;

def PrevClose = close(period = AggregationPeriod.Day)[1];

plot level1 = PrevClose * 1.9;
plot level2 = PrevClose * 2.2;
plot level3 = PrevClose * 2.8;
plot level4 = PrevClose * 3.4;
plot level5 = PrevClose * 4.6;




AddLabel(1, "90% = " + Round(level1,2), Color.White);
AddLabel(1, "120% = " + Round(level2,2), Color.White);
AddLabel(1, "180% = " + Round(level3,2), Color.White);
AddLabel(1, "240% = " + Round(level4,2), Color.White);
AddLabel(1, "360% = " + Round(level5,2), Color.White);

# End Levels Based on Previous Daily Close

""
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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