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).