Prior Day VWAP

prolab

New member
Hello,

Would it be possible to write a VWAP indicator that is starting from the prior day's open?
 
Solution
Hello,

Would it be possible to write a VWAP indicator that is starting from the prior day's open?

Just input the number of days back and the start time for a continuous VWAP from that point.

Screenshot 2024-02-01 150423.png
Code:
#Anchored_VWAP_Continuous_from_DaysBacck_Start_Time

input num_days_to_show  = 2;
input start_time        = 0930;
input numdeviations1    = 2.0;
input numdeviations2    = 1.0;
input show_deviations   = yes;
def ymd                 = GetYYYYMMDD();
def candles  = !IsNaN(close);
def capture  = candles and ymd != ymd[1] ;
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;
def start = if thisday==num_days_to_show - 1 and...
Hello,

Would it be possible to write a VWAP indicator that is starting from the prior day's open?

Just input the number of days back and the start time for a continuous VWAP from that point.

Screenshot 2024-02-01 150423.png
Code:
#Anchored_VWAP_Continuous_from_DaysBacck_Start_Time

input num_days_to_show  = 2;
input start_time        = 0930;
input numdeviations1    = 2.0;
input numdeviations2    = 1.0;
input show_deviations   = yes;
def ymd                 = GetYYYYMMDD();
def candles  = !IsNaN(close);
def capture  = candles and ymd != ymd[1] ;
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;
def start = if thisday==num_days_to_show - 1 and secondsFromTime(start_time)==0 then 1 else start[1];
def isperiodrolled = thisDay <= num_days_to_show - 1 and start;


rec volumeSum;
rec volumeVwapSum;
rec 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));

rec v  = if IsNaN(reference VWAP()) then v[1] else price;

plot VWAP = if isperiodrolled then v else Double.NaN;
plot UpperBand1 = if show_deviations and isperiodrolled then price + numdeviations1 * deviation else Double.NaN;
plot LowerBand1 = if show_deviations and isperiodrolled then price - numdeviations1 * deviation else Double.NaN;

VWAP.SetDefaultColor(Color.CYAN);
VWAP.SetLineWeight(2);
UpperBand1.SetDefaultColor(Color.YELLOW);
LowerBand1.SetDefaultColor(Color.YELLOW);
UpperBand1.SetStyle(Curve.SHORT_DASH);
LowerBand1.SetStyle(Curve.SHORT_DASH);

plot UpperBand2 = if show_deviations and isperiodrolled then price + numdeviations2 * deviation else Double.NaN;
plot LowerBand2 = if show_deviations and isperiodrolled then price - numdeviations2 * deviation else Double.NaN;

UpperBand2.SetDefaultColor(Color.WHITE);
LowerBand2.SetDefaultColor(Color.WHITE);
UpperBand2.SetStyle(Curve.SHORT_DASH);
LowerBand2.SetStyle(Curve.SHORT_DASH);

input showvertical = yes;
AddVerticalLine(showvertical and isperiodrolled != isperiodrolled[1], "", Color.CYAN);
 
Solution
Just input the number of days back and the start time for a continuous VWAP from that point.
What is different in this calculation vs. the calculation that is done by TOS? Would it be possible to adjust the calculation to match TOS? I think the calculation is starting from the opening price the average price in that candle?
 

Attachments

  • Capture.JPG
    Capture.JPG
    114.1 KB · Views: 51
Just input the number of days back and the start time for a continuous VWAP from that point.
Hi There,

How do we display the previous day VWAP for SPX like shown (in green) in the attached image?

I played with the above code and couldn't get it to look like the one shown in the image. Interestingly, it doesn't show the vwap for SPX.

Thank You!
 

Attachments

  • PD VWAP.png
    PD VWAP.png
    306.2 KB · Views: 31
Last edited:
Hi There,

How do we display the previous day VWAP for SPX like shown (in green) in the attached image?

I played with the above code and couldn't get it to look like the one shown in the image. Interestingly, it doesn't show the vwap for SPX.

Thank You!

1. SPX dioes not have any volume in TOS. Therefore, the VWAP will not plot.
2. The script is coded to start each day selected at a time input. The previous version was set to begin at 0930, the start of regular trading hours.
3. Since you are using futures, I have modified the script to start at the beginning of the trading day (1800) for the /ES.
4. The image shows the TOS VWAP native study, which starts plotting at the start of each day and then restarts anew at the beginning of the next day.
5. The image also shows 2 versions of the script. One is set to 1 and other 2. Each maps the same day's VWAP as the native study. The one set at 2 continues plotting into the following day(s) from the start without restarting like the TOS native version.
6. Your image shows another broker chart than TOS. So I do not know how that compares to TOS.

Screenshot 2024-04-05 134953.png
Code:
#Anchored_VWAP_Continuous_from_DaysBacck_Start_Time_Futures

input num_days_to_show  = 2;
input start_time        = 1800;
input numdeviations1    = 2.0;
input numdeviations2    = 1.0;
input show_deviations   = yes;
def ymd                 = GetYYYYMMDD();
def candles  = !IsNaN(close);
def capture  = candles and ymd != ymd[1] ;
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;
def start = if thisDay == num_days_to_show - 1 and SecondsFromTime(start_time + GetAggregationPeriod() / 60000) == 0 then 1 else start[1];
def isperiodrolled = thisDay <= num_days_to_show - 1 and start;


rec volumeSum;
rec volumeVwapSum;
rec 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));

rec v  = if IsNaN(reference VWAP()) then v[1] else price;

plot VWAP = if isperiodrolled then v else Double.NaN;
plot UpperBand1 = if show_deviations and isperiodrolled then price + numdeviations1 * deviation else Double.NaN;
plot LowerBand1 = if show_deviations and isperiodrolled then price - numdeviations1 * deviation else Double.NaN;

VWAP.SetDefaultColor(Color.CYAN);
VWAP.SetLineWeight(2);
UpperBand1.SetDefaultColor(Color.YELLOW);
LowerBand1.SetDefaultColor(Color.YELLOW);
UpperBand1.SetStyle(Curve.SHORT_DASH);
LowerBand1.SetStyle(Curve.SHORT_DASH);

plot UpperBand2 = if show_deviations and isperiodrolled then price + numdeviations2 * deviation else Double.NaN;
plot LowerBand2 = if show_deviations and isperiodrolled then price - numdeviations2 * deviation else Double.NaN;

UpperBand2.SetDefaultColor(Color.WHITE);
LowerBand2.SetDefaultColor(Color.WHITE);
UpperBand2.SetStyle(Curve.SHORT_DASH);
LowerBand2.SetStyle(Curve.SHORT_DASH);

input showvertical = yes;
AddVerticalLine(showvertical and isperiodrolled != isperiodrolled[1], "", Color.CYAN);

#
 
1. SPX dioes not have any volume in TOS. Therefore, the VWAP will not plot.
2. The script is coded to start each day selected at a time input. The previous version was set to begin at 0930, the start of regular trading hours.
3. Since you are using futures, I have modified the script to start at the beginning of the trading day (1800) for the /ES.
4. The image shows the TOS VWAP native study, which starts plotting at the start of each day and then restarts anew at the beginning of the next day.
5. The image also shows 2 versions of the script. One is set to 1 and other 2. Each maps the same day's VWAP as the native study. The one set at 2 continues plotting into the following day(s) from the start without restarting like the TOS native version.
6. Your image shows another broker chart than TOS. So I do not know how that compares to TOS.
Awesome. Thanks for coding the script. Would you be able to add labels to the previous day vwap (call it 'PD VWAP') and the current day vvwap (call it 'VWAP') so that we can distinguish between them?

Thanks Again!
 
Awesome. Thanks for coding the script. Would you be able to add labels to the previous day vwap (call it 'PD VWAP') and the current day vvwap (call it 'VWAP') so that we can distinguish between them?

Thanks Again!

Optional bubbles are added that can be moved sideways by the number of bars input at bubblemover.
The image displays a 1d and 2d.

Screenshot 2024-04-23 110429.png
Code:
#Anchored_VWAP_Continuous_from_DaysBacck_Start_Time_Futures

input num_days_to_show  = 2;
input start_time        = 1800;
input numdeviations1    = 2.0;
input numdeviations2    = 1.0;
input show_deviations   = yes;
def ymd                 = GetYYYYMMDD();
def candles  = !IsNaN(close);
def capture  = candles and ymd != ymd[1] ;
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;
def start = if thisDay == num_days_to_show - 1 and SecondsFromTime(start_time + GetAggregationPeriod() / 60000) == 0 then 1 else start[1];
def isperiodrolled = thisDay <= num_days_to_show - 1 and start;


rec volumeSum;
rec volumeVwapSum;
rec 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 v  = if IsNaN(reference VWAP()) then v[1] else price;

plot VWAP = if isperiodrolled then v else Double.NaN;
plot UpperBand1 = if show_deviations and isperiodrolled then price + numdeviations1 * deviation else Double.NaN;
plot LowerBand1 = if show_deviations and isperiodrolled then price - numdeviations1 * deviation else Double.NaN;

VWAP.SetDefaultColor(Color.CYAN);
VWAP.SetLineWeight(2);
UpperBand1.SetDefaultColor(Color.YELLOW);
LowerBand1.SetDefaultColor(Color.YELLOW);
UpperBand1.SetStyle(Curve.SHORT_DASH);
LowerBand1.SetStyle(Curve.SHORT_DASH);

plot UpperBand2 = if show_deviations and isperiodrolled then price + numdeviations2 * deviation else Double.NaN;
plot LowerBand2 = if show_deviations and isperiodrolled then price - numdeviations2 * deviation else Double.NaN;

UpperBand2.SetDefaultColor(Color.WHITE);
LowerBand2.SetDefaultColor(Color.WHITE);
UpperBand2.SetStyle(Curve.SHORT_DASH);
LowerBand2.SetStyle(Curve.SHORT_DASH);

input showvertical = yes;
AddVerticalLine(showvertical and isperiodrolled != isperiodrolled[1], "", Color.CYAN);

input showbubbles = yes;
input bubblemover = 10;
def mover = IsNaN(close[bubblemover]) and !IsNaN(close[bubblemover + 1]) and showbubbles;

AddChartBubble(mover, VWAP[bubblemover + 1], "V-" + num_days_to_show, VWAP.TakeValueColor());
AddChartBubble(mover, UpperBand1[bubblemover + 1], "U1-" + num_days_to_show, UpperBand1.TakeValueColor());
AddChartBubble(mover, LowerBand1[bubblemover + 1], "L1-" + num_days_to_show, LowerBand1.TakeValueColor());
AddChartBubble(mover, UpperBand2[bubblemover + 1], "U2-" + num_days_to_show, UpperBand2.TakeValueColor());
AddChartBubble(mover, LowerBand2[bubblemover + 1], "L2-" + num_days_to_show, LowerBand2.TakeValueColor());
#
 

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
310 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