# Tos Code: IV Rank & IV Percentile Overlayed (Will Work For Futures)

9 Jun

————————START BELOW THIS LINE————————–
#
# Michael Rechenthin, Ph.D.
#
# IV Rank is a description of where the current IV lies in comparison
# to its yearly high and low IV
#
# IV Percentile gives the percentage of days over the last year, that
# were below the current IV
#
# For information on the two, see Skinny on Options Data Science,
# titled “IV Rank and IV Percentile”
# http://ontt.tv/1Nt4fcS
#
# version 2.1
#
declare lower;
declare hide_on_intraday; # do not display when using intra-day plots
input days_back = 252; # it is most common to use 1-year (or 252 trading days)

# implied volatility
# using proxies for futures
def df = if (GetSymbol() == “/ES”) then close(“VIX”) / 100
else if (GetSymbol() == “/CL”) then close(“OIV”) / 100
else if (GetSymbol() == “/GC”) then close(“GVX”) / 100
else if (GetSymbol() == “/SI”) then close(“VXSLV”) / 100
else if (GetSymbol() == “/NQ”) then close(“VXN”) / 100
else if (GetSymbol() == “/TF”) then close(“RVX”) / 100
else if (GetSymbol() == “/YM”) then close(“VXD”) / 100
else if (GetSymbol() == “/6E”) then close(“EVZ”) / 100
else if (GetSymbol() == “/6J”) then close(“JYVIX”) / 100
else if (GetSymbol() == “/6B”) then close(“BPVIX”) / 100
else if (GetSymbol() == “/ZN”) then close(“TYVIX”) / 100
else imp_volatility();

def df1 = if !IsNaN(df) then df else df[-1];

# display regular implied volatility
# —————————
AddLabel(yes, “IV: ” + Round(df1 * 100.0, 0), Color.ORANGE);

# calculate the IV rank
# —————————
# calculate the IV rank
def low_over_timespan = Lowest(df1, days_back);
def high_over_timespan = Highest(df1, days_back);

def iv_rank = Round( (df1 – low_over_timespan) / (high_over_timespan – low_over_timespan) * 100.0, 0);
plot IVRank = iv_rank;
IVRank.SetDefaultColor(Color.GRAY);
AddLabel(yes, “IV Rank: ” + iv_rank, Color.GRAY);

# calculate the IV percentile
# —————————
# how many times over the past year, has IV been below the current IV
def counts_below = fold i = 1 to days_back + 1 with count = 0
do
if df1[0] > df1[i] then
count + 1
else
count;

def iv_percentile = Round(counts_below / days_back * 100.0, 0);
plot IVPercentile = iv_percentile;
IVPercentile.SetDefaultColor(Color.RED);
AddLabel(yes, “IV Percentile: ” + iv_percentile + “% of days (over the past year) were below the current IV”, Color.RED);