OPEN-SOURCE SCRIPT

vol_ranges

Updated
This script shows three measures of volatility:

historical (hv): realized volatility of the recent past
median (mv): a long run average of realized volatility
implied (iv): a user-defined volatility

Historical and median volatility are based on the EWMA, rather than standard deviation, method of calculating volatility. Since Tradingview's built in ema function uses a window, the "window" parameter determines how much historical data is used to calculate these volatility measures. E.g. 30 on a daily chart means the previous 30 days.

The plots above and below historical candles show past projections based on these measures. The "periods to expiration" dictates how far the projection extends. At 30 periods to expiration (default), the plot will indicate the one standard deviation range from 30 periods ago. This is calculated by multiplying the volatility measure by the square root of time. For example, if the historical volatility (hv) was 20% and the window is 30, then the plot is drawn over: close[30] * 1.2 * sqrt(30/252).

At the most recent candle, this same calculation is simply drawn as a line projecting into the future.

This script is intended to be used with a particular options contract in mind. For example, if the option expires in 15 days and has an implied volatility of 25%, choose 15 for the window and 25 for the implied volatility options. The ranges drawn will reflect the two standard deviation range both in the future (lines) and at any point in the past (plots) for HV (blue), MV (red), and IV (grey).
Release Notes
since iv is not based on historical data, removed the plots. this makes the output more legible.
Release Notes
Fixed a bug with the plots (not lines) which were using the window input (rather than periods to expiration). The plots are supposed to show the projected 2 SD range from "periods to expiration" ago. This allows you to see how often the current projection failed/succeeded in the past.
Release Notes
This is a substantial upgrade over the original script. I wish I could rename it to "vol_models". The idea is to test different volatility forecasts. Right now there are three volatility models:

historical (hv): a ema-derived volatility using a fixed window of historical returns.
median historical (mv): the median of a fixed-range of hv samples.
implied volatility (iv): a static implied volatility value that you derived elsewhere (e.g. copy/paste from your broker)

I hope to add more models in the future. The new features in this version are:

- Better comments and naming, to make the script more readable.
- The forecast plots are disabled by default. Use the settings to enable the ones you want to see.
- There is a table that shows how many periods price "breached" the forecast, as percentage and total. Every bar should have a forecast for hv and iv, but mv is limited to 4999 forecasts due to pinescript's limits.
- You can adjust the number of standard deviations to use in the forecast. This input replaces the 1 SD option from previous versions.
- The forecasts now use 365 days per year, not 252.
Release Notes
improvements in this version:

- theoretical values added to the table
- blended volatility model
- added a discount rate input
- (arguably) better names and comments, to make the code more readable

the theoretical values are computed by assuming a call was sold at a strike equal to each upper forecast, or a put at each lower forecast. If the closing price is above (below) the forecast, the contract is in the money; otherwise, it is worthless. the table shows the average value, at expiration, of a call ("t") or put ("b"), discounted to present value using the discount rate input.

the "blended volatility" (bv) is the more conservative of each historical (hv) and median historical (mv) forecast. for example, if the upper hv forecast is greater than the upper mv forecast, the upper bv forecast is equal to the upper hv forecast. there is no point drawing a line for this forecast, since it is obvious from looking at the hv and mv lines, but you can see the historical plots.
Release Notes
two main changes:

- normalized the theoretical values, which should make them much more accurate.
- changed some input defaults:
- range from 4999 to 365 (making it more compatible with most tickers)
- IV to 0, which the only default that is relevant for all tickers (since it shows the straddle)
historicalimpliedmedianoptionsrangeVolatility

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer