Plot previous day's VWAP Close as extended line

JADragon3

New member
Hello , Does anyone have a VWAP with the close extended into the next day for TOS ? The concept does a pretty good job of keeping you on the right side of the market. I will post a link from tradingview so you can see what I am talking about. Please let me know if this can be done on TOS.

Code:
study("VWAP MTF",overlay=true)

TimeFrame = input('W')
start = security(tickerid, TimeFrame, time)

//------------------------------------------------
newSession = iff(change(start), 1, 0)
//------------------------------------------------
vwapsum = iff(newSession, hl2*volume, vwapsum[1]+hl2*volume)
volumesum = iff(newSession, volume, volumesum[1]+volume)
v2sum = iff(newSession, volume*hl2*hl2, v2sum[1]+volume*hl2*hl2)
myvwap = vwapsum/volumesum
dev = sqrt(max(v2sum/volumesum - myvwap*myvwap, 0))
Coloring=close>myvwap?green:red
av=myvwap
showBcol = input(false, type=bool, title="Show barcolors")
showPrevVWAP = input(false, type=bool, title="Show previous VWAP close")
prevwap = iff(newSession, myvwap[1], prevwap[1])
plot(showPrevVWAP ? prevwap : na, style=circles, color=close > prevwap ? green : red)

A=plot(av, style=circles, color=Coloring)

barcolor(showBcol?Coloring:na)

https://www.tradingview.com/v/n2F64NVO/

Thanks , JADragon3
 

XeoNoX

Well-known member
VIP
Plots the Previous Day's VWAP and the Current VWAP ( volume weighted average price )

Appreciation goes a long way... Remember to this the like button if you found this post useful.

h3dVErK.png


Code:
#
# Plot Previous Days VWAP
# By XeoNoX via https://usethinkscript.com/
#

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;
def UpperBand = price + numDevUp * deviation;
def LowerBand = price + numDevDn * deviation;


input aggregationPeriod = AggregationPeriod.DAY;
input length = 1;
input displace = -1;
input showOnlyLastPeriod = no;
plot PrevDayVWAP;
if showOnlyLastPeriod and !IsNaN(vwap(period = aggregationPeriod)[-1]) {
    PrevDayVWAP = Double.NaN;
} else {
    PrevDayVWAP = Highest(vwap(period = aggregationPeriod)[-displace], length);
}
PrevDayVWAP.SetDefaultColor(GetColor(9));
PrevDayVWAP.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
 

TCB

Member
Hi, is there a script available or can one be created to plot yesterday's end of day vwap price on todays chart. For example if the vwap at the close was 200, the script would draw a line on todays intraday chart at the 200 level. Thanks
 

rad14733

Well-known member
VIP
@TCB The code below will paint a label with yesterdays closing VWAP... As you can see, it's fairly simple... You should be learning rather than simply asking others to do your coding, however...

Ruby:
# Yesterdays Closing VWAP
def yesterdaysClosingVWAP = vwap(period = AggregationPeriod.DAY)[1];
AddLabel(yes, "Yesterday's Closing VWAP: " + yesterdaysClosingVWAP, Color.WHITE);
 
  • Like
Reactions: TCB

mngolfer123

New member
@TCB
Code:
declare hide_on_daily;

def marketclose = secondstillTime(1600) == 0;
def vw = if marketclose then reference vwap() else vw[1];
plot vwap = vw;
I have tried over 5 scripts and cannot get this study to look how I want. Do you know how I can get the study to only show 2day or 3day VWAP and not have those ugly lines connecting days together?


Code:
declare hide_on_daily;

input DaysBack = 1;

def VWAP_StartDate = GetYYYYMMDD();
def BeyondStartDate = VWAP_StartDate >= GetYYYYMMDD()[DaysBack];
AddLabel(yes, BeyondStartDate, Color.WHITE);
def marketclose = SecondsTillTime(1600) == 0;
AddLabel(yes, marketclose, Color.WHITE);
def vw = if marketclose then reference vwap()[DaysBack] else vw[1];
plot vwap = vw;

vwap.SetDefaultColor(Color.ORANGE);
vwap.SetStyle(Curve.FIRM);
vwap.SetLineWeight(3);
 

generic

Active member
@mngolfer123
Code:
declare hide_on_daily;

input DaysBack = 2;

def days = getday() >= getlastday() - daysback;

def marketclose = SecondsTillTime(1600) == 0;

def vw = if marketclose then reference vwap() else vw[1];
plot vwap = if days then vw else double.nan;
vwap.setpaintingstrategy(paintingstrategy.horizontal);
 

mngolfer123

New member
@mngolfer123
Code:
declare hide_on_daily;

input DaysBack = 2;

def days = getday() >= getlastday() - daysback;

def marketclose = SecondsTillTime(1600) == 0;

def vw = if marketclose then reference vwap() else vw[1];
plot vwap = if days then vw else double.nan;
vwap.setpaintingstrategy(paintingstrategy.horizontal);
@generic Thank you so much for this! I really appreciate it. I have been searching and digging through code for probably two weeks now. I have written a decent amount of ThinkScripts but I'm not on your level.

I have tried to change the code above to show only 1 VWAP per indicator, but I can't seem to figure that out either.

I wasn't sure how the "vw[1]" part of your code works. I get everything else, but it seems like I have to add in some more lines to get it so that it shows VWAP from the DaysBack candle at marketclose and extends it forward through the entire next trading day.

Thats what you have, but I'm wondering how I could drop the line from two days back or at least make it start at the 1600 marketclose time instead of the end of day time 1800?

Code:
declare hide_on_daily;

input DaysBack = 1;

def days = (getday() >= getlastday() - daysback);# and (GetDay() - daysback) and getday() <= getlastday();

def marketclose = SecondsTillTime(1600) == 0;

def vw = if marketclose then reference vwap() else vw[1];
plot vwap = if days then vw else double.nan;

#To verify values
AddLabel(Yes, days, color.yellow);
AddLabel(Yes, getday(), color.yellow);
AddLabel(Yes, getlastday(), color.yellow);
AddLabel(Yes, getlastday() - daysback, color.yellow);
AddLabel(Yes, marketclose, color.yellow);

vwap.setpaintingstrategy(paintingstrategy.horizontal);
vwap.SetDefaultColor(Color.ORANGE);
#vwap.SetStyle(Curve.FIRM);
vwap.SetLineWeight(3);
 

generic

Active member
@mngolfer123 Sorry, I don't really understand the question. Could you post a ss and I'll look at it tmr?
The code already checks for 1600 and plots vwap into next day.
The vw[1] just says the vwap value until it checks for market close.
 

mngolfer123

New member
@mngolfer123 Sorry, I don't really understand the question. Could you post a ss and I'll look at it tmr?
The code already checks for 1600 and plots vwap into next day.
The vw[1] just says the vwap value until it checks for market close.
@generic Hey Generic! So it may be that I am using the show extended hours feature on my chart, which I need for my trading style. The VWAP from 2 days ago end at the end of day time of 1800 instead of the 1600, so essentially it is not the correct VWAP value from close.


Then Also wondering how I would extend the VWAP line from 2 days ago all the way to the end of today?
 

mngolfer123

New member
The vw[1] just says the vwap value until it checks for market close.
Okay, so I see that it plots the vwap value, but since it is in the else statement, it doesn't seem to use the "reference VWAP()" function at all?

def vw = if marketclose then reference vwap() else vw[1]; Not sure how it knows to use VWAP since the "reference VWAP()" is inside of the first IF/THEN statement.
plot vwap = if days then vw else double.nan;
 

generic

Active member
@mngolfer123 Okay, so the vwap value is correct but it gets cut off because of how the plot condition works. The plot condition will plot on the next day because it doesn't include AH from yesterday but the data for the plot is the vwap of yesterdays close. You should be able to verify this by adding vwap study and comparing the numbers.
If you look at the vw line of code then you'll see that vw is using reference vwap so vw[1], which saves the previous value of vw, will also use reference vwap.
Hope this makes sense.
 

Similar threads

Top