[Pandora] Vast Volatility Treasure TroveINTRODUCTION:
Volatility enthusiasts, prepare for VICTORY on this day of July 4th, 2024! This is my "Vast Volatility Treasure Trove," intended mostly for educational purposes, yet these functions will also exhibit versatility when combined with other algorithms to garner statistical excellence. Once again, I am now ripping the lid off of Pandora's box... of volatility. Inside this script is a 'vast' collection of volatility estimators, reflecting the indicators name. Whether you are a seasoned trader destined to navigate financial strife or an eagerly curious learner, this script offers a comprehensive toolkit for a broad spectrum of volatility analysis. Enjoy your journey through the realm of market volatility with this code!
WHAT IS MARKET VOLATILITY?:
Market volatility refers to various fluctuations in the value of a financial market or asset over a period of time, often characterized by occasional rapid and significant deviations in price. During periods of greater market volatility, evolving conditions of prices can move rapidly in either direction, creating uncertainty for investors with results of sharp declines as well as rapid gains. However, market volatility is a typical aspect expected in financial markets that can also present opportunities for informed decision-making and potential benefits from the price flux.
SCRIPT INTENTION:
Volatility is assuredly omnipresent, waxing and waning in magnitude, and some readers have every intention of studying and/or measuring it. This script serves as an all-in-one armada of volatility estimators for TradingView members. I set out to provide a diverse set of tools to analyze and interpret market volatility, offering volatile insights, and aid with the development of robust trading indicators and strategies.
In today's fast-paced financial markets, understanding and quantifying volatility is informative for both seasoned traders and novice investors. This script is designed to empower users by equipping them with a comprehensive suite of volatility estimators. Each function within this script has been meticulously crafted to address various aspects of volatility, from traditional methods like Garman-Klass and Parkinson to more advanced techniques like Yang-Zhang and my custom experimental algorithms.
Ultimately, this script is more than just a collection of functions. It is a gateway to a deeper understanding of market volatility and a valuable resource for anyone committed to mastering the complexities of financial markets.
SCRIPT CONTENTS:
This script includes a variety of functions designed to measure and analyze market volatility. Where applicable, an input checkbox option provides an unbiased/biased estimate. Below is a brief description of each function in the original order they appear as code upon first publish:
Parkinson Volatility - Estimates volatility emphasizing the high and low range movements.
Alternate Parkinson Volatility - Simpler version of the original Parkinson Volatility that I realized.
Garman-Klass Volatility - Estimates volatility based on high, low, open, and close prices using a formula that adjusts for biases in price dynamics.
Rogers-Satchell-Yoon Volatility #1 - Estimates volatility based on logarithmic differences between high, low, open, and close values.
Rogers-Satchell-Yoon Volatility #2 - Similar estimate to Rogers-Satchell with the same result via an alternate formulation of volatility.
Yang-Zhang Volatility - An advanced volatility estimate combining both strengths of the Garman-Klass and Rogers-Satchell estimators, with weights determined by an alpha parameter.
Yang-Zhang (Modified) Volatility - My experimental modification slightly different from the Yang-Zhang formula with improved computational efficiency.
Selectable Volatility - Basic customizable volatility calculation based on the logarithmic difference between selected numerator and denominator prices (e.g., open, high, low, close).
Close-to-Close Volatility - Estimates volatility using the logarithmic difference between consecutive closing prices. Specifically applicable to data sources without open, high, and low prices.
Open-to-Close Volatility - (Overnight Volatility): Estimates volatility based on the logarithmic difference between the opening price and the last closing price emphasizing overnight gaps.
Hilo Volatility - Estimates volatility using a method similar to Parkinson's method, which considers the logarithm of the high and low prices.
Vantage Volatility - My experimental custom 'vantage' method to estimate volatility similar to Yang-Zhang, which incorporates various factors (Alpha, Beta, Gamma) to generate a weighted logarithmic calculation. This may be a volatility advantage or disadvantage, hence it's name.
Schwert Volatility - Estimates volatility based on arithmetic returns.
Historical Volatility - Estimates volatility considering logarithmic returns.
Annualized Historical Volatility - Estimates annualized volatility using logarithmic returns, adjusted for the number of trading days in a year.
If I omitted any other known varieties, detailed requests for future consideration can be made below for their inclusion into this script within future versions...
BONUS ALGORITHMS:
This script also includes several experimental and bonus functions that push the boundaries of volatility analysis as I understand it. These functions are designed to provide additional insights and also are my ideal notions for traders looking to explore other methods of volatility measurement.
VOLATILITY APPLICATIONS:
Volatility estimators serve a common role across various facets of trading and financial analysis, offering insights into market behavior. These tools are already in instrumental with enhancing risk management practices by providing a deeper understanding of market dynamics and the inherent uncertainty in asset prices. With volatility estimators, traders can effectively quantifying market risk and adjust their strategies accordingly, optimizing portfolio performance and mitigating potential losses. Additionally, volatility estimations may serve as indication for detecting overbought or oversold market conditions, offering probabilistic insights that could inform strategic decisions at turning points. This script
distinctly offers a variety of volatility estimators to navigate intricate financial terrains with informed judgment to address challenges of strategic planning.
CODE REUSE:
You don't have to ask for my permission to use/reuse these functions in your published scripts, simply because I have better things to do than answer requests for the reuse of these functions.
Notice: Unfortunately, I will not provide any integration support into member's projects at all. I have my own projects that require way too much of my day already.
Historicalvolatity
Historical Volatility StudyThe goal of this script it to provide you an idea to forecast the future momentum by looking at historical volatility.
This chart has basically three parts.
1. Three lines are there. The multi color line represents the historical annualized volatility in terms of minimum look back period . The white line represents the historical annualized volatility in terms of medium term look back period . The green line represents the historical annualized volatility in terms of longer term look back period .
2. The back ground color has three components. Green zone is the zone where overall volatility is on the lower side. Red zone is the zone where overall volatility is on the higher side. Purple zone means fluctuating volatility.
3. The multi color line has three colors. Red color means volatility moving towards extreme low. Yellow means it is moving towards extreme high. Purple means it is in normal course of action.
This tool can be used as a confirmation tool with other studies to aid you to make better decisions. For example- look at the diagram below.
Make your thorough study before making any trading decision. Thanks.
JFD-Adaptive, GKYZ-Filtered KAMA [Loxx]JFD-Adaptive, GKYZ-Filtered KAMA is a Kaufman Adaptive Moving Average with the option to make it Jurik Fractal Dimension Adaptive. This also includes a Garman-Klass-Yang-Zhang Historical Volatility Filter to reduce noise.
What is KAMA?
Developed by Perry Kaufman, Kaufman's Adaptive Moving Average ( KAMA ) is a moving average designed to account for market noise or volatility . KAMA will closely follow prices when the price swings are relatively small and the noise is low. KAMA will adjust when the price swings widen and follow prices from a greater distance. This trend-following indicator can be used to identify the overall trend, time turning points and filter price movements.
What is Jurik Fractal Dimension?
There is a weak and a strong way to measure the random quality of a time series.
The weak way is to use the random walk index ( RWI ). You can download it from the Omega web site. It makes the assumption that the market is moving randomly with an average distance D per move and proposes an amount the market should have changed over N bars of time. If the market has traveled less, then the action is considered random, otherwise it's considered trending.
The problem with this method is that taking the average distance is valid for a Normal (Gaussian) distribution of price activity. However, price action is rarely Normal, with large price jumps occuring much more frequently than a Normal distribution would expect. Consequently, big jumps throw the RWI way off, producing invalid results.
The strong way is to not make any assumption regarding the distribution of price changes and, instead, measure the fractal dimension of the time series. Fractal Dimension requires a lot of data to be accurate. If you are trading 30 minute bars, use a multi-chart where this indicator is running on 5 minute bars and you are trading on 30 minute bars.
What is Garman-Klass-Yang-Zhang Historical Volatility?
Yang and Zhang derived an extension to the Garman Klass historical volatility estimator that allows for opening jumps. It assumes Brownian motion with zero drift. This is currently the preferred version of open-high-low-close volatility estimator for zero drift and has an efficiency of 8 times the classic close-to-close estimator. Note that when the drift is nonzero, but instead relative large to the volatility , this estimator will tend to overestimate the volatility . The Garman-Klass-Yang-Zhang Historical Volatility calculation is as follows:
GKYZHV = sqrt((Z/n) * sum((log(open(k)/close( k-1 )))^2 + (0.5*(log(high(k)/low(k)))^2) - (2*log(2) - 1)*(log(close(k)/open(2:end)))^2))
Included
Alerts
Signals
Loxx's Expanded Source Types
Bar coloring
Roger & Satchell Estimator Historical Volatility Bands [Loxx]Roger & Satchell Estimator Historical Volatility Bands are constructed using:
Average as the middle line.
Upper and lower bands using theRoger & Satchell Estimator Historical Volatility Bands for bands calculation.
What is Roger & Satchell Estimator Historical Volatility?
The Rogers–Satchell estimator does not handle opening jumps; therefore, it underestimates the volatility. It accurately explains the volatility portion that can be attributed entirely to a trend in the price evolution. Rogers and Satchell try to embody the frequency of price observations in the model in order to overcome the drawback. They claim that the corrected estimator outperforms the uncorrected one in a study based on simulated data.
RSEHV = sqrt((Z/n) * sum((log(high/close)*log(high/open)) + (log(low/close)*log(low/open))))
The color of the middle line, unlike the bands colors, has 3 colors. When colors of the bands are the same, then the middle line has the same color, otherwise it's white.
Included
Alerts
Signals
Loxx's Expanded Source Types
Bar coloring
Garman-Klass-Yang-Zhang Historical Volatility Bands [Loxx]Garman-Klass-Yang-Zhang Historical Volatility Bands are constructed using:
Average as the middle line.
Upper and lower bands using the Garman-Klass-Yang-Zhang Historical Volatility Bands for bands calculation.
What is Garman-Klass-Yang-Zhang Historical Volatility?
Yang and Zhang derived an extension to the Garman Klass historical volatility estimator that allows for opening jumps. It assumes Brownian motion with zero drift. This is currently the preferred version of open-high-low-close volatility estimator for zero drift and has an efficiency of 8 times the classic close-to-close estimator. Note that when the drift is nonzero, but instead relative large to the volatility, this estimator will tend to overestimate the volatility. The Garman-Klass-Yang-Zhang Historical Volatility calculation is as follows:
GKYZHV = sqrt((Z/n) * sum((log(open(k)/close(k-1)))^2 + (0.5*(log(high(k)/low(k)))^2) - (2*log(2) - 1)*(log(close(k)/open(2:end)))^2))
The color of the middle line, unlike the bands colors, has 3 colors. When colors of the bands are the same, then the middle line has the same color, otherwise it's white.
Included
Alerts
Signals
Loxx's Expanded Source Types
Bar coloring
Related Indicators
Garman & Klass Estimator Historical Volatility Bands
Garman & Klass Estimator Historical Volatility Bands [Loxx]Garman & Klass Estimator Historical Volatility Bands are constructed using:
Average as the middle line.
Upper and lower bands using the Garman & Klass Estimator Historical Volatility (instead of "regular" Historical Volatility ) for bands calculation.
What is Garman & Klaus Historical Volatility?
Garman Klass is a volatility estimator that incorporates open, low, high, and close prices of a security. The Garman and Klass estimator for estimating historical volatility assumes Brownian motion with zero drift and no opening jumps (i.e. the opening = close of the previous period). This estimator is 7.4 times more efficient than the close-to-close estimator. Garman-Klass volatility extends Parkinson's volatility by taking into account the opening and closing price. As markets are most active during the opening and closing of a trading session, it makes volatility estimation more accurate. Garman and Klass also assumed that the process of price change is a process of continuous diffusion (geometric Brownian motion). However, this assumption has several drawbacks. The method is not robust for opening jumps in price and trend movements. Despite its drawbacks, the Garman-Klass estimator is still more effective than the basic formula since it takes into account not only the price at the beginning and end of the time interval but also intraday price extremums.
The Garman & Klass Estimator is as follows:
GKE = sqrt((Z/n)* sum((0.5*(log(high./low)).^2) - (2*log(2) - 1).*(log(close./open)).^2))
The color of the middle line, unlike the bands colors, has 3 colors. When colors of the bands are the same, then the middle line has the same color, otherwise it's white.
Included
Alerts
Signals
Loxx's Expanded Source Types
Bar coloring
Related indicators:
Parkinson's Historical Volatility Bands
High/Low Historical Volatility Bands [Loxx]High/Low Historical Volatility Bands are constructed using:
Average as the middle line.
Upper and lower bands using the Historical Volatility high/low (instead of "regular" Historical Volatility) for bands calculation.
What is Historical Volatility?
Historical Volatility (HV) is a statistical measure of the dispersion of returns for a given security or market index over a given period of time. Generally, this measure is calculated by determining the average deviation from the average price of a financial instrument in the given time period. Using standard deviation is the most common, but not the only, way to calculate Historical Volatility .
The higher the Historical Volatility value, the riskier the security. However, that is not necessarily a bad result as risk works both ways - bullish and bearish , i.e: Historical Volatility is not a directional indicator and should not be used as other directional indicators are used. Use to to determine the rising and falling price change volatility .
SH is stock's High price in t day.
SL is stock's Low price in t day.
High/Low Return (xt^HL) is calculated as the natural logarithm of the ratio of a stock's High price to stock's Low price.
Return:
And Parkinson's number: 1 / (4 * math.log(2)) * 252 / n * Σ (n, t =1) {math.log(Ht/Lt)^2}
An important use of the Parkinson's number is the assessment of the distribution prices during the day as well as a better understanding of the market dynamics. Comparing the Parkinson's number and periodically sampled volatility helps traders understand the tendency towards mean reversion in the market as well as the distribution of stop-losses.
The color of the middle line, unlike the bands colors, has 3 colors. When colors of the bands are the same, then the middle line has the same color, otherwise it's white.
Included
Alerts
Signals
Loxx's Expanded Source Types
Bar coloring
Related indicators:
Parkinson's Historical Volatility Bands
Historical Volatility Bands
Parkinson's Historical Volatility Bands [Loxx]Parkinson's Historical Volatility Bands are constructed using:
Average as the middle line.
Upper and lower bands using the Parkinson's historical volatility (instead of "regular" Historical Volatility) for bands calculation.
What is Parkinson's Historical Volatility?
The Parkinson's number, or High Low Range Volatility developed by the physicist, Michael Parkinson in 1980, aims to estimate the Volatility of returns for a random walk using the High and Low in any particular period. IVolatility.com calculates daily Parkinson values. Prices are observed on a fixed time interval: n = 10, 20, 30, 60, 90, 120, 150, 180 days.
SH is stock's High price in t day.
SL is stock's Low price in t day.
High/Low Return (xt^HL) is calculated as the natural logarithm of the ratio of a stock's High price to stock's Low price.
Return:
And Parkinson's number: 1 / (4 * math.log(2)) * 252 / n * Σ (n, t =1) {math.log(Ht/Lt)^2}
An important use of the Parkinson's number is the assessment of the distribution prices during the day as well as a better understanding of the market dynamics. Comparing the Parkinson's number and periodically sampled volatility helps traders understand the tendency towards mean reversion in the market as well as the distribution of stop-losses.
The color of the middle line, unlike the bands colors, has 3 colors. When colors of the bands are the same, then the middle line has the same color, otherwise it's white.
Included
Alerts
Signals
Loxx's Expanded Source Types
Bar coloring
Historical Volatility Bands [Loxx]Historical Volatility Bands are constructed using:
Average as the middle line.
Upper and lower bands using the Historical Volatility for bands calculation.
What is Historical Volatility?
Historical Volatility (HV) is a statistical measure of the dispersion of returns for a given security or market index over a given period of time. Generally, this measure is calculated by determining the average deviation from the average price of a financial instrument in the given time period. Using standard deviation is the most common, but not the only, way to calculate Historical Volatility.
The higher the Historical Volatility value, the riskier the security. However, that is not necessarily a bad result as risk works both ways - bullish and bearish, i.e: Historical Volatility is not a directional indicator and should not be used as other directional indicators are used. Use to to determine the rising and falling price change volatility.
The color of the middle line, unlike the bands colors, has 3 colors. When colors of the bands are the same, then the middle line has the same color, otherwise it's white.
Included
Alerts
Signals
Loxx's Expanded Source Types
Bar coloring
LS Volatility Index█ OVERVIEW
This indicator serves to measure the volatility of the price in relation to the average.
It serves four purposes:
1. Identify abnormal prices, extremely stretched in relation to an average;
2. Identify acceptable prices in the context of the main trend;
3. Identify market crashes;
4. Identify divergences.
█ CONCEPTS
The LS Volatility Index was originally described by Brazilian traders Alexandre Wolwacz (Stormer) , Fabrício Lorenz , and Fábio Figueiredo (Vlad)
Basically, this indicator can be used in two ways:
1. In a mean reversion strategy , when there is an unusual distance from it;
2. In a trend following strategy , when the price is in an acceptable region.
Perhaps the version presented here may have some slight differences, but the core is the same.
The original indicator is presented with a 21-period moving average, but here this value is customizable.
I made some fine tuning available, namely:
1. The possibility of smoothing the indicator;
2. Choose the type of moving average;
3. Customizable period;
4. Possibility to show a moving average of the indicator;
5. Color customization.
█ CALCULATION
First, the distance of the price from a given average in percentage terms is measured.
Then, the historical average volatility is obtained.
Finally the indicator is calculated through the ratio between the distance and the historical volatility.
To facilitate visualization, the result is normalized in a range from 0 to 100.
When it reaches 0, it means the price is on average.
When it hits 100, it means the price is way off average (stretched).
█ HOW TO USE IT
Here are some examples:
1. In a return-to-average strategy
2. In a trend following strategy
3. Identification of crashes and divergences
█ THANKS AND CREDITS
- Alexandre Wolwacz (Stormer), Fabrício Lorenz, Fábio Figueiredo (Vlad)
- Feature scaler (for normalization)
- HPotter (for calc of Historical Volatility)