TradingView
xel_arjona
2017年8月25日午後10時30分

OHLC Volatility Estimators by @Xel_arjona 

S&P 500SP

詳細

DISCLAIMER:

The Following indicator/code IS NOT intended to be a formal investment advice or recommendation by the author, nor should be construed as such. Users will be fully responsible by their use regarding their own trading vehicles/assets.

The embedded code and ideas within this work are FREELY AND PUBLICLY available on the Web for NON LUCRATIVE ACTIVITIES and must remain as is by Creative-Commons as TradingView's regulations. Any use, copy or re-use of this code should mention it's origin as it's authorship.


WARNING NOTICE!

THE INCLUDED FUNCTION MUST BE CONSIDERED AS DEBUGING CODE The models included in the function have been taken from openly sources on the web so they could have some errors as in the calculation scheme and/or in it's programatic scheme. Debugging are welcome.


WHAT'S THIS?

Here's a full collection of candle based (compressed tick) Volatility Estimators given as a function, openly available for free, it can print IMPLIED VOLATILITY by an external symbol ticker like INDEX:VIX.

Models included in the volatility calculation function:

  • CLOSE TO CLOSE: This is the classic estimator by rule, sometimes referred as HISTORICAL VOLATILITY and is the must common, accepted and widely used out there. Is based on traditional Standard Deviation method derived from the logarithm return of current close from yesterday's.

  • ELASTIC WEIGHTED MOVING AVERAGE: This estimator has been used by RiskMetriks®. It's calculation is based on an ElasticWeightedMovingAverage Standard Deviation method derived from the logarithm return of current close from yesterday's. It can be viewed or named as an EXPONENTIAL HISTORICAL VOLATILITY model.

  • PARKINSON'S: The Parkinson 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.

  • ROGERS-SATCHELL: The Rogers-Satchell function is a volatility estimator that outperforms other estimators when the underlying follows a Geometric Brownian Motion (GBM) with a drift (historical data mean returns different from zero). As a result, it provides a better volatility estimation when the underlying is trending. However, this Rogers-Satchell estimator does not account for jumps in price (Gaps). It assumes no opening jump. The function uses the open, close, high, and low price series in its calculation and it has only one parameter, which is the period to use to estimate the volatility.

  • YANG-ZHANG: Yang and Zhang were the first to derive an historical volatility estimator that has a minimum estimation error, is independent of the drift, and independent of opening gaps. This estimator is maximally 14 times more efficient than the close-to-close estimator.

  • LOGARITHMIC GARMAN-KLASS: The former is a pinescript transcript of the model defined as in iVolatility. The metric used is a combination of the overnight, high/low and open/close range. Such a volatility metric is a more efficient measure of the degree of volatility during a given day. This metric is always positive.

リリースノート

- Removed "Compound Volatility" as boolean and replaced with algorithm in function. If you do not want aompund at all, you need to specify Compund Periods as 0.

リリースノート

  • Volatility Function algos fix & clean up. - Weight values (lambda) has been hardcoded at EWMA and LGK models to approach Lookback Window Definition in favor to aproximate 0.94 for 30 days back volatility for EWMA (RiskMetrics) and 0.90 for LGK (iVolatility.com) as defined from original authors.
  • Logarithmic Garman-Klass Algorithm (apparent) Fix. - Now it plots from any symbol without returning zero values. (Experimented Math users please help confirm / Debug this issue).
  • Replaced old "Implied Volatility" mode with an "External Symbol" approach model in order to select any of the Volatility Models defined by the function to calculate Volatility from any External Symbol (Index). As an example: From GDJ calculate Historical/Realized volatility from any model from INDEX:HUI, or just use RAW close values in SPY from INDEX:VIX as an Implied calculated value.
  • As from the Volatility function cleanup, Menus are more cleaner for customization selections.


CHEERS!
コメント
unelma
only one thing mate, the EWMA risk metrics assumes that variance= squared return. So why in the formula you do the following:
lambda* power( previous squared return - average volatility,2) ?
actually it should only be lambda* power( previous squared return,2) as average volatility is assumed to be = average squared return, but returns are assumed to be 0 on average.
Simply put, could you eliminate the "minus average volatility" term? or maybe just add an option to do so.
That would be very appreciated, many thanks.
xel_arjona
@unelma, From my understanding reading lot's of sources about the model, the use of yesterday's return is because of the estimation nature. You could review yourself this both mathematically from iVolatility.com definition formula ( ivolatility.com/help/6.html ) and directly from a widely and accepted used python library from this listing: (lines 1318 and 1367 github.com/bashtage/arch/blob/master/arch/univariate/volatility.py ) Basically RiskMetrics is Close-To-Close but exponentially averaged with the EWMA generalized formula: EWMA = weight * YesterdaysEWMA + (1-weight) * array ; where "array" is yesterday squared return as today estimation and weight must be > 0 and <= 1. RiskMetrics use a constant fixed weight of 0.94 (6%). If you want to not use error (squared returns - average of squared returns) you could define it unchecking the "mt" boolean input. Basically you will have lil to less difference, but I decided to put on formula for anyone using large windows of returns. The only algorithm doing strange things only with some symbols is the Logarithmic-Garman-Klass, but don't know if is a bad code transcript from myself or a TradingView's pine engine bug or problem. Cheers!
unelma
@xel_arjona, yes exactly I agree with you on everything. My question was, why then you are using this code? (1-lambda)*pow(ln_cc[1]-avg_cc, 2)+lambda*nz(var_ewma[1],var_cc)
avg_cc should not be there if as you mentioned the Emma formula is EWMA = weight * YesterdaysEWMA + (1-weight) * array
It should be weight * YesterdaysEWMA and not weight * (YesterdaysEWMA- variance) as per your code.
xel_arjona
@unelma, And again I mentioned you that it was for usability. If you want to calculate with pure RAW returns (With no error algorithm) then you have the option at the only boolean settings the indicator have: "Use Median-Error for Variance:". If you uncheck it, you will have your plain (ln_cc - 0) which is equal to have ln_cc only.
unelma
@xel_arjona, alright, many thanks mate. May i ask you where i can find the %vRSI used in the chart?
xel_arjona
@unelma, The %vRSI is still a work in progress indicator that have been tweaked with good help of @glaz. I'll publish it when I feel happy with maths involved.
unelma
very good job, I was looking for an EMWA volatility indicator. Many thanks mate !
syracusepro
Also for Lambda? Thanks.
xel_arjona
@syracusepro, Volatility is a monster theme. You can search for it at google and find lot's and lot's and lot's of info. Regarding this models, *window is the same as "n" in traditoinal formulas (openly and fastly found on google) which counts for periods used in the sum process of standard deviations. "N" (uppercase) is used for compounded function, for example if you want to Annualize de volatility, it should multiply the standard deviation of the periods used (typically 21 days) against the square root of N (typically 252 trading days for stocks).
xel_arjona
@syracusepro, Lambda is a weight factor used for EWMA. EWMA is an average math method that give more enfasis on the last period given this lambda weight. The weight itself must follow a percentille rule in which: 0 < lambda < 1. RiskMetrik's use 0.94 as default for all weightings.
詳細