Implied vs Historical Volatility Comparison For ThinkOrSwim

Philly1224

New member
VIP
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.
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;

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.

ImpvsHistVol.png


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.
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
461 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top