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

horserider

Well-known member
VIP
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));
 

Gogat

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

Pensar

Active member
VIP
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:

Gogat

New member
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.
 

Pensar

Active member
VIP
@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?
 

Gogat

New member
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.
 

Pensar

Active member
VIP
@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.
 

samoya

Active member
VIP
@horserider I thought the VWAP level is the same on all timeframes: Am I wrong? What is the difference?

Thank you for your help.

X4EU5DT.png
[/IMG]

The yellow line is the "intraday VWAP"

Code:
declare hide_on_daily;

def anchorTime = 0930;
def anchorEnd = 1600;

input ShowTodayOnly = yes;
def Today = if GetDay() == GetLastDay() then 1 else 0;
def postAnchorTime = if SecondsFromTime(anchorTime) >= 0 then 1 else 0;
def endAchorTime = if SecondsTillTime(anchorEnd) >= 0 then 1 else 0;

#plot anchorVWAP for intraday
def volumeSum = compoundValue(1, if postAnchorTime and endAchorTime then volumeSum[1] + volume else 0, volume);
def volumeVwapSum = compoundValue(1, if postAnchorTime and endAchorTime then volumeVwapSum[1] + volume * vwap else 0, volume * vwap);

plot anchorVWAP = if ShowTodayOnly and !Today then Double.NaN else if anchorTime then volumeVwapSum / volumeSum else Double.NaN;
anchorVWAP.setStyle(Curve.Firm);
anchorVWAP.setDefaultColor(Color.light_ORANGE);
anchorVWAP.setlineWeight(2);
 

martinstocks87

New member
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!!
 

BenTen

Administrative
Staff
VIP
This is what you need.

Code:
def currentPrice = close;

And then, to round:

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

martinstocks87

New member
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?
 

Similar threads

Top