Customize ZScore to capture/display higher volatility

uTSusr07A

Member
Plus
Hello,
pls see the included screenshot of a 15min chart of $AMD with ZScore lower indicator.
NzLVtHa.png


The code of the Zscore indicator is included below:
-------------------------------------------------------------------------------------------------------------
Code:
declare lower;

input price = close;
input length = 20;
input ZavgLength = 20;

#Initialize values
def oneSD = stdev(price,length);
def avgClose = simpleMovingAvg(price,length);
def ofoneSD = oneSD*price[1];
def Zscorevalue = ((price-avgClose)/oneSD);
def avgZv = average(Zscorevalue,20);

#Compute and plot Z-Score

plot avgZscore = average(Zscorevalue,ZavgLength);
avgZscore.setPaintingStrategy(paintingStrategy.LINE);

plot Zscore = ((price-avgClose)/oneSD);
Zscore.setPaintingStrategy(paintingStrategy.HISTOGRAM);
Zscore.setLineWeight(2);
#Zscore.assignValueColor(if Zscore > 0 then color.green else color.red);

#Diff.SetDefaultColor(GetColor(5));
Zscore.DefineColor("Positive and Up", Color.DARK_GREEN);
Zscore.DefineColor("Positive and Down", Color.LIGHT_GREEN);
Zscore.DefineColor("Negative and Down", Color.DARK_RED);
Zscore.DefineColor("Negative and Up", Color.ORANGE);
Zscore.AssignValueColor(if Zscore >= 0 then if Zscore > Zscore[1] then Zscore.color("Positive and Up") else Zscore.color("Positive and Down") else if Zscore < Zscore[1] then Zscore.color("Negative and Down") else Zscore.color("Negative and Up"));



#This is an optional plot that will display the momentum of the Z-Score average
#plot momZAvg = (avgZv-avgZv[5]);

#Plot zero line and extreme bands
plot zero = 0;
plot two = 2;
plot one = 1;
plot negone = -1;
plot negtwo = -2;
def three = 3;
def negthree = -3;

zero.setDefaultColor(color.yellow);
two.setDefaultColor(color.red);
one.setDefaultColor(color.orange);
negone.setDefaultColor(color.light_green);
negtwo.setDefaultColor(color.green);

AddCloud(three, two, Color.dark_RED, Color.CURRENT);
AddCloud(negtwo, negthree, Color.dark_GREEN, Color.CURRENT);
---------------------------------------------------------------------------------------------------

As indicated in the screenshot, while price increased steeply for over 100minutes from the open, Zscore peaked at +3 standard deviations, at the end of the very first 15min candle..and started dropping steeply.

I'm not well versed with scripting, but is there a way to customize this indicator, to truly capture & display the price volatility (in standard deviations), up until the price actually starts stagnating(or reversing)? ( and not drop steeply like this one , even with steep upside volatility). You could argue, the subsequent candles , after the first 15min close, were not as huge and so Zscore is showing a decline in price volatility and is the expected behavior....,but I'm not using Zscore necessarily for divergences, but to reflect the actual price volatility , going to +4,+5 etc, as needed.

So in this case, would like the Zscore plot to move up in tandem with the price (even at a lower rate/angle to reflect reduced volatility compared to the first candle) and only decline when the price stagnates/enters a range (or reverses). Yes, I can use other indicators like RSI or Stochastics to reflect price momentum, but wanted to see if we can adjust Zscore, as mentioned above.

Any help appreciated.
Thanks again

Here's another example of what I'm referring to, with ZScore. Today's $TSLA sell-off from the open, on a 15min chart.
a9TNQos.png


ZScore went into an oversold zone and hit its lowest SD (-2.8) on the 2nd 15min candle and started reversing up..,while the price volatility to the downside continued,until 10:30am.

I do have Bollinger Bands plotted on my production 15min chart , that can show the unbounded volatility to either side, but need this to be a lower study.
In "normal" volatility conditions, in most cases a standard deviation of 2-3 puts the price into overbought(or oversold) conditions and allows for mean reversion trades, but this fails when there is a big move, such as the examples included. I dont mind, having a couple of ZScore studies, one for normal conditions with 2-3 as overbought/oversold and another for big moves, but either way, need the Zscore plot to go beyond the the current max values shown.
 
Last edited by a moderator:

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

@BenTen
1. Correct, massive divergence...which defeats the very purpose of me using this indicator to capture/show normalized price volatility, without such drastic divergence. I'm aware of the reason why its happening..the first one or two candles , being relatively huge, push the avgClose too high(or low) and the successive candle's Zscorevalue are measured relative to this high/low avgClose , ends up being incrementally small..,causing Zscorevalue to reverse, much earlier than the price,leading to huge divergence.

Understand this is the expected behavior of this indicator in its current form..,as it shows the difference between the current price and its moving average,normalized by the Standard deviation.
Bollinger bands as an upper study do potray the unbounced volatility...,but for mean reversion trades, Zscore is one of the indicators I tend to use (as lower study) and it works well in "normal volatility" conditions, but when there is big breakout like that, tends to distort hugely, making it useless.....

Looks like I need a cross between Zscore and BollingerBand Oscillator study..

2. No , during regular market hours, as shown.

Thanks!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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