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));