Advanced VWAP

woulf1004

Active member
VIP
Can someone check and see why the anchor VWAP (AVWAP) does not plot anything nor show the value for the label created?
Code:
input timeFrame = {default DAY, WEEK, MONTH};
input begin = 0930;
input end = 1030;
input numDevUp1 = 1.0;
input numDevUP2 = 2.0;
input numDevUP3 = 3.0;
input numDevDn1 = -1.0;
input numDevDn2 = -2.0;
input numDevDn3 = -3.0;
input volAvgLength = 50;
input labels = yes;

def rth = SecondsFromTime(begin) >= 0 and SecondsTillTime(end) >= 0;
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 UpperBand1 = price + numDevUp1 * deviation;
plot UpperBand2 = price + numDevUP2 * deviation;
plot UpperBand3 = price + numDevUP3 * deviation;
plot LowerBand1 = price + numDevDn1 * deviation;
plot LowerBand2 = price + numDevDn2 * deviation;
plot LowerBand3 = price + numDevDn3 * deviation;

VWAP.SetDefaultColor(GetColor(0));
UpperBand1.SetDefaultColor(GetColor(2));
UpperBand2.SetDefaultColor(GetColor(2));
UpperBand3.SetDefaultColor(GetColor(2));
LowerBand1.SetDefaultColor(GetColor(4));
LowerBand2.SetDefaultColor(GetColor(4));
LowerBand3.SetDefaultColor(GetColor(4));

plot AVWAP = price and rth;

AVWAP.SetDefaultColor(GetColor(0));

def volumeSum2 = if rth then CompoundValue(1, volumeSum2[1] + volume, volume) else 0;
def volumeVwapSum2 = if rth then CompoundValue(1, volumeVwapSum2[1] + volume * AVWAP, volume * AVWAP) else 0;
def volumeVwap2Sum2 = if rth then CompoundValue(1, volumeVwap2Sum2[1] + volume * Sqr(AVWAP), volume * Sqr(AVWAP)) else 0;
def price2 = volumeVwapSum2 / volumeSum2;
def deviation2 = Sqrt(Max(volumeVwap2Sum2 / volumeSum2 - Sqr(price2), 0));

AddLabel(labels, (if close > LowerBand3 then " [ -SD3 ]" else " [ -SD3 ]") + " " + AsText(LowerBand3) + " ", if close <= LowerBand2 then Color.CYAN else Color.LIGHT_GRAY );

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > LowerBand2 then " [ -SD2 ]" else " [ -SD2 ]") + " " + AsText(LowerBand2) + " ", if close <= LowerBand1 and close > LowerBand2 then Color.MAGENTA else Color.LIGHT_GRAY);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > LowerBand1 then " [ -SD1 ]" else " [ -SD1] ") + " " + AsText(LowerBand1) + " ", if close < VWAP and close > LowerBand1 then Color.RED else Color.LIGHT_GRAY);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > VWAP then " [ VWAP ]" else " [ -VWAP ]") + " " + AsText(VWAP) + " ", if close > VWAP then Color.DARK_ORANGE else Color.DARK_ORANGE);

#AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > AVWAP and rth then " [ AVWAP ]" else " [ -AVWAP ]") + " " + AsText(AVWAP and rth) + " ", if close > AVWAP and rth then Color.CYAN else Color.CYAN);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > UpperBand1 then " [ SD1 ]" else " [ SD1 ]") + " " + AsText(UpperBand1) + " ", if close > VWAP and close < UpperBand1 then Color.GREEN else Color.LIGHT_GRAY);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > UpperBand2 then " [ SD2 ]" else " [ SD2 ]") + " " + AsText(UpperBand2) + " ", if close >= UpperBand1 and close < UpperBand2 then Color.MAGENTA else Color.LIGHT_GRAY);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > UpperBand3 then " [ SD3 ]" else " [ SD3 ]") + " " + AsText(UpperBand3) + " ", if close >= UpperBand2 then Color.CYAN else Color.LIGHT_GRAY);

def Vol = volume;
def VolAvg = Average(volume, VolAvgLength);

def above_volume = Vol > VolAvg;
def bullish_vwap = close crosses above VWAP;

plot bull = above_volume and bullish_vwap;
bull.AssignValueColor(Color.CYAN);
bull.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

def bearish_vwap = close crosses below VWAP;
plot bear = above_volume and bearish_vwap;
bear.AssignValueColor(Color.MAGENTA);
bear.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

AddLabel(labels, "||", Color.YELLOW);
 
Last edited by a moderator:
Can someone check and see why the anchor VWAP (AVWAP) does not plot anything nor show the value for the label created?

The AVWAP as coded, as price and rth, will return 1,0 if during rthrs. It will plot at the value 1.0.

Assuming you only want the plot to appear during rthrs timeframe, but the label to remain throughout the rest of time with the last value of the rthrs timeframe, then change these parts of code to:

Screenshot 2024-02-27 060349.png
Code:
def adv_vwap = if rth then price else adv_vwap[1];
plot AVWAP = if !rth then double.nan else price ;
AVWAP.SetDefaultColor(color.cyan);#GetColor(0));
AVWAP.setlineWeight(5);

The adv_vwap will be used in the label so that the label will appear throughout the day.
Code:
AddLabel(labels, (if close > Adv_VWAP  then " [ AVWAP ]" else " [ -AVWAP ]") + " " + AsText(Adv_VWAP ) + " ", if close > Adv_VWAP then Color.CYAN else Color.CYAN);
 
The AVWAP as coded, as price and rth, will return 1,0 if during rthrs. It will plot at the value 1.0.

Assuming you only want the plot to appear during rthrs timeframe, but the label to remain throughout the rest of time with the last value of the rthrs timeframe, then change these parts of code to:



The adv_vwap will be used in the label so that the label will appear throughout the day.
Hi SleepyZ!
The goal is to have two separate plots (VWAP in color orange and Anchored VWAP "AVWAP" in color cyan) on the same chart in addition to labels reflecting each respective value so, orange for VWAP value and Cyan for AVWAP. Oh, and yes, I would like the plots and labels to appear throughout the day.
Please see the attached for your reference.

Any updates please?
 

Attachments

  • Screenshot 2024-02-27 at 10.11.41 AM.png
    Screenshot 2024-02-27 at 10.11.41 AM.png
    296.9 KB · Views: 103
Last edited by a moderator:
Hi SleepyZ!
The goal is to have two separate plots (VWAP in color orange and Anchored VWAP "AVWAP" in color cyan) on the same chart in addition to labels reflecting each respective value so, orange for VWAP value and Cyan for AVWAP. Oh, and yes, I would like the plots and labels to appear throughout the day.
Please see the attached for your reference.

Any updates please?

Added to versions of RTH (0930-1030) and ROD (0930-1615) to capture the label value and both plots

Screenshot 2024-02-29 061437.png
Code:
input timeFrame = {default DAY, WEEK, MONTH};
input numDevUp1 = 1.0;
input numDevUP2 = 2.0;
input numDevUP3 = 3.0;
input numDevDn1 = -1.0;
input numDevDn2 = -2.0;
input numDevDn3 = -3.0;
input volAvgLength = 50;
input labels = yes;

def v = volume;
def vw = vwap;
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 UpperBand1 = price + numDevUp1 * deviation;
plot UpperBand2 = price + numDevUP2 * deviation;
plot UpperBand3 = price + numDevUP3 * deviation;
plot LowerBand1 = price + numDevDn1 * deviation;
plot LowerBand2 = price + numDevDn2 * deviation;
plot LowerBand3 = price + numDevDn3 * deviation;

VWAP.SetDefaultColor(GetColor(0));
UpperBand1.SetDefaultColor(GetColor(2));
UpperBand2.SetDefaultColor(GetColor(2));
UpperBand3.SetDefaultColor(GetColor(2));
LowerBand1.SetDefaultColor(GetColor(4));
LowerBand2.SetDefaultColor(GetColor(4));
LowerBand3.SetDefaultColor(GetColor(4));

## AnchoredVWAP RTHrs
input begin = 0930;
input end   = 1030;
def rth = SecondsFromTime(begin) >= 0 and SecondsTillTime(end) >= 0;

def rthvolumeSum = if !rth then 0 else if rth then CompoundValue(1, rthvolumeSum[1] + v, v) else 0;
def rthvolumeVwapSum = if !rth then 0 else if rth then CompoundValue(1, rthvolumeVwapSum[1] + v * vw, v * vw) else 0;
def rthvolumeVwap2Sum = if !rth then 0 else if rth then CompoundValue(1, rthvolumeVwap2Sum[1] + v * Sqr(vw), v * Sqr(vw)) else 0;
def rthprice = rthvolumeVwapSum / rthvolumeSum;
def adv_vwap = if rth then rthprice else adv_vwap[1];
plot AVWAP = if !rth then Double.NaN else rthprice ;
AVWAP.SetStyle(Curve.POINTS);
AVWAP.SetDefaultColor(Color.CYAN);
AVWAP.SetLineWeight(5);

#AVWAP the rest of the day
input rodbegin = 0930;
input rodend   = 1615;
def rod = SecondsFromTime(rodbegin) >= 0 and SecondsTillTime(rodend) >= 0;

def rodvolumeSum = if !rod then 0 else if rod then CompoundValue(1, rodvolumeSum[1] + v, v) else 0;
def rodvolumeVwapSum = if !rod then 0 else if rod then CompoundValue(1, rodvolumeVwapSum[1] + v * vw, v * vw) else 0;
def rodvolumeVwap2Sum = if !rod then 0 else if rod then CompoundValue(1, rodvolumeVwap2Sum[1] + v * Sqr(vw), v * Sqr(vw)) else 0;
def rodprice = rodvolumeVwapSum / rodvolumeSum;
def rod_vwap = if rod then rodprice else rod_vwap[1];
plot RODVWAP = if !rod then Double.NaN else rodprice ;
RODVWAP.SetStyle(Curve.SHORT_DASH);
RODVWAP.SetDefaultColor(Color.CYAN);
RODVWAP.SetLineWeight(5);

def volumeSum2 = if rth then CompoundValue(1, volumeSum2[1] + volume, volume) else 0;
def volumeVwapSum2 = if rth then CompoundValue(1, volumeVwapSum2[1] + volume * AVWAP, volume * AVWAP) else 0;
def volumeVwap2Sum2 = if rth then CompoundValue(1, volumeVwap2Sum2[1] + volume * Sqr(AVWAP), volume * Sqr(AVWAP)) else 0;
def price2 = volumeVwapSum2 / volumeSum2;
def deviation2 = Sqrt(Max(volumeVwap2Sum2 / volumeSum2 - Sqr(price2), 0));
#######

AddLabel(labels, (if close > LowerBand3 then " [ -SD3 ]" else " [ -SD3 ]") + " " + AsText(LowerBand3) + " ", if close <= LowerBand2 then Color.CYAN else Color.LIGHT_GRAY );

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > LowerBand2 then " [ -SD2 ]" else " [ -SD2 ]") + " " + AsText(LowerBand2) + " ", if close <= LowerBand1 and close > LowerBand2 then Color.MAGENTA else Color.LIGHT_GRAY);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > LowerBand1 then " [ -SD1 ]" else " [ -SD1] ") + " " + AsText(LowerBand1) + " ", if close < VWAP and close > LowerBand1 then Color.RED else Color.LIGHT_GRAY);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > VWAP then " [ VWAP ]" else " [ -VWAP ]") + " " + AsText(VWAP) + " ", if close > VWAP then Color.DARK_ORANGE else Color.DARK_ORANGE);

#AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > adv_vwap  then " [ AVWAP ]" else " [ -AVWAP ]") + " " + AsText(adv_vwap ) + " ", if close > adv_vwap then Color.CYAN else Color.CYAN);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > UpperBand1 then " [ SD1 ]" else " [ SD1 ]") + " " + AsText(UpperBand1) + " ", if close > VWAP and close < UpperBand1 then Color.GREEN else Color.LIGHT_GRAY);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > UpperBand2 then " [ SD2 ]" else " [ SD2 ]") + " " + AsText(UpperBand2) + " ", if close >= UpperBand1 and close < UpperBand2 then Color.MAGENTA else Color.LIGHT_GRAY);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > UpperBand3 then " [ SD3 ]" else " [ SD3 ]") + " " + AsText(UpperBand3) + " ", if close >= UpperBand2 then Color.CYAN else Color.LIGHT_GRAY);

def Vol = volume;
def VolAvg = Average(volume, volAvgLength);

def above_volume = Vol > VolAvg;
def bullish_vwap = close crosses above VWAP;

plot bull = above_volume and bullish_vwap;
bull.AssignValueColor(Color.CYAN);
bull.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

def bearish_vwap = close crosses below VWAP;
plot bear = above_volume and bearish_vwap;
bear.AssignValueColor(Color.MAGENTA);
bear.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

AddLabel(labels, "||", Color.YELLOW);

#
 
I found a couple issues. One, it has an overall lagging in price and market movement. Two, there is a bubble which I believe is for the AVWAP that plots on the price axis and it stays there. And three, the labels do not plot the correct VWAP and AVWAP values. I took a screenshot and marked by colors for your reference. You can also compare with the second chart on the right that has the correct VWAP values and market movement to see the difference.
 

Attachments

  • Screenshot 2024-02-29 at 12.55.22 PM.png
    Screenshot 2024-02-29 at 12.55.22 PM.png
    412.5 KB · Views: 55
I found a couple issues. One, it has an overall lagging in price and market movement. Two, there is a bubble which I believe is for the AVWAP that plots on the price axis and it stays there. And three, the labels do not plot the correct VWAP and AVWAP values. I took a screenshot and marked by colors for your reference. You can also compare with the second chart on the right that has the correct VWAP values and market movement to see the difference.

To add the RTHrs AVWAP, required me to use the TOS VWAP code which was already in your script that used the same definitons for voume and vwap (the price). So I tried to rename them to avoid problems with 'def v and def vw.

Moreover, you noticed that the AVWAP that I overlaid on your original was just the RTHrs potion of the TOS VWAP. So I rewrote it be correctly starting at the 0930 time anew, rather than just be a continuation of the TOS VWAP..

Below is the corrected code for the AVWAP, which matches what was in the bubble you saw. Also, included in your image that I was trying to duplicate, I saw that you included a contuation beyond the RTHrs end of 1030 to the end of the day. This is the 2nd set of code for the RODVWAP, which had to start at the beginning of RTHrs 0930 to continue the ROD line. If you do not want this, do not use it.

Add the portions of the following code to whatever code you are using before without the new AWAP.

In response to your points:
I have no idea what code you are actually using for the images you provided. So I cannot help with your performance issues nor am I able to see what the static bubble relates to.
The price you circled at the top of 1111.50 looks like it relates to the current chart close.

As the TOS VWAP uses the price type 'vwap' as part of its' computation of the PLOT also named VWAP, the indicators line,. I have tried to even rename the following code's names to be distinct from same code names in the orighinal code.

Beware that it is easy to misuse the references in your bubbles. I only looked at the AVWAP label to see if it matched the script, which it did. Review the others for correctness.

Screenshot 2024-02-29 145342.png
Code:
## AnchoredVWAP RTHrs
input begin = 0930;
input end   = 1030;
def rth = SecondsFromTime(begin) >= 0 and SecondsTillTime(end) >= 0;
def vol = volume;
def volwap =vwap;
def rthvolumeSum = if !rth then 0 else if rth then CompoundValue(1, rthvolumeSum[1] + vol, vol) else 0;
def rthvolumeVwapSum = if !rth then 0 else if rth then CompoundValue(1, rthvolumeVwapSum[1] + vol * volwap, vol * volwap) else 0;
def rthvolumeVwap2Sum = if !rth then 0 else if rth then CompoundValue(1, rthvolumeVwap2Sum[1] + vol * Sqr(volwap), vol * Sqr(volwap)) else 0;
def rthprice = rthvolumeVwapSum / rthvolumeSum;
def adv_vwap = if rth then rthprice else adv_vwap[1];
plot AVWAP = if !rth then Double.NaN else rthprice ;
AVWAP.SetStyle(Curve.POINTS);
AVWAP.SetDefaultColor(Color.CYAN);
AVWAP.SetLineWeight(5);

#AVWAP the rest of the day
input rodbegin = 0930;
input rodend   = 1615;
def rod = SecondsFromTime(rodbegin) >= 0 and SecondsTillTime(rodend) >= 0;

def rodvolumeSum = if !rod then 0 else if rod then CompoundValue(1, rodvolumeSum[1] + vol, vol) else 0;
def rodvolumeVwapSum = if !rod then 0 else if rod then CompoundValue(1, rodvolumeVwapSum[1] + vol * volwap, vol * volwap) else 0;
def rodvolumeVwap2Sum = if !rod then 0 else if rod then CompoundValue(1, rodvolumeVwap2Sum[1] + vol * Sqr(volwap), vol * Sqr(volwap)) else 0;
def rodprice = rodvolumeVwapSum / rodvolumeSum;
def rod_vwap = if rod then rodprice else rod_vwap[1];
plot RODVWAP = if !rod then Double.NaN else rodprice ;
RODVWAP.SetStyle(Curve.SHORT_DASH);
RODVWAP.SetDefaultColor(Color.CYAN);
RODVWAP.SetLineWeight(5);
 
To add the RTHrs AVWAP, required me to use the TOS VWAP code which was already in your script that used the same definitons for voume and vwap (the price). So I tried to rename them to avoid problems with 'def v and def vw.

Moreover, you noticed that the AVWAP that I overlaid on your original was just the RTHrs potion of the TOS VWAP. So I rewrote it be correctly starting at the 0930 time anew, rather than just be a continuation of the TOS VWAP..

Below is the corrected code for the AVWAP, which matches what was in the bubble you saw. Also, included in your image that I was trying to duplicate, I saw that you included a contuation beyond the RTHrs end of 1030 to the end of the day. This is the 2nd set of code for the RODVWAP, which had to start at the beginning of RTHrs 0930 to continue the ROD line. If you do not want this, do not use it.

Add the portions of the following code to whatever code you are using before without the new AWAP.

In response to your points:
I have no idea what code you are actually using for the images you provided. So I cannot help with your performance issues nor am I able to see what the static bubble relates to.
The price you circled at the top of 1111.50 looks like it relates to the current chart close.

As the TOS VWAP uses the price type 'vwap' as part of its' computation of the PLOT also named VWAP, the indicators line,. I have tried to even rename the following code's names to be distinct from same code names in the orighinal code.

Beware that it is easy to misuse the references in your bubbles. I only looked at the AVWAP label to see if it matched the script, which it did. Review the others for correctness.


I am sorry if I'm making things complicated but, can we start over again and use the code in the below to make sure the anchored VWAP "AVWAP" begins and starts the plot at the opening of the first tick at 9:30am (rth)? I think this should be the easiest way to explain to fix this issue.

input timeFrame = {default DAY, WEEK, MONTH};
input numDevUp1 = 1.0;
input numDevUP2 = 2.0;
input numDevUP3 = 3.0;
input numDevDn1 = -1.0;
input numDevDn2 = -2.0;
input numDevDn3 = -3.0;
input VolAvgLength = 50;
input labels = yes;

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 UpperBand1 = price + numDevUp1 * deviation;
plot UpperBand2 = price + numDevUp2 * deviation;
plot UpperBand3 = price + numDevUp3 * deviation;
plot LowerBand1 = price + numDevDn1 * deviation;
plot LowerBand2 = price + numDevDn2 * deviation;
plot LowerBand3 = price + numDevDn3 * deviation;

VWAP.setDefaultColor(getColor(0));
UpperBand1.setDefaultColor(getColor(2));
UpperBand2.setDefaultColor(getColor(2));
UpperBand3.setDefaultColor(getColor(2));
LowerBand1.setDefaultColor(getColor(4));
LowerBand2.setDefaultColor(getColor(4));
LowerBand3.setDefaultColor(getColor(4));

input begin = 0930;
input end = 1630;
input numDevDn = -2.0;
input numDevUp = 2.0;

def rth = SecondsFromTime(begin) >= 0 and SecondsTillTime(end) >= 0;
def volumeSum2 = if rth then CompoundValue(1, volumeSum2[1] + volume, volume) else 0;
def volumeVwapSum2 = if rth then CompoundValue(1, volumeVwapSum2[1] + volume * vwap, volume * vwap) else 0;
def volumeVwap2Sum2 = if rth then CompoundValue(1, volumeVwap2Sum2[1] + volume * Sqr(vwap), volume * Sqr(vwap)) else 0;
def price2 = volumeVwapSum2 / volumeSum2;
def adv_vwap = if rth then price else adv_vwap[1];

plot AVWAP = if !rth then double.nan else price;
AVWAP.SetDefaultColor(color.cyan);#GetColor(0));
#AVWAP.setDefaultColor(getColor(0));
AVWAP.setlineWeight(5);

#AddLabel(labels, "|", Color.YELLOW);

AddLabel(labels, (if close > LowerBand3 then " [ -SD3 ]" else " [ -SD3 ]") + " " + AsText(LowerBand3) + " ", if close <= LowerBand2 then Color.CYAN else Color.LIGHT_GRAY );

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > LowerBand2 then " [ -SD2 ]" else " [ -SD2 ]") + " " + AsText(LowerBand2) + " ", if close <= LowerBand1 and close > LowerBand2 then Color.MAGENTA else Color.LIGHT_GRAY);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > LowerBand1 then " [ -SD1 ]" else " [ -SD1] ") + " " + AsText(LowerBand1) + " ", if close < VWAP and close > LowerBand1 then Color.RED else Color.LIGHT_GRAY);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > VWAP then " [ VWAP ]" else " [ -VWAP ]") + " " + AsText(VWAP) + " ", if close > VWAP then Color.DARK_ORANGE else Color.DARK_ORANGE);

#AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > Adv_VWAP then " [ AVWAP ]" else " [ -AVWAP ]") + " " + AsText(Adv_VWAP ) + " ", if close > Adv_VWAP then Color.CYAN else Color.CYAN);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > UpperBand1 then " [ SD1 ]" else " [ SD1 ]") + " " + AsText(UpperBand1) + " ", if close > VWAP and close < UpperBand1 then Color.GREEN else Color.LIGHT_GRAY);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > UpperBand2 then " [ SD2 ]" else " [ SD2 ]") + " " + AsText(UpperBand2) + " ", if close >= UpperBand1 and close < UpperBand2 then Color.MAGENTA else Color.LIGHT_GRAY);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > UpperBand3 then " [ SD3 ]" else " [ SD3 ]") + " " + AsText(UpperBand3) + " ", if close >= UpperBand2 then Color.CYAN else Color.LIGHT_GRAY);

AddLabel(labels, "||", Color.YELLOW);
 
Last edited:
I am sorry if I'm making things complicated but, can we start over again and use the code in the below to make sure the anchored VWAP "AVWAP" begins and starts the plot at the opening of the first tick at 9:30am (rth)? I think this should be the easiest way to explain to fix this issue.

Thank you for the code

1. The following was modified to have a universal defs for volume (v_volume) and vwap (v_vwap). These were used in the standard TOS VWAP, ADVANCED VWAP and RODVWAP code.
2. The Advanced VWAP was added for RTHrs 0930-1030. The bubble matches the label's price.
3.The Rest of the day (RODVWAP) is included to extend the Advanced VWAP plot. Remove this if you do not need it. Its price bubble is hidden.
4. No performance issues noted.

Screenshot 2024-03-01 093752.png
Code:
input timeFrame = {default DAY, WEEK, MONTH};
input numDevUp1 = 1.0;
input numDevUP2 = 2.0;
input numDevUP3 = 3.0;
input numDevDn1 = -1.0;
input numDevDn2 = -2.0;
input numDevDn3 = -3.0;
input VolAvgLength = 50;
input labels = yes;

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 v_volume = volume;
def v_vwap   = vwap;
def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;

if (isPeriodRolled) {
volumeSum = v_volume;
volumeVwapSum =v_volume * v_vwap;
volumeVwap2Sum = v_volume * Sqr(v_vwap);
} else {
volumeSum = compoundValue(1, volumeSum[1] + v_volume, v_volume);
volumeVwapSum = compoundValue(1, volumeVwapSum[1] + v_volume * v_vwap, v_volume * v_vwap);
volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + v_volume * Sqr(v_vwap), v_volume * Sqr(v_vwap));
}
def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

plot VWAP = price;
plot UpperBand1 = price + numDevUp1 * deviation;
plot UpperBand2 = price + numDevUp2 * deviation;
plot UpperBand3 = price + numDevUp3 * deviation;
plot LowerBand1 = price + numDevDn1 * deviation;
plot LowerBand2 = price + numDevDn2 * deviation;
plot LowerBand3 = price + numDevDn3 * deviation;

VWAP.setDefaultColor(getColor(0));
UpperBand1.setDefaultColor(getColor(2));
UpperBand2.setDefaultColor(getColor(2));
UpperBand3.setDefaultColor(getColor(2));
LowerBand1.setDefaultColor(getColor(4));
LowerBand2.setDefaultColor(getColor(4));
LowerBand3.setDefaultColor(getColor(4));

#######################################################################
## AnchoredVWAP RTHrs
input avrthbegin = 0930;
input avrthend   = 1030;
def avrth = SecondsFromTime(avrthbegin) >= 0 and SecondsTillTime(avrthend) >= 0;
def avrthvolumeSum = if !avrth then 0 else if avrth then CompoundValue(1, avrthvolumeSum[1] + v_volume, v_volume) else 0;
def avrthvolumeVwapSum = if !avrth then 0 else if avrth then CompoundValue(1, avrthvolumeVwapSum[1] + v_volume * v_vwap, v_volume * v_vwap) else 0;
def avrthvolumeVwap2Sum = if !avrth then 0 else if avrth then CompoundValue(1, avrthvolumeVwap2Sum[1] + v_volume * Sqr(v_vwap), v_volume * Sqr(v_vwap)) else 0;
def avrthprice = avrthvolumeVwapSum / avrthvolumeSum;
def adv_vwap = if avrth then avrthprice else adv_vwap[1];
plot AVWAP = if !avrth then Double.NaN else avrthprice;
AVWAP.SetStyle(Curve.POINTS);
AVWAP.SetDefaultColor(Color.CYAN);
AVWAP.SetLineWeight(5);

#######################################################################
#REMOVE THIS IF YOU DO NOT WANT TO SEE THE ADVANCED VWAP 0930-1030 EXTENDED TO THE END OF DAY 1615
#AVWAP the rest of the day
input rodbegin = 0930;
input rodend   = 1615;
def rod = SecondsFromTime(rodbegin) >= 0 and SecondsTillTime(rodend) >= 0;

def rodvolumeSum = if !rod then 0 else if rod then CompoundValue(1, rodvolumeSum[1] + v_volume, v_volume) else 0;
def rodvolumeVwapSum = if !rod then 0 else if rod then CompoundValue(1, rodvolumeVwapSum[1] + v_volume * v_vwap, v_volume * v_vwap) else 0;
def rodvolumeVwap2Sum = if !rod then 0 else if rod then CompoundValue(1, rodvolumeVwap2Sum[1] + v_volume * Sqr(v_vwap), v_volume * Sqr(v_vwap)) else 0;
def rodprice = rodvolumeVwapSum / rodvolumeSum;
def rod_vwap = if rod then rodprice else rod_vwap[1];
plot RODVWAP = if !rod then Double.NaN else rodprice ;
RODVWAP.SetStyle(Curve.SHORT_DASH);
RODVWAP.SetDefaultColor(Color.CYAN);
RODVWAP.SetLineWeight(5);
RODVWAP.hidebubble();

########################################################################

#AddLabel(labels, "|", Color.YELLOW);

AddLabel(labels, (if close > LowerBand3 then " [ -SD3 ]" else " [ -SD3 ]") + " " + AsText(LowerBand3) + " ", if close <= LowerBand2 then Color.CYAN else Color.LIGHT_GRAY );

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > LowerBand2 then " [ -SD2 ]" else " [ -SD2 ]") + " " + AsText(LowerBand2) + " ", if close <= LowerBand1 and close > LowerBand2 then Color.MAGENTA else Color.LIGHT_GRAY);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > LowerBand1 then " [ -SD1 ]" else " [ -SD1] ") + " " + AsText(LowerBand1) + " ", if close < VWAP and close > LowerBand1 then Color.RED else Color.LIGHT_GRAY);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > VWAP then " [ VWAP ]" else " [ -VWAP ]") + " " + AsText(VWAP) + " ", if close > VWAP then Color.DARK_ORANGE else Color.DARK_ORANGE);

#AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > Adv_VWAP then " [ AVWAP ]" else " [ -AVWAP ]") + " " + AsText(Adv_VWAP ) + " ", if close > Adv_VWAP then Color.CYAN else Color.CYAN);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > UpperBand1 then " [ SD1 ]" else " [ SD1 ]") + " " + AsText(UpperBand1) + " ", if close > VWAP and close < UpperBand1 then Color.GREEN else Color.LIGHT_GRAY);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > UpperBand2 then " [ SD2 ]" else " [ SD2 ]") + " " + AsText(UpperBand2) + " ", if close >= UpperBand1 and close < UpperBand2 then Color.MAGENTA else Color.LIGHT_GRAY);

AddLabel(labels, ":", Color.YELLOW);

AddLabel(labels, (if close > UpperBand3 then " [ SD3 ]" else " [ SD3 ]") + " " + AsText(UpperBand3) + " ", if close >= UpperBand2 then Color.CYAN else Color.LIGHT_GRAY);

AddLabel(labels, "||", Color.YELLOW);

#
 
This is great!! Thank you so much for all your support SleepyZ. I really mean it.
I make a little change for the label to plot the ROD_VWAP value instead and it works perfectly fine.

AddLabel(labels, (if close > rod_vwap then " [ AVWAP ]" else " [ -AVWAP ]") + " " + AsText(rod_vwap ) + " ", if close > rod_vwap then Color.CYAN else Color.CYAN);

Thanks for this awesome work.
 

Attachments

  • Screenshot 2024-03-01 at 12.34.54 PM.png
    Screenshot 2024-03-01 at 12.34.54 PM.png
    280 KB · Views: 62

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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