Multi-Day VWAP Indicator for ThinkorSwim

Did you check if @SleepZ version is causing you the same problem?
I actually found an old thread, deep in the pages, where others have similar issues to me. Basically multi day vwap would reset on Monday. Appears a fix was offered but I’ve yet to test it. I’ll update here! Thanks for the quick replies. Sorry my original post was unclear-I was having a hell of a time trying to figure out the issues.
 
This is cool @horserider . Thank you.

@yman , possibly the answer is only using a 2 day chart (like 2D/5m or 2D/15m) when using the 2 day setting. The indicator seems to throw off different plots when set otherwise.

On a second look, something doesn't look quite right to me but I don't know anything about how it should be. First, when i set the indicator for 3 days and then use a 3D/5m chart, the first day both vwaps begin at the same time (6pm EST) and mult day vwap overlaps regular vwap (logical) and day two they're divergent. But on day 3 mult day vwap seems to converge back to regular vwap at midnight EST (which is odd, I think..as regular VWAP begins at 6pm EST when the market opens). On Day 3, mult day vwap then diverges only slightly from vwap and intraday vwap for the day.

When I set the indicator for 2 days and use a 2D/5m chart, both begin at 6pm EST and begin to diverge after only a few hours. They stay diverged slightly for all of day 1 but seem quite diverged for day two without converging back to regular vwap at midnight EST.

The only way to know if this is giving a true 2 day vwap I guess is to see a 2 day vwap on another platform and compare.

This is cool though. I'll play with it and post back here if there are any new conclusions. In the meantime, enlighten me (anyone) please if anything I've posted is off. Thanks.
 
Hello, curious if you managed to figure this out? I was looking to do this today, but can't seem to get it done.

is there a way to do it through displacement of vwap to plot yesterday's vwap on today's chart? Not looking for aggregation, but actual.

i tried using displace on formual - volume(period = agg)[displace]; but this does not seem to be working

or if i can ask a different way, how can i get yesterday's volume calculation plotted on today's chart?

thanks.
 
Hello, curious if you managed to figure this out? I was looking to do this today, but can't seem to get it done.

is there a way to do it through displacement of vwap to plot yesterday's vwap on today's chart? Not looking for aggregation, but actual.

i tried using displace on formual - volume(period = agg)[displace]; but this does not seem to be working

or if i can ask a different way, how can i get yesterday's volume calculation plotted on today's chart?

thanks.
Code:
https://www.tradingview.com/v/TczzZxF6/
// author: twitter => @ashokb_
// last updated on => 20-July-2019

study("cVwap-pVwap", overlay = true)

nsd = iff(change(security(tickerid, "D", time)), 1, 0)

getVWAP(ns) =>
    p = iff(ns, hlc3 * volume, p[1] + hlc3 * volume)
    vol = iff(ns, volume, vol[1] + volume)
    v = p / vol
    Sn = iff(ns, 0, Sn[1] + volume * (hlc3 - v[1]) * (hlc3 - v))
    std = sqrt(Sn / vol)
    [v, std]


[vD, stdevD] = getVWAP(nsd)

vDplot = plot(vD, title = "Today's VWAP", color = #523584, style = line, transp = 10, linewidth = 2)
plot(valuewhen(nsd, vD[1], 0), title = "Previous day VWAP", color = #068E21, style = line, transp = 10, linewidth = 2)

Found this code on TV showing it can be done, does anyone know if there is an equivalent to [v ] (Series subscript )for TOS?
 
See if this study from Growex @Thinkscript Lounge that I modified to allow you to choose the number of days to include in the vwap.
@SleepyZ Been looking for this, so whats the work around on mondays to get the previous day vwap? I would guess look back 3 days to get the vwap from friday then adjust it on tuesday to look back one day to get prior day. Haven't done it yet but thats my assumption.
 
@Stephan908vkt 2 day VWAP

Code:
input numDevDn = -2.0;
input numDevUp = 2.0;
input timeFrame = {default TWO_DAY, DAY, THREE_DAY,WEEK, MONTH};
input showbubbles = yes;
input bubbleoffset = 2;

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 TWO_DAY:
    periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 2);
case THREE_DAY:
    periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 3);
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(6));
#UpperBand.setDefaultColor(getColor(2));
#LowerBand.setDefaultColor(getColor(4));

####Bubbles
def n = BubbleOffset;
def n1 = n + 1;
AddChartBubble(showbubbles and IsNaN(close[n]) and !IsNaN(close[n1]) , price[n1] , "VWAP 2D: " + Round(price[n1] , 2), Color.GREEN);

https://tos.mx/nSDFiqb
Can anyone help me combine the Daily, Two_days and Three_days VWAP into one study. I need all three VWAP lines drawn. Thanks in advance.
 
VWAP already has day,week, and month. Do not think an hourly is possible. Maybe describe the exact VWAP periods you want and see what might show up. For what purpose ??????

i5umtBP.png
Does anyone have the script for this study? Thanks in advance.
 
Does anyone have the script for this study? Thanks in advance.
@chinks1980 both scripts are on page one of the thread, @horserider has one and @SleepyZ has the other one, the one that youre looking at looks like the one sleepyz posted just scroll towards the middle bottom of page 1 of the thread you'll see it.

input days = 3;
def ymd = GetYYYYMMDD();
def ok = !IsNaN(close);
def capture = ok and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture
then dayCount[1] + 1
else dayCount[1], 0);
def thisDay = (HighestAll(dayCount) - dayCount) + 1;

#hint:plots the deviation channels based on the price-times-volume consideration. This may be called the dispersion of price-volume data.
#VM_MIDAS_StandartDeviationBands coded by Growex;
def Data = BarNumber();#Barnumber at the bar being calculated
def Start_Bar = if thisday[1]==days+1 and thisday==days then barnumber() else start_bar[1];

input price = close;
def Start_Cond = Data >= highestall(Start_Bar);#If true plotting is happening
def Price_x_Vol = if Start_Cond then Price_x_Vol[1] + price * volume else 0;#Cumulative Price times Volume
def cumvolume = if Start_Cond then cumvolume[1] + volume else 0;# Cumulative volume
plot Equiv_price = Price_x_Vol / cumvolume;# The equivalent price
def bars = data - Start_Bar;
def sample = if Start_Cond then sample[1] + sqr(price - Equiv_price) else 0;# Sum of the Square-Root of difference between the current price and the equivalent price. This also may be called the statistical total variance
def var = sample/bars;# Variance per bar
def dev = sqrt(var);# By statistical-definition, the standard deviation (SD) equals the square root of the variance
plot UBand = Equiv_price + dev;# The 1-deviation upper band
plot LBand = Equiv_price - dev;# The 1-deviation lower band
plot UBand2 = Equiv_price + 2 * dev;# The 2-deviation upper band
plot LBand2 = Equiv_price - 2 * dev;# The 2-deviation lower band
plot UBand3 = Equiv_price + 3 * dev;# The 3-deviation upper band
plot LBand3 = Equiv_price - 3 * dev;# The 1-deviation lower band
#end
 
Has anyone figured out how to plot both daily and previous day on the present chart from a single script? I have @horserider 2 day vwap and the TOS VWAP on my chart which works great but I need them combined into one script so I can backtest. Thanks in advance
 
Hi thanks for this post.
https://usethinkscript.com/threads/...manually-adjust-it-in-a-studys-settings.8867/

I found it and used some of the code to do something similar with VWAP but it still displays different values depending on the timeframe. What I want is a label that displays the slope of the 5 day VWAP (aggregated every 15 minutes) for the underlying symbol independent of the chart timeframe. Any thoughts on how to fix it?

declare lower;

input average_type = VWAP;
input average_length = 37.5;


def returnlength = 1; .

def avg1 = LinearRegCurve(price = average_type, length = average_length);

def NumChange;
NumChange = (avg1-avg1[returnlength])/avg1[returnlength] *100;

def yyyyMmDd = getYyyyMmDd();

def session_duration_quarters = (regularTradingEnd(yyyyMmDd) - regularTradingStart(yyyyMmDd)) / AggregationPeriod.FIFTEEN_MIN;

def agg = getAggregationPeriod();

def interval_size_raw;

if (agg == AggregationPeriod.DAY) {
interval_size_raw = session_duration_quarters * 4;

} else if (agg == AggregationPeriod.THREE_DAYS) {
interval_size_raw = session_duration_quarters * 4 * 3;


} else if (agg == AggregationPeriod.WEEK) {
interval_size_raw = session_duration_quarters * 4 * 5;

} else if (agg == AggregationPeriod.MONTH or agg == AggregationPeriod.OPT_EXP) {
interval_size_raw = session_duration_quarters * 4 * 22;

} else if (agg >= AggregationPeriod.FIFTEEN_MIN) {
interval_size_raw = agg / AggregationPeriod.FIFTEEN_MIN;
} else {
interval_size_raw = agg;
}


AddLabel(yes, " " + numchange + " ", if numchange > (0)
then CreateColor(255,255,204)
else if numchange < (0) then Color.Red
else Color.ORANGE);
 
Last edited by a moderator:
Hi thanks for this post. I found it and used some of the code to do something similar with VWAP but it still displays different values depending on the timeframe. What I want is a label that displays the slope of the 5 day VWAP (aggregated every 15 minutes) for the underlying symbol independent of the chart timeframe. Any thoughts on how to fix it?

Your script is doing the opposite of what you are requesting.

You are looking for this:
https://usethinkscript.com/threads/multi-day-vwap-indicator-for-thinkorswim.481/#post-4793
 
Last edited:
hi,
ANYBODY KNOW HOW TO plot vwap from 20min timeframe into lower timeframe, script provided below?

Im trying to plot vwap from 20min timeframe into lower timeframe just like what i did on moving average as example below but somehow I cannot make a script for vwap due to errors. does anybody know how to script the vwap similar to the moving average?thanks experts

SAMPLE SCRIPT ;

declare upper ;
def VWAP = reference VWAP() ;

plot exptwentyminutesChartclose = VWAP(VWAP, close(period = "20 min"), 1) ; ---------NEED HELP WITH THIS SCRIPT, ERROR ON THIS ONE

plot exptwentyminutesChartopen = MovingAverage(AverageType.EXPONENTIAL, open(period = "20 min"), 1) ;
 
Last edited by a moderator:
hi,
ANYBODY KNOW HOW TO plot vwap from 20min timeframe into lower timeframe, script provided below?

Im trying to plot vwap from 20min timeframe into lower timeframe just like what i did on moving average as example below but somehow I cannot make a script for vwap due to errors. does anybody know how to script the vwap similar to the moving average?thanks experts

SAMPLE SCRIPT ;

declare upper ;
def VWAP = reference VWAP() ;

plot exptwentyminutesChartclose = VWAP(VWAP, close(period = "20 min"), 1) ; ---------NEED HELP WITH THIS SCRIPT, ERROR ON THIS ONE

plot exptwentyminutesChartopen = MovingAverage(AverageType.EXPONENTIAL, open(period = "20 min"), 1) ;

1. The TOS standard VWAP does not provide intraday timeframes that can be used in reference vwap()
2. However, you can modify the standard code as shown in the following code to do this.
3. The exptwentyminutesChartopen period needs to be in the form of timeframe, "20 MIN", or as AggregationPeriod.TWENTY_MIN

Screenshot 2024-03-25 194222.png
Code:
#VWAP_MovAvg_Input_Intraday_Timeframes

declare upper ;

#VWAP with Optional Intraday Timeframes
input timeFrame = {"1 MIN", "2 MIN", "3 MIN", "5 MIN", "10 MIN", "15 MIN", default "20 MIN", "30 MIN", "45 MIN", "1 HOUR", "2 HOURS", "4 HOURS", DAY, WEEK, MONTH, CHART};

def cap = GetAggregationPeriod();
def errorInAggregation =
    timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
    timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
Assert(!errorInAggregation, "vwap timeFrame should be not less than current chart aggregation period");

def yyyyMmDd = GetYYYYMMDD();
def periodIndx;
def seconds = SecondsFromTime(0);
def day_number = DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd));
switch (timeFrame) {
case "1 MIN":
    periodIndx = Floor(seconds / 60 + day_number * 24 * 60);
case "2 MIN":
    periodIndx = Floor(seconds / 120 + day_number * 24);
case "3 MIN":
    periodIndx = Floor(seconds / 180 + day_number * 24);
case "5 MIN":
    periodIndx = Floor(seconds / 300 + day_number * 24);
case "10 MIN":
    periodIndx = Floor(seconds / 600 + day_number * 24);
case "15 MIN":
    periodIndx = Floor(seconds / 900 + day_number * 24);
case "20 MIN":
    periodIndx = Floor(seconds / 1200 + day_number * 24);
case "30 MIN":
    periodIndx = Floor(seconds / 1800 + day_number * 24);
case "45 MIN":
    periodIndx = Floor(seconds / 2700 + day_number * 24);
case "1 HOUR":
    periodIndx = Floor(seconds / 3600 + day_number * 24);
case "2 HOURS":
    periodIndx = Floor(seconds / 7200 + day_number * 24);
case "4 HOURS":
    periodIndx = Floor(seconds / 14400 + day_number * 24);
case DAY:
    periodIndx = yyyyMmDd;
case WEEK:
    periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
case MONTH:
    periodIndx = RoundDown(yyyyMmDd / 100, 0);
case CHART:
    periodIndx = 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));

def v  = if IsNaN(vwap) then v[1] else price;

plot exptwentyminutesChartclose = v; #---------NEED HELP WITH THIS SCRIPT, ERROR ON THIS ONE

#ExpAverage Selectable Timeframe

plot exptwentyminutesChartopen = MovingAverage(AverageType.EXPONENTIAL, open(period = timeframe), 1);

#
 

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