# ########################################
# VIX (Market Fear Meter/Oscillator)
# Created by @rewardiaz
# v1.0 : 07/25/2024
# v2.0 : 08/07/2024
# ########################################
# Charting & Formatting
declare lower;
declare real_size;
# ########################################
# Chart Coloring
DefineGlobalColor("bull", CreateColor(0, 165, 0));
DefineGlobalColor("neutral", Color.DARK_ORANGE);
DefineGlobalColor("bear", CreateColor(225, 0, 0));
# ########################################
# Inputs
# Define the length for moving averages
input symbol = "VIX";
input VIXback = 14;
input smoothLength = 5;
# Historical Levels
input BullishLevel = 0.0;
input BearishLevel = 0.0;
input level1 = 16;
input level2 = 18;
input level3 = 20;
input level4 = 22;
input level5 = 25;
input level6 = 28;
input level7 = 30;
input level8 = 32;
# ########################################
# Globals
def na = Double.NaN;
def c = close("VIX");
def VIX = if !isNaN(c) then c else
if isNan(c) then c[1] else
na;
# ########################################
# Normalize the VIX data by calculating its Z-score
def meanVIX = if IsNaN(Average(VIX, VIXback))
then meanVIX[1]
else Average(VIX, VIXback);
def stdDevVIX = if IsNan(StDev(VIX, VIXback))
then stdDevVIX[1]
else StDev(VIX, VIXback);
def normalizedVIX = (VIX - meanVIX) / stdDevVIX;
# ########################################
# Smooth the normalized VIX data using an exponential moving average (EMA)
def smoothNormalizedVIX = ExpAverage(normalizedVIX, smoothLength);
plot HV = 3;
HV.SetStyle(Curve.SHORT_DASH);
plot LV = -3;
LV.SetStyle(Curve.SHORT_DASH);
# Plot the sentiment oscillator
plot SO = smoothNormalizedVIX;
SO.AssignValueColor(if SO > SO[1] then GlobalColor("bear") else GlobalColor("bull"));
SO.SetLineWeight(3);
SO.HideBubble();
SO.HideTitle();
# Determine the level that VIX has crossed
def crossedAbove = if VIX crosses above level1 && VIX > VIX[1] then level1 else
if VIX crosses above level2 && VIX > VIX[1] then level2 else
if VIX crosses above level3 && VIX > VIX[1] then level3 else
if VIX crosses above level4 && VIX > VIX[1] then level4 else
if VIX crosses above level5 && VIX > VIX[1] then level5 else
if VIX crosses above level6 && VIX > VIX[1] then level6 else
if VIX crosses above level7 && VIX > VIX[1] then level7 else
if VIX crosses above level8 && VIX > VIX[1] then level8 else
if VIX crosses above BullishLevel && VIX > VIX[1] then BullishLevel else
if VIX crosses above BearishLevel && VIX > VIX[1] then BearishLevel
else na;
def crossedBelow = if VIX crosses below level1 && VIX < VIX[1] then level1 else
if VIX crosses below level2 && VIX < VIX[1] then level2 else
if VIX crosses below level3 && VIX < VIX[1] then level3 else
if VIX crosses below level4 && VIX < VIX[1] then level4 else
if VIX crosses below level5 && VIX < VIX[1] then level5 else
if VIX crosses below level6 && VIX < VIX[1] then level6 else
if VIX crosses below level7 && VIX < VIX[1] then level7 else
if VIX crosses below level8 && VIX < VIX[1] then level8 else
if VIX crosses below BullishLevel && VIX < VIX[1] then BullishLevel else
if VIX crosses below BearishLevel && VIX < VIX[1] then BearishLevel
else na;
plot hLevel = if crossedAbove or crossedBelow then smoothNormalizedVIX else na;
hLevel.SetPaintingStrategy(PaintingStrategy.POINTS);
hLevel.SetDefaultColor(GlobalColor("neutral"));
hLevel.SetLineWeight(3);
hLevel.HideBubble();
hLevel.HideTitle();
# Add bubbles for level crossings
AddChartBubble(crossedAbove, smoothNormalizedVIX, crossedAbove, GlobalColor("bear"), yes);
AddChartBubble(crossedBelow, smoothNormalizedVIX, crossedBelow, GlobalColor("bull"), no);
# Add alerts for level crossings
Alert(crossedAbove, "$VIX crossing above " + crossedAbove, Alert.BAR, Sound.Bell);
Alert(crossedBelow, "$VIX crossing below " + crossedBelow, Alert.BAR, Sound.Bell);
AddLabel(yes,
if SO > SO[1] then " $VIX " + VIX + " Bearish and rising. " else
if SO < SO[1] then " $VIX " + VIX + " Bullish and falling. " else
" VIXCHOP ",
if SO > SO[1] then GlobalColor("bear") else
if SO < SO[1] then GlobalColor("bull") else
GlobalColor("neutral"));
AddLabel(crossedAbove, " $VIX crossing above " + crossedAbove + " level. ", GlobalColor("bear"));
AddLabel(crossedBelow, " $VIX crossing below " + crossedBelow + " level. ", GlobalColor("bear"));