Implied vs Historical Volatility Comparison Indicator (monthly and yearly)
I was frustrated with ToS that when I combined the IV indicator with HV indicators onto the same chart, the scaling would never align correctly for easy comparisons.
Luckily, Hahn Tech developed a method for doing just this. He provides the code for free. I took their indicator and tweaked it a bit.
Their indicator only compared the current IV to the monthly HV30. I added in the yearly HV252 into the code so that comparisons can be made to show not only where the IV is to the monthly HV, but also the yearly HV.
Credit goes to Pete Hahn .... I just added the HV252 into the chart as well to compare for the year on the same graph.
Scans can also be made according to instructions in the video, and I also added into the code the lines which would be needed to compare IV to HV252 for scans too.
In the picture, the Cyan line is the current IV, the purple line is the HV20 (monthly), and the yellow line is the HV252 (yearly). It goes by the trading days, so 20 and 252 are monthly trading days and yearly respectively.
This can give a trader a quick look to see whether the current IV is relatively high/low compared to historical volatility.
It's a great tool for options traders.
The video to describe this code is here: Historical Implied Volatility Video
Below is the thinkscript:
I was frustrated with ToS that when I combined the IV indicator with HV indicators onto the same chart, the scaling would never align correctly for easy comparisons.
Luckily, Hahn Tech developed a method for doing just this. He provides the code for free. I took their indicator and tweaked it a bit.
Their indicator only compared the current IV to the monthly HV30. I added in the yearly HV252 into the code so that comparisons can be made to show not only where the IV is to the monthly HV, but also the yearly HV.
Credit goes to Pete Hahn .... I just added the HV252 into the chart as well to compare for the year on the same graph.
Scans can also be made according to instructions in the video, and I also added into the code the lines which would be needed to compare IV to HV252 for scans too.
In the picture, the Cyan line is the current IV, the purple line is the HV20 (monthly), and the yellow line is the HV252 (yearly). It goes by the trading days, so 20 and 252 are monthly trading days and yearly respectively.
This can give a trader a quick look to see whether the current IV is relatively high/low compared to historical volatility.
It's a great tool for options traders.
The video to describe this code is here: Historical Implied Volatility Video
Below is the thinkscript:
Code:
# This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
# Author: Pete Hahn (Hahn-Tech, LLC)
# tutorial video: https://www.hahn-tech.com/thinkorswim-historical-implied-volatility/
declare lower;
declare hide_on_intraday;
input length = 20;
input length2 = 252;
input basis = {default Annual, Monthly, Weekly, Daily};
input volatilitySpreadMultiplier = 2.0;
def ap = getAggregationPeriod();
Assert(ap >= AggregationPeriod.MIN, "Study can only be calculated for time-aggregated charts: " + ap);
def barsPerDay = (RegularTradingEnd(getYyyyMmDd()) - RegularTradingStart(getYyyyMmDd())) / ap;
def barsPerYear =
if ap > AggregationPeriod.WEEK then 12
else if ap == AggregationPeriod.WEEK then 52
else if ap >= AggregationPeriod.DAY then 252 * AggregationPeriod.DAY / ap
else 252 * barsPerDay;
def basisCoeff;
switch (basis) {
case Annual:
basisCoeff = 1;
case Monthly:
basisCoeff = 12;
case Weekly:
basisCoeff = 52;
case Daily:
basisCoeff = 252;
}
def clLog = log(close / close[1]);
def wklyPrctMove = round( ( highest(high, 5) - lowest(low, 5) ) / lowest(low,5), 3) * 100;
AddLabel(1,concat("H/L Wk: ", wklyPrctMove), if wklyPrctMove >= 5 then color.RED else color.GREEN);
def mthlyPrctMove = round( ( highest(high, 21) - lowest(low,21) ) / lowest(low,21), 3) * 100;
AddLabel(1,concat("H/L Mth: ", mthlyPrctMove), if mthlyPrctMove >= 10 then color.RED else color.GREEN);
def impVol = IMP_VOLATILITY();
def hv = stdev(clLog, length) * Sqrt(barsPerYear / basisCoeff * length / (length - 1));
def hv2 = stdev(clLog, length2) * Sqrt (barsPerYear / basisCoeff * length2 / (length2 - 1));
# to use in scan mode, place '#' in front of these two plot statements
plot impVolLine = impVol;
plot histVolLine = hv ;
plot histVolLine2 = hv2 ;
def doubleVolSpread = impVol > hv * volatilitySpreadMultiplier;
def doubleVolSpread2 = impvol > hv2 * volatilitySpreadMultiplier;
def signal = doubleVolSpread and highest(doubleVolSpread[1], 10) < 1;
def signal2 = doublevolspread2 and highest (doublevolspread2[1], 10) < 1;
def check = impVol != Double.NEGATIVE_INFINITY;
def check2 = impVol != Double.POSITIVE_INFINITY;
# use ONE of these three plot statements in scan mode. After commenting out
# the two plot statments above, select one of the three below and remove the
# '#' symbol from in front of the plot statment
# use this to find stocks where Implied Vol has reached two times Hist Vol
#plot scan = if signal and check and check2 then 1 else Double.NaN;
#plot scan = if signal2 and check and check2 then 1 else Double.NaN;
# use this to find stocks where Implied Vol has crossed above Hist Vol
#plot scan = impVol > hv and impVol[1] < hv[1] and check and check2;
#plot scan = impVol > hv2 and impVol[1] < hv2[1] and check and check2;
# use this to find stocks where Implied Vol has crossed below Hist Vol
#plot scan = impVol < hv and impVol[1] > hv[1] and check and check2;
#plot scan = impVol < hv2 and impVol[1] < hv2[1] and check and check2;
Last edited by a moderator: