declare upper; declare hide_on_intraday; input days_back = 252; # 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() == "/ZN") then close("VXTYN") / 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, 1), Color.BLACK); # 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, 1); def lowend = iv_rank < 50; def highend = iv_rank > 50; addlabel(yes,concat("IV Rank: ",round(iv_rank,2)), if lowend then color.red else if highend then color.green else color.cyan); # 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, 1); def lowend2 = iv_percentile < 50; def highend2= iv_percentile > 50; addlabel(yes,concat("IV Percentile: ",round(iv_percentile,2)+ " (azaz " + days_back + " napból " + counts_below + " volt jelenlegi IV alatt)"), if lowend2 then color.red else if highend2 then color.green else color.cyan);