VWAP Deviation, Z Score with Chart and lower indicators

horserider

horserider

Well-known member
VIP
As asked for here is the VWAP deviations in several forms.



Chart study. Pink is VWAP and cyan is 200 period Std Dev.

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2018-2019
#

input length = 200;

def zeros = vwap -vwap[2];
def filter = reference EhlersSuperSmootherFilter(price = zeros, "cutoff length" = 0.5 * length);
def rms = Sqrt(Average(Sqr(filter), length));
def scaledFilter = filter / rms;
def alpha = 5 * AbsValue(scaledFilter) / length;
def deviationScaledMovAvg = CompoundValue(1, alpha * vwap + (1 - alpha) * deviationScaledMovAvg[1], vwap);

plot DSMA = deviationScaledMovAvg;
DSMA.SetDefaultColor(GetColor(1));

First lower study. VWAP deviation at 20 period and 200 period lengths.

Code:
declare lower;

input price = close; 
input length = 20;

def SMA = Average(vwap[0], length);

plot DEV =(vwap/sma -1 )*100;

plot pdl = .5;
plot ndl = -.5;
plot zero = 0;

DEV.SetDefaultColor(GetColor(1));

input price2 = close; 
input length2 = 200;

def SMA2 = Average(vwap[0], length2);

plot DEV2 =(vwap/SMA2 -1 )*100;

DEV2.SetDefaultColor(GetColor(2));

Middle lower study. VWAP Z Score.

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

declare lower;
def RTH = GetTime() >= RegularTradingStart(GetYYYYMMDD()) and
          GetTime() <= RegularTradingEnd(GetYYYYMMDD());
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);
AddCloud(0, Zscore, color.red, color.green);
# End Code
Bottom study. Just simple VWAP deviation.

Code:
declare lower;

input price = close; 
input length = 20;

def vwd = Average(vwap[0], length);


plot DEV =(vwap/vwd -1 )*10;

plot pdl = .05;
plot ndl = -.05;
plot zero = 0;

Alert(DEV crosses below ndl, "", Alert.BAR, Sound.Bell);
Alert(DEV crosses above pdl, "", Alert.BAR, Sound.Bell);


DEV.SetDefaultColor(GetColor(1));
 
Last edited:
AGD

AGD

New member
Thanks, I have question.
In bottom study, you using this.
def allDev = STDevAll(close, 252);
plot DEV =(price/vwd -1 ) * 10;

I don't see you using allDev and why (price/vwd -1 ) * 10. I don't see in other studies.
 
horserider

horserider

Well-known member
VIP
@AGD Thanks for pointing this out. I did those studies a while ago when just starting thinkscript. Do not remember why I put allDev in as it does nothing. Redid the code to correct errors. Hope it passes inspection now.
 
  • Haha
Reactions: AGD
H

Harleym2009

New member
Anyone have an idea on how to trade using this indicator setup like is there a video out there or something I could watch tutorial thanks for any help
 
horserider

horserider

Well-known member
VIP
@Harleym2009 Not really an indicator setup. Just a few examples to show deviation from the VWAP as someone had asked about VWAP deviation.
Each study is a different way to show basically the same info. That is deviation of price from the VWAP. The idea would be a larger deviation may result in a reversion. In any deviation from the mean two things can happen, reversion to the mean or mean catches up to the deviation. So price moves back to the VWAP or the VWAP catches up with the price. Nothing is ever certain except at some point the two will come together. So just look those over and see if any can be useful to you. Ask questions about any particular one if you have any.
 
V

VicD

Member
@Harleym2009 I use it to see for momentum change. if it can reach 2.5 positive and negative, strong trend, if it can't and sits within 1 standard deviation, it's a weakness. Further, I look at divergence, if ZScore is going down, and the price is going up, I know there will be a correction soon.
 
V

VicD

Member
Easycators also has a VWAP scanner that’s shows stocks that are +2.0 and greater, and -2.0 and greater... I called TDA and they said it’s not available and would need to be created thru a script...

Here’s the link https://easycators.com/thinkscript/z-score-distance-from-vwap/

I really want it, any idea on how I can get it?

I can provide a screenshot if you need... but link shoudl

You can probably hack one together with an existing column that works off of a dynamic indicator and levels. Maybe an RSI or something? Or the TTMSqueeze column since it has 4 perimeters and you need 5. Pump the zscore logic in and set your text and colors based on the logic.
 
A

allnetbrett

New member
You can probably hack one together with an existing column that works off of a dynamic indicator and levels. Maybe an RSI or something? Or the TTMSqueeze column since it has 4 perimeters and you need 5. Pump the zscore logic in and set your text and colors based on the logic.
I don’t think I know enough about coding to create one similar. Not confident. Willing to pay someone $25 to create one
 
V

VicD

Member
This is Hahn-Tech's ttmqueeze column code, he just references the study, pretty clean, you just need to have the study loaded and properly named and referenced.

Code:
input price = CLOSE;
input length = 20;
input nK = 1.5;
input nBB = 2.0;
input alertLine = 1.0;
def squeezeHistogram = TTM_Squeeze(price, length, nK, nBB, alertLine).Histogram;
plot hist = if squeezeHistogram>= 0 then
if squeezeHistogram> squeezeHistogram[1] then 2 else 1
else if squeezeHistogram < squeezeHistogram[1] then -2 else -1; assignBackgroundColor(if squeezeHistogram >= 0 then
if squeezeHistogram > squeezeHistogram[1] then color.CYAN else color.BLUE
else if squeezeHistogram < squeezeHistogram[1] then color.RED else color.YELLOW); hist.assignvaluecolor(if squeezeHistogram >= 0 then
if squeezeHistogram > squeezeHistogram[1] then color.CYAN else color.BLUE
else if squeezeHistogram < squeezeHistogram[1] then color.RED else color.YELLOW);
 
V

VicD

Member
I can make you one but not till end of weekend, too much crap to do. Hit me up on discord. VictoriaD#7975. But honestly it should not be too hard to hack that code above.
 
A

allnetbrett

New member
This is Hahn-Tech's ttmqueeze column code, he just references the study, pretty clean, you just need to have the study loaded and properly named and referenced.

Code:
input price = CLOSE;
input length = 20;
input nK = 1.5;
input nBB = 2.0;
input alertLine = 1.0;
def squeezeHistogram = TTM_Squeeze(price, length, nK, nBB, alertLine).Histogram;
plot hist = if squeezeHistogram>= 0 then
if squeezeHistogram> squeezeHistogram[1] then 2 else 1
else if squeezeHistogram < squeezeHistogram[1] then -2 else -1; assignBackgroundColor(if squeezeHistogram >= 0 then
if squeezeHistogram > squeezeHistogram[1] then color.CYAN else color.BLUE
else if squeezeHistogram < squeezeHistogram[1] then color.RED else color.YELLOW); hist.assignvaluecolor(if squeezeHistogram >= 0 then
if squeezeHistogram > squeezeHistogram[1] then color.CYAN else color.BLUE
else if squeezeHistogram < squeezeHistogram[1] then color.RED else color.YELLOW);
thanks. I’m looking for a scan tho. This seems to be a chart study. Can you clarify?
 
V

VicD

Member
That is a column
So say you pull up all the gappers, and some are at z score of 2.5 or more on the daily setting, you can toggle through them, and only look at the charts that are in that area, or you can look at whatever watch list you have and sett it as a column.
 
A

allnetbrett

New member
Yea, either thats not what I am looking for, or I have no clue what I am doing. I created that code, and cannot get it into my watchlist. I was hoping someone could give me the parameters to use for the scan for VWAP +2.0 and -2.0
 
V

VicD

Member
Go to scan, add a study, then look for your zscore study, then set the target whatever your call your MA in the study to be greater than either 2 or greater than 70% (.7) depends how the study is get up (just check in the code what your moving average is called and what your overbought zone is set to). You can do that as 2 separate watch lists or you can do in one. so you have to set and "or", done it, and copy and invert your perimeters (i would suggest dong this after you have a successful scan result) but then your alternative is whatever MA below 30% or -2 depending how your script defines oversold. DON'T FORGET TO CHANGE the aggregation period next to the pencil icon on the scan screen.

Adopt this

 
A

allnetbrett

New member
Thank you for the break down, spent about an hour reading, interpreting, (watched the video twice), looked at my ZScore study, looked at the code, and am as lost as I was at the beginning. I guess its a sign I need to learn more... discouraging, I hate code, but I love #'s.... all very confusing. Thank you for trying to help... Not even sure what to ask to help me more
 
M

megahead34

New member
I did a little updating... also, got help. It looks like LazyBear indicator on Trading View but for TOS. https://tos.mx/lAOyZEx Zscore of Distance from VWAP indicator. It's like the Easycators one as well.
Hi VicD, thanks for sharing this. I loaded this Study in ThinkorSwim and have a couple of questions. When the Zscore is 0, does that mean the stock price is sitting on the VWAP line? Similarly, if the Zscore is greater than 0, stock price should be above the VWAP line? And when the Zscore is less than 0, stock price should be below the VWAP line. I'm looking at a intraday (1min) chart and noticed that this is not the case. For example, when the Zscore is 2, the stock price can be below the VWAP line. I can share a screen shot if it will help explain things.
 

Similar threads

Top