# Interday Anchored VWAP Zscore.

#### Whistler

##### New member
I am trying to convert some previously posted thinkscript code for plotting VWAP Z-Score on an intraday chart so that It can calculate and display values anchored to both the 7:00 and 12:30 time periods. I’m having a hard time grasping how thinkscript deals with time values. Can anyone help?

Code:
``````# Intraday VWAP Zscore
# Mobius
# 06.10.2019 Chat Room Request

declare lower;
def RTH = GetTime() >= RegularTradingStart(GetYYYYMMDD()) and
def n = if RTH and !RTH[1]
then 1
else if RTH
then n[1] + 1
else n[1];
def Avg = (fold i = 0 to n
with s
do s + getValue(close, i)) / n;
def VWAP_ = (fold ii = 0 to n
with ss
do ss + getValue(vwap, ii)) / n;
def StDev = Sqrt((fold iii = 0 to n
with sss = 0
do sss + Sqr(Avg - getValue(close, iii))) / n);
plot Zscore = (close - VWAP_) / StDev;
plot "0" = if isNaN(close) then double.nan else 0;
"0".SetDefaultColor(Color.white);
plot "1SD" = if isNaN(close) then double.nan else 1;
"1SD".SetDefaultColor(Color.Green);
plot "2SD" = if isNaN(close) then double.nan else 2;
"2SD".SetDefaultColor(Color.Green);
plot "-1SD" = if isNaN(close) then double.nan else -1;
"-1SD".SetDefaultColor(Color.Red);
plot "-2SD" = if isNaN(close) then double.nan else -2;
"-2SD".SetdefaultColor(Color.Red);
# End Code``````

Hey!
I have been trying to blend two indicators but am having trouble getting them to work correctly. Thanks for taking the time to open this thread!

I looked around and found many interday studies of the standard deviation from the vwap, but had trouble finding a z score of an anchored vwap. I have been trying to use the VWAP Zscore (which is really intraday) from this thread: https://usethinkscript.com/threads/z-score-distance-from-vwap-for-thinkorswim.602/

Code:
``````# Intraday VWAP Zscore
# Mobius
# 06.10.2019 Chat Room Request

declare lower;
def RTH = GetTime() >= RegularTradingStart(GetYYYYMMDD()) and
def n = if RTH and !RTH[1]
then 1
else if RTH
then n[1] + 1
else n[1];
def Avg = (fold i = 0 to n
with s
do s + getValue(close, i)) / n;
def VWAP_ = (fold ii = 0 to n
with ss
do ss + getValue(vwap, ii)) / n;
def StDev = Sqrt((fold iii = 0 to n
with sss = 0
do sss + Sqr(Avg - getValue(close, iii))) / n);
plot Zscore = (close - VWAP_) / StDev;
plot "0" = if isNaN(close) then double.nan else 0;
"0".SetDefaultColor(Color.white);
plot "1SD" = if isNaN(close) then double.nan else 1;
"1SD".SetDefaultColor(Color.Green);
plot "2SD" = if isNaN(close) then double.nan else 2;
"2SD".SetDefaultColor(Color.Green);
plot "-1SD" = if isNaN(close) then double.nan else -1;
"-1SD".SetDefaultColor(Color.Red);
plot "-2SD" = if isNaN(close) then double.nan else -2;
"-2SD".SetdefaultColor(Color.Red);
# End Code``````

And to replace the VWAP with an anchored VWAP with adjustable date and time from this thread: https://usethinkscript.com/threads/anchored-vwap-indicator-for-thinkorswim.171/

Code:
``````input anchorDate = 20200422;
input anchorTime = 2030;

def inPeriod = if SecondsTillTime(tradeStartEST) <= 0 and SecondsTillTime(tradeEndEST) > 0 then 1 else 0;

def revisedDate = if SecondsTillTime(anchorTime)<=0 and !inPeriod then anchorDate+1 else if SecondsTillTime(anchorTime)<=0 and inPeriod then anchorDate else anchorDate;

def postAnchorDate = if GetYYYYMMDD() >= revisedDate then 1 else 0;
def postAnchorTime = if SecondsTillTime(anchorTime) <= 0 then 1 else 0;

plot anchoredVWAP = TotalSum(if postAnchorDate and postAnchorTime then ((high+low+close)/3)*(volume) else 0)/TotalSum(if postAnchorDate and postAnchorTime then volume else 0);

anchoredVWAP.setStyle(Curve.Firm);
anchoredVWAP.SetLineWeight(3);
anchoredVWAP.SetDefaultColor(Color.Cyan);

I have tried simply changing out VWAP with achoredVWAP or changing RTH to try to emulate the time change, but I appear to be lost. I just want a lower indicator with an oscillator showing this score of the anchored vwap but the math and different codes have me a bit turned around. I would be extremely grateful for anyone's help or input! Thanks!

I'm sorry, I don't have an answer to your question but I do have a question you might be able to help me with. I'm trying to add a custom study one of my scans. I want it to scan for stocks trading no less than -1 standard deviation from VWAP. Have you come across any scans that have just this code without all the extra stuff that you're adding in? Thanks for the help!

As you are setting up the scan in TOS, add a study filter. Under popular scans is the VWAP scan. You need to specify the time period VWAP you are looking at, but you can enter less than -1 as the criteria and it should work.

Hey!
I have been trying to blend two indicators but am having trouble getting them to work correctly. Thanks for taking the time to open this thread!

I looked around and found many interday studies of the standard deviation from the vwap, but had trouble finding a z score of an anchored vwap. I have been trying to use the VWAP Zscore (which is really intraday) from this thread: https://usethinkscript.com/threads/z-score-distance-from-vwap-for-thinkorswim.602/

Code:
``````# Intraday VWAP Zscore
# Mobius
# 06.10.2019 Chat Room Request

declare lower;
def RTH = GetTime() >= RegularTradingStart(GetYYYYMMDD()) and
def n = if RTH and !RTH[1]
then 1
else if RTH
then n[1] + 1
else n[1];
def Avg = (fold i = 0 to n
with s
do s + getValue(close, i)) / n;
def VWAP_ = (fold ii = 0 to n
with ss
do ss + getValue(vwap, ii)) / n;
def StDev = Sqrt((fold iii = 0 to n
with sss = 0
do sss + Sqr(Avg - getValue(close, iii))) / n);
plot Zscore = (close - VWAP_) / StDev;
plot "0" = if isNaN(close) then double.nan else 0;
"0".SetDefaultColor(Color.white);
plot "1SD" = if isNaN(close) then double.nan else 1;
"1SD".SetDefaultColor(Color.Green);
plot "2SD" = if isNaN(close) then double.nan else 2;
"2SD".SetDefaultColor(Color.Green);
plot "-1SD" = if isNaN(close) then double.nan else -1;
"-1SD".SetDefaultColor(Color.Red);
plot "-2SD" = if isNaN(close) then double.nan else -2;
"-2SD".SetdefaultColor(Color.Red);
# End Code``````

And to replace the VWAP with an anchored VWAP with adjustable date and time from this thread: https://usethinkscript.com/threads/anchored-vwap-indicator-for-thinkorswim.171/

Code:
``````input anchorDate = 20200422;
input anchorTime = 2030;

def inPeriod = if SecondsTillTime(tradeStartEST) <= 0 and SecondsTillTime(tradeEndEST) > 0 then 1 else 0;

def revisedDate = if SecondsTillTime(anchorTime)<=0 and !inPeriod then anchorDate+1 else if SecondsTillTime(anchorTime)<=0 and inPeriod then anchorDate else anchorDate;

def postAnchorDate = if GetYYYYMMDD() >= revisedDate then 1 else 0;
def postAnchorTime = if SecondsTillTime(anchorTime) <= 0 then 1 else 0;

plot anchoredVWAP = TotalSum(if postAnchorDate and postAnchorTime then ((high+low+close)/3)*(volume) else 0)/TotalSum(if postAnchorDate and postAnchorTime then volume else 0);

anchoredVWAP.setStyle(Curve.Firm);
anchoredVWAP.SetLineWeight(3);
anchoredVWAP.SetDefaultColor(Color.Cyan);

I have tried simply changing out VWAP with achoredVWAP or changing RTH to try to emulate the time change, but I appear to be lost. I just want a lower indicator with an oscillator showing this score of the anchored vwap but the math and different codes have me a bit turned around. I would be extremely grateful for anyone's help or input! Thanks!
can you make the Intraday VWAP Zscore an upper study? can "xSD" lines be shown on the price as upper study

can you make the Intraday VWAP Zscore an upper study? can "xSD" lines be shown on the price as upper study

If your question is: "Can you SIMPLY scale the vwapZscore standard deviations to price?"
The answer is: "No". The vwapZscore is apples and the price on the upper chart is oranges. They have nothing in common upon which to correspond their plots and scale.

Last edited:

87k+ Posts
242 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?