VWAP AddLabel w/ colors outside of bands

G

Gogat

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

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

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:
G

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.
 
P

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?
 
G

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.
 
P

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.
 

Similar threads

Top