declare lower;
#declare hide_on_intraday;
#IVPercentile
def vol = imp_volatility();
input DisplayIVPercentile = yes;
input TimePeriod = 252;
input LowVolLimit = 30;
input HighVolLimit = 50;
input Display6monthIVR = yes;
input MiddleTimePeriod = 126;
input DisplayShorterTerm = yes;
input ShortTimePeriod = 63;
input DisplayImpVolatility = yes;
input DisplayDaily1StandardDev = no;
input DisplayWeekly1StandardDev = yes;
input DisplayMonthly1StandardDev = yes;
input InvertRedGreen = yes;
input PercentileMALength = 21;
def data = if !IsNaN(vol) then vol else vol[-1];
def hi = Highest(data, TimePeriod);
def lo = Lowest(data, TimePeriod);
def MiddleHi = Highest(data, MiddleTimePeriod);
def MiddleLo = Lowest(data, MiddleTimePeriod);
def ShortHi = Highest(data, ShortTimePeriod);
def ShortLo = Lowest(data, ShortTimePeriod);
plot Percentile = (data β lo) / (hi β lo) * 100;
Percentile.SetDefaultColor(CreateColor(0, 102, 204));
Percentile.SetLineWeight(2);
plot PercentileMA = ExpAverage(Percentile, PercentileMALength);
PercentileMA.SetDefaultColor(Color.ORANGE);
PercentileMA.SetLineWeight(2);
def lowend = Percentile < LowVolLimit; def highend = Percentile > HighVolLimit;
def mPercentile = (data β MiddleLo) / (MiddleHi β MiddleLo) * 100;
def MiddleLowend = mPercentile < LowVolLimit;
def MiddleHighend = mPercentile < HighVolLimit;
def sPercentile = (data β ShortLo) / (ShortHi β ShortLo) * 100;
def ShortLowend = sPercentile < LowVolLimit; def ShortHighend = sPercentile > HighVolLimit;
DefineGlobalColor(βlowcolorβ, if InvertRedGreen then Color.RED else Color.GREEN);
DefineGlobalColor(βShortlowcolorβ, if InvertRedGreen then Color.GREEN else Color.GREEN);
DefineGlobalColor(βhighcolorβ, if InvertRedGreen then Color.GREEN else Color.RED);
DefineGlobalColor(βShorthighcolorβ, if InvertRedGreen then Color.GREEN else Color.RED);
#Labels
AddLabel(DisplayIVPercentile , Concat(βIV Rank = β, AsPrice(Round(Percentile, 0))), color = CreateColor(255-(Percentile*2.55),Percentile*2.55,0));
AddLabel(Display6monthIVR , Concat(β6mo IVR = β, AsPrice(Round(mPercentile , 0))), color = CreateColor(255-(mPercentile*2.55),mPercentile*2.55,0));
AddLabel(DisplayShorterTerm , Concat(β3mo IVR = β, AsPrice(Round(sPercentile , 0))), color = CreateColor(255-(sPercentile*2.55),sPercentile*2.55,0));
AddLabel(DisplayImpVolatility, Concat(βImpVol: β, AsPercent(vol)), if lowend then color.gray else if highend then color.gray else color.gray);
def ImpPts = (vol / Sqrt(252)) * close;
AddLabel(DisplayDaily1StandardDev , Concat(β1 Day SD +/- $β, AsText( ImpPts, NumberFormat.TWO_DECIMAL_PLACES)), if lowend then color.gray else if highend then color.gray else Color.gray);
def ImpPts2 = (vol / Sqrt(52)) * close;
AddLabel(DisplayWeekly1StandardDev, Concat(β1 Week SD +/- $β, AsText( ImpPts2, NumberFormat.TWO_DECIMAL_PLACES)), if lowend then color.gray else if highend then color.gray else Color.gray);
def ImpPts3 = (vol / Sqrt(12)) * close;
AddLabel(DisplayMonthly1StandardDev, Concat(β1 Month SD +/- $β, AsText( ImpPts3, NumberFormat.TWO_DECIMAL_PLACES)), if lowend then color.gray else if highend then color.gray else Color.gray);
plot LowVol = LowVolLimit;
plot HighVol = HighVolLimit;
LowVol.SetDefaultColor(GetColor(6));
HighVol.SetDefaultColor(GetColor(5));