Volatility Band Studies
Three studies are shown in the figure below.
For the upper study look for the price cross into the second band that will indicate the future direction of Volatility.
If the price pierces the second lower band, means Volatility is too high and is likely to decrease.
If the price pierces the second upper band, means Volatility is too low and is likely to increase.
The middle study shows the IV MACD. Best used for long term projection on weekly chart.
The lower study show the Implied Vol (red) and Historic Vol (grey) in comparison.
The ThinkScript code for each study is shown below the chart:
#############################################
#
# iv_band_study.ts
#
#############################################
declare hide_on_intraday;
### below are configurable input parameters
input price = close;
input timeFrame = {default DAY, WEEK, MONTH};
input project = 0;
input length = 20;
def time; # define the time frame
switch (timeFrame) {
case Day:
time = sqrt(252);
case Week:
time = sqrt(52);
case Month:
time = sqrt(12);
}
def vol = impvolatility();
def sigma1 = (vol/time)*price; # calculate the 1st sigma
def sigma2 = sigma1*2; # calculate the 2nd sigma
def upper = (price + sigma1);
def maxUpper = (price + sigma2);
def lower = (price - sigma1);
def maxLower = (price - sigma2);
plot upperBand = upper[1 + project]; # plot the inner upper band
plot lowerBand = lower[1 + project]; # plot the inner lower band
plot maxUpperBand = maxUpper[1 + project]; # plot the second outer upper band
plot maxLowerBand = maxLower[1 + project]; # plot the second outer lower band
addcloud(maxUpperBand, maxLowerBand, color.DARK_GRAY); # format the second band
addcloud(upperBand, lowerBand, color.GRAY); # format the inner band
upperBand.setdefaultcolor(color.GRAY); # format the lines
lowerBand.setdefaultcolor(color.GRAY); # format the lines
maxUpperBand.setdefaultcolor(color.DARK_GRAY); # format the lines
maxLowerBand.setdefaultcolor(color.DARK_GRAY); # format the lines
#############################################
#
# iv_MACD.ts
#
#
#############################################
declare lower; # place the graph on a lower chart
### below are configurable input parameters
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input AverageType = {SMA, default EMA};
def price = impVolatility();
plot Value;
plot Avg;
switch (AverageType) {
case SMA:
Value = Average(price, fastLength) - Average(price, slowLength);
Avg = Average(Value, MACDLength);
case EMA:
Value = ExpAverage(price, fastLength) - ExpAverage(price, slowLength);
Avg = ExpAverage(Value, MACDLength);
}
plot Diff = Value - Avg;
plot ZeroLine = 0;
Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0
then if Diff > Diff[1]
then Diff.color("Positive and Up")
else Diff.color("Positive and Down")
else if Diff < Diff[1]
then Diff.color("Negative and Down")
else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
#############################################
#
# iv_hv_study.ts
# thinkorswim, inc. (c) 2007
#
#
#############################################
declare lower;
input longLength = 262;
input shortLength = 20;
def clLog = Log(close / close[1]);
plot HV = stdev(clLog, shortLength) * Sqrt(shortLength * longLength / (shortLength - 1));
HV.SetDefaultColor(GetColor(3));
declare hide_on_intraday;
plot ImpVol = IMP_VOLATILITY();
ImpVol.SetDefaultColor(GetColor(5));