Multiple-Day Anchored VWAP For ThinkOrSwim

VP7

New member
VIP
Hello,
I'm trying to place a 5 day rolling AVWAP on my chart. The default VWAP from ToS, set to weekly, resets on Monday each week. I'd actually prefer VWAP to be anchored to the previous 5 days on a rolling basis.

Most AVWAP scripts have a static anchor point, either using a date (ex. 20230601) and time, or using the same time each day. Is it possible to modify the following script (or create a new one) to have a rolling AVWAP?
 
Last edited by a moderator:

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

This does multiple continuous days without a reset on the last of these days
Screenshot 2023-06-07 051201.png
Code:
#VWAP_Days_Multiple_Days
input num_days_to_show  = 5;
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 isperiodrolled = thisDay <= num_days_to_show - 1;


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);
 
Last edited by a moderator:
I see this sets it at Monday at 6pm, how do I change it to Sunday at 6pm instead?

This shoud plot up to the last 5 days Vwap continuously for symbols that trade on Sunday from the last Sunday starting at 1800

Screenshot 2023-08-20 101323.png
Code:
#VWAP_Days_Multiple_Days_for_symbols_trading_on_Sunday

input num_days_to_show  = 5;
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 Sunday_Start = GetDayOfWeek(GetYYYYMMDD()[1]) == 5 and GetDayOfWeek(GetYYYYMMDD()) == 1;
def Sunday_Count = if Sunday_Start then Sunday_Count[1] + 1 else Sunday_Count[1];
def isperiodrolled = if Sunday_Start then 0 else thisDay <= num_days_to_show - 1;
###########

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 =  if Sunday_Count < HighestAll(Sunday_Count) then Double.NaN else 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 Sunday_Count == HighestAll(Sunday_Count) and isperiodrolled != isperiodrolled[1], "", Color.CYAN);
 
I really like using this VWAP indicator with it's standard deviation bands, and I want to see if it would be possible to modify it slightly so that at the end of each day the VWAP lines (including the standard deviation lines) are extended out like a continuous VWAP. It is basically creating an automatic anchored VWAP for each day. I have anchored vwap indicators for specific start days and times, but wasn't sure how to make each day be continuous if that makes sense. I have searched around but haven't been able to find anything yet. Anyone have any ideas or could point me in the right direction? I could maybe do a bar counter that creates a new AVWAP after so many bars and match it up to the time frame chosen and the aggregation period, but figured there could be an easier solution.

Code:
# VWAP Standard Deviation Bands
# lar
# 12.12.2015

# V1.0 - 12.12.2015 - lar     - Initial release of VWAP Standard Deviation bands
# V1.1 - 12.17.2019 - tomsk   - Minor edits

input timeFrame = {default Day, Week, Month, Quarter, Year, Minute, Hour, Chart, "Opt Exp", Bar};
input BandType = {default Standard, "1/4 Day Range", "3x Avg Bar Range", ThinkScripter, None};
input ShowCloud = yes;
input HideSdLines = no;

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 seconds = SecondsFromTime(0);
def month = GetYear() * 12 + GetMonth();
def year = GetYear();
def day_number = DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd));
def dom = GetDayOfMonth(yyyyMmDd);
def dow = GetDayOfWeek(yyyyMmDd - dom + 1);
def expthismonth = (if dow > 5 then 27 else 20) - dow;
def exp_opt = month + (dom > expthismonth);
def periodIndx;
def qtr =(GetMonth() - 1) % 3;
switch (timeFrame) {
case Chart:
    periodIndx = 0;

case Minute:
    periodIndx = Floor(seconds / 60 + day_number * 24 * 60);

case Hour:
    periodIndx = Floor(seconds / 3600 + day_number * 24);

case Day:
    periodIndx = CountTradingDays(Min(First(yyyyMmDd), yyyyMmDd), yyyyMmDd) - 1;

case Week:
    periodIndx = Floor(day_number / 7);

case Month:
    periodIndx = Floor(month - First(month));

case Year:
    periodIndx = Floor(year - First(year));

case Quarter:

    periodIndx = qtr == 0 and qtr[1] != 0;

case "Opt Exp":
    periodIndx = exp_opt - First(exp_opt);

case Bar:
    periodIndx = BarNumber() - 1;
}
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;
switch (BandType) {

case Standard:
    deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

case "1/4 Day Range":
    deviation = Sqrt(AbsValue(high(Period = timeFrame) - low(Period = timeFrame)) * .25);

case "3x Avg Bar Range":
    deviation = Sqrt(Average(TrueRange(high,  close,  low),  20) * 3);

case ThinkScripter:
    deviation = Sqrt(TotalSum(Sqr(((open + high + low + close) / 4) - price) * volume) / TotalSum(volume));

case None:
    deviation = Double.NaN;
}

plot VWAP = price;
VWAP.SetDefaultColor(CreateColor(40, 40, 40));
                
VWAP.SetStyle(Curve.SHORT_DASH);
VWAP.SetLineWeight(2);

# TS_CHART_VWAP_SD_BANDS
# [URL]http://www.thinkscripter.com[/URL]
# [EMAIL][email protected][/EMAIL]
# Last Update 03 APR 2010

input VWAPStdev1 = 0.5;
input VWAPStdev2 = 1.0;
input VWAPStdev3 = 1.5;
input VWAPStDev4 = 2.0;

plot r1 = VWAP + VWAPStdev1 * deviation;
plot s1 = VWAP - VWAPStdev1 * deviation;
plot r2 = VWAP + VWAPStdev2 * deviation;
plot s2 = VWAP - VWAPStdev2 * deviation;
plot r3 = VWAP + VWAPStdev3 * deviation;
plot s3 = VWAP - VWAPStdev3 * deviation;
plot r4 = VWAP + VWAPStdev4 * deviation;
plot s4 = VWAP - VWAPStdev4 * deviation;

DefineGlobalColor("sDev1", (CreateColor(70, 70, 70)));
DefineGlobalColor("sDev2", (CreateColor(128, 128, 128)));
DefineGlobalColor("sDev3", (CreateColor(100, 100, 100)));
DefineGlobalColor("sDev4", (CreateColor(100, 100, 100)));


r1.SetDefaultColor(GlobalColor("sDev1"));
s1.SetDefaultColor(GlobalColor("sDev1"));
r2.SetDefaultColor(GlobalColor("sDev2"));
s2.SetDefaultColor(GlobalColor("sDev2"));
r3.SetDefaultColor(GlobalColor("sDev2"));
s3.SetDefaultColor(GlobalColor("sDev2"));
r4.SetDefaultColor(Color.DOWNTICK);
s4.SetDefaultColor(Color.UPTICK);


r1.SetStyle(Curve.SHORT_DASH);
r2.SetStyle(Curve.SHORT_DASH);
r3.SetStyle(Curve.SHORT_DASH);
r4.SetStyle(Curve.SHORT_DASH);
s1.SetStyle(Curve.SHORT_DASH);
s2.SetStyle(Curve.SHORT_DASH);
s3.SetStyle(Curve.SHORT_DASH);
s4.SetStyle(Curve.SHORT_DASH);


VWAP.HideBubble();
r1.HideBubble();
r2.HideBubble();
r3.HideBubble();
r4.HideBubble();
s1.HideBubble();
s2.HideBubble();
s3.HideBubble();
s4.HideBubble();

r1.SetHiding(HideSdLines);
r2.SetHiding(HideSdLines);
r3.SetHiding(HideSdLines);
r4.SetHiding(HideSdLines);
s1.SetHiding(HideSdLines);
s2.SetHiding(HideSdLines);
s3.SetHiding(HideSdLines);
s4.SetHiding(HideSdLines);


#AddCloud(VWAP, r1, GlobalColor("sDev3"), GlobalColor("sDev3"));
#AddCloud(VWAP, s1, GlobalColor("sDev3"), GlobalColor("sDev3"));
AddCloud(VWAP, r2, GlobalColor("sDev1"), GlobalColor("sDev1"));
AddCloud(VWAP, s2, GlobalColor("sDev1"), GlobalColor("sDev1"));
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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