```
declare lower;
input ShowClouds = yes;
# ---- Historical Volatility Code ----
input length = 20;
input basis = {default Annual, Monthly, Weekly, Daily};
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 HV = StDev(clLog, length) * Sqrt(barsPerYear / basisCoeff * length / (length - 1));
#HV.SetDefaultColor(Color.CYAN);
def HVHIGH = Highest(data = HV, length = 252);
#HVHIGH.SetDefaultColor(Color.RED);
def HVLOW = Lowest(HV, 252);
#HVLOW.SetDefaultColor(Color.DARK_GREEN);
def Difference = (HVHIGH - HVLOW) / 5;
# ---- Plotting IV Levels ----
plot HV1to2 = HVLOW + (Difference * 1);
plot HV2to3 = HVLOW + (Difference * 2);
plot HV3to4 = HVLOW + (Difference * 3);
plot HV4to5 = HVLOW + (Difference * 4);
HV1to2.SetDefaultColor(Color.WHITE);
HV2to3.SetDefaultColor(Color.WHITE);
HV3to4.SetDefaultColor(Color.WHITE);
HV4to5.SetDefaultColor(Color.WHITE);
HV1to2.SetStyle(Curve.SHORT_DASH);
HV2to3.SetStyle(Curve.SHORT_DASH);
HV3to4.SetStyle(Curve.SHORT_DASH);
HV4to5.SetStyle(Curve.SHORT_DASH);
# ---- Clouds ---
AddCloud(If showclouds then HVLOW else Double.NaN, if showclouds then HV1to2 else Double.NaN, Color.GREEN, Color.GREEN);
AddCloud(if showclouds then HV1to2 else Double.NaN, if showclouds then HV2to3 else double.nan, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud(If showclouds then HV2to3 else Double.NaN, if showclouds then HV3to4 else double.nan, Color.YELLOW, Color.YELLOW);
AddCloud(if showclouds then HV3to4 else Double.naN, if showclouds then HV4to5 else double.nan, Color.PINK, Color.PINK);
AddCloud(if showclouds then HV4to5 else Double.nan, if showclouds then HVHIGH else double.nan, Color.DOWNTICK, Color.DOWNTICK);
# ---- Current Implied Volatility ----
plot ImpVol = imp_volatility(symbol = GetUnderlyingSymbol());
ImpVol.SetDefaultColor(Color.CYAN);
ImpVol.SetLineWeight(3);
AddLabel(1,"Current IV: " + ROUND(ImpVol,4) * 100 + " %",color.white);
AddLabel(1, "Percentile: " + if ImpVol < HV1to2 then 1
else if ImpVol between HV1to2 and HV2to3 then 2
else if ImpVol between HV2to3 and HV3to4 then 3
else if ImpVol between HV3to4 and HV4to5 then 4
else if ImpVol > HV4to5 then 5 else 0, if ImpVol < HV1to2 then Color.GREEN
else if ImpVol between HV1to2 and HV2to3 then Color.LIGHT_GREEN
else if ImpVol between HV2to3 and HV3to4 then Color.YELLOW
else if ImpVol between HV3to4 and HV4to5 then Color.PINK
else if ImpVol > HV4to5 then Color.PINK else Color.WHITE);
# ---- IV Rank ----
def IVRank = Round(((ImpVol - HVLOW) / (HVHIGH - HVLOW)), 4) * 100;
AddLabel(1, "IV Rank: " + IVRank + " %", Color.WHITE);
# ---- IV Percentile ----
#def num =
# ---- Bubbles ----
#def isLastBar = !IsNaN(close) and IsNaN(close[-1]);
#AddChartBubble(isLastbar, ImpVol, Round(ImpVol, 4) * 100 + " %", Color.WHITE,if HV2to3 > ImpVol then yes else no);
# ---- TroubleShoot ----
input Troubleshoot = no;
AddLabel(Troubleshoot, Round(HVLOW, 4) * 100 + " %", Color.YELLOW);
AddLabel(Troubleshoot, Round(HVHIGH, 4) * 100 + " %", Color.YELLOW);
AddLabel(Troubleshoot, Round(Difference, 4) * 100 + " %", Color.PINK);
AddLabel(Troubleshoot, HV1to2, Color.WHITE);
AddLabel(Troubleshoot, HV2to3, Color.WHITE);
AddLabel(Troubleshoot, HV3to4, Color.WHITE);
AddLabel(Troubleshoot, HV3to4, Color.WHITE);
```