VIX Cheat Sheet
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © KioseffTrading

//@version=5
indicator("VIX Cheat Sheet [Kioseff Trading]", overlay = true, max_labels_count = 500, max_lines_count = 500, max_boxes_count = 500)

// _______________________________________________________
//
// Input Box                                 
// _______________________________________________________

select = input.string("VIX", title = "VIX (Generally Stocks) or VixFix (Works Anywhere) ?", options = ["VIX", "VixFix"])
vixTV = input.string("No", title = "Turn Off Tradingview TV?", options = ["No", "Yes", "Unplug"], group = "Utility Inputs")
tvCol = input.string("On", title = 'VIX Box Color Fills ("Color Commentary")', options = ["On", "Off"])
labelSize = input.string("Tiny", title = "Label Size", options = ["Tiny", "Small", "Auto"])
labelCount = input.int(defval = 500, title = "Label Count", maxval = 500, minval = 0)
lossBox = input.string("On", title = "Show Average Loss Boxes", options = ["On", "Off"])
hide = input.string(defval = "No", title = 'Send "Color Commentary" to a Table? \n(Select "Yes" When VIX is Fluctuating Wildly)', options = ["No", "Yes"], group = "Nonutility inputs")
BaW = input.string(defval = "Color", title = "Color TV? Black and White?", options = ["Color", "Black and White"])
// _______________________________________________________
//
// Data Request
// _______________________________________________________

vix() => [close, open, high, low]
[vClose, vOpen, vHigh, vLow] = request.security("VIX", "D", vix())
vFix = (ta.highest(close, 22) - low) / (ta.highest(close, 22)) * 100
spx = request.security("PCSPX", "D", close)
ratioSMA = ta.sma(spx, 50)
VorVFclose = select == "VIX" ? vClose : vFix

// _______________________________________________________
//
// Standard Deviation and Correlation                                
// _______________________________________________________

[mid, upper, lower] = ta.bb(VorVFclose, 20, 2.0)
corr = ta.correlation(close, vClose, 200)

// _______________________________________________________
//
// One-Time Initialization Variables                              
// _______________________________________________________

var float assetClose = 0.0
var float tenSessionCalcCumulative = 0.0
var float fiveSessionCalcCumulative = 0.0
var float sessionCalc = 0.0
var float fiveSessionLowest = 0.0
var float sessionCalc1 = 0.0
var float fiveSessionCalc = 0.0
var float tenSessionCalc = 0.0
var float tenSessionLowest = 0.0

var int fiveSessionCounter = 0
var int tenSessionCounter = 0
var int counter = 0
var int vixCounter = 0
var int sessionCounter = 0
var int indexCounter = 0

var label labelX = na
var label labelX1 = na
var label labelX3 = na
var label labelX4 = na

var line lineF = na
var line lineF1 = na
var line lineF2 = na
var line lineF3 = na
var linefill lineF4 = na
var line lineF5 = na
var line lineF6 = na 
var line lineF7 = na

var box lossBox5 = na
var box lossBox10 = na

var label vixLabel = na
var box vixBox = na
var line vixLine3 = na
var linefill vixLineFill = na
var float fixedHigh = 0.0
var int fixedHighCounter = 0

var line red = na
var line green = na
var line yellow = na
var line blue = na
var line purple = na
var line orange = na

var linefill redx = na
var linefill greenx = na
var linefill yellowx = na
var linefill bluex = na
var linefill purplex = na
var linefill orangex = na

var label TV = na

var label x = na 
var label x1 = na 
var label x2 = na 
var label x3 = na 
var label x4 = na 
var label x5 = na 
var label x6 = na 
var label x7 = na 
var label x8 = na 
var label x9 = na 
var label x10 = na 
var label x11 = na 
var label x12 = na 
var label x13 = na 
var label x14 = na 

var color r2 = na
var color g2 = na
var color ye2 = na
var color b2 = na
var color p2 = na
var color o2 = na

var float y = 0.0
var float y1 = 0.0
var float y2 = 0.0
var float y3 = 0.0
var int y4 = -1
var int y5 = 0
var int y6 = 0
var line j = na
var line j1 = na
var line j2 = na
var line j3 = na
var linefill j4 = na

var label n = na 
var label n1 = na
var label n2 = na
var label n4 = na
var line nx = na
var line nx1 = na
var line nx2 = na
var line nx3 = na
var label saveLabel = na


var line [] vA = array.new_line()
var line [] lowerA = array.new_line()
var line [] upperA = array.new_line()
var linefill [] aFill = array.new_linefill()
var float[] trackLow = array.new_float()
var float[] trackHigh = array.new_float()


var color[] colorChange = array.new_color()
var color[] colorChange2 = array.new_color()

// _______________________________________________________
//
// Initial Performance Calculations                           
// _______________________________________________________

vixLow = ta.lowest(close, bar_index + 1 - indexCounter)

if bar_index - indexCounter > 5000
    indexCounter := bar_index

if VorVFclose > upper and counter == 0
    assetClose := close
    counter := 1
    indexCounter := bar_index

if counter == 1 
    vixCounter += 1


if counter == 1 and vixCounter == 11 
    counter := 0
    tenSessionCalc := ((close / assetClose) - 1) * 100
    tenSessionCalcCumulative :=((close / assetClose) - 1) * 100
    tenSessionCalcCumulative := tenSessionCalcCumulative[1] + tenSessionCalcCumulative
    tenSessionCounter += 1
    vixCounter := 0
    tenSessionLowest := ((vixLow / assetClose) - 1) * 100
    tenSessionLowest := tenSessionLowest[1] + tenSessionLowest

if counter == 1 and vixCounter == 6
    fiveSessionCounter += 1
    fiveSessionCalc := ((close / assetClose) - 1) * 100
    fiveSessionCalcCumulative := ((close / assetClose) - 1) * 100
    fiveSessionCalcCumulative := fiveSessionCalcCumulative[1] + fiveSessionCalcCumulative
    fiveSessionLowest := ((vixLow / assetClose) - 1) * 100
    fiveSessionLowest := fiveSessionLowest[1] + fiveSessionLowest
    
    
if counter == 1 and vixCounter == 2
    sessionCalc := ((close / assetClose) - 1) * 100
    sessionCalc1 := ((close / assetClose) - 1) * 100
    sessionCalc1 := sessionCalc1[1] + sessionCalc1
    sessionCounter += 1
    

// _______________________________________________________
//
// Plot Calculations                               
// _______________________________________________________    
    

vgl = ((vClose / vClose[1] - 1) * 100)
vfgl = ((vFix / vFix[1] - 1) * 100)

vixGainLossString = vgl > 0.0 ? " (Gain)" : vgl < 0.0 ? " (Loss)" : " (No Gain/Loss)"
vixFixGainLossString = vfgl > 0.0 ? " (Gain)" : vfgl < 0.0 ? " (Loss)" : " (No Gain/Loss)"
vixVixFixGainLossString = select == "VIX " ? "VIX " : "VixFix "
pcString = spx == 1.00 ? "(Equal Puts/Calls)" : spx < 1.00 ? "(More Calls Than Puts)" : "(More Puts Than Calls)"

// Used for Box Plot Smoothing
highestHigh = ta.highest(high, 500)
lowestLow = ta.lowest(low, 500)
highestHigh1 = ta.highest(high, 200)
lowestLow1 = ta.lowest(low, 200)
highestSmooth = ta.sma(math.avg(highestHigh, highestHigh1, lowestLow1, ohlc4), 100)
mult = input.float(2.5, title = "TV Size (Decrease for Low Price Assets) (Default is 2.5x)", step = 0.1)


// Exaggerating VIX/VixFix Price Flucuations
vixPlotCalc = select == "VIX" ? VorVFclose * mult * ((math.avg(ta.highest(high, 252), ta.lowest(low, 252)) * .003) + math.abs(close / close[1])) : VorVFclose * (mult) * ((math.avg(ta.highest(high, 252), ta.lowest(low, 252))* .003) + math.abs(close / close[1]))
upperPlotCalc =  select == "VIX" ? upper * mult * ((math.avg(ta.highest(high, 252), ta.lowest(low, 252)) * .003)+ math.abs(close / close[1])) :  upper * (mult) * ((math.avg(ta.highest(high, 252), ta.lowest(low, 252)) * .003)+ math.abs(close / close[1]))
lowerPlotCalc =  select == "VIX" ? lower * mult * ((math.avg(ta.highest(high, 252), ta.lowest(low, 252)) * .003) + math.abs(close / close[1])):  lower * (mult) * ((math.avg(ta.highest(high, 252), ta.lowest(low, 252)) * .003)+ math.abs(close / close[1]))


// _______________________________________________________
//
// Final Performance Calculations                                 
// _______________________________________________________

finVix10 = tenSessionCalcCumulative / tenSessionCounter
finVix5 = fiveSessionCalcCumulative / fiveSessionCounter
finVix = sessionCalc1 / sessionCounter
vixLowest = fiveSessionLowest / fiveSessionCounter
vixLowestTen = tenSessionLowest / tenSessionCounter
runningVIX = ((close / assetClose) - 1) * 100
lPlot = ta.lowest(low, 5)[5]


// _______________________________________________________
//
// Lines & Lables                              
// _______________________________________________________



if bar_index > bar_index[1] and vixCounter == 0 
    label.delete(labelX1[1])
    line.delete(lineF1[1])
    lineF1 := line.new(bar_index, close, bar_index + 5, close * .96, color = color.white, style = line.style_dashed)
    labelX1 := label.new(bar_index + 5, close* .96, text = select == "VIX" ? "VIX/" + str.tostring(syminfo.ticker) + " Correlation Coeffecient: " + str.tostring(corr, format.mintick) + "\n_______________________________________\n" + 
         "Average 5-Session Greatest % Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
                 "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
                     "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                         "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix10, format.percent)
         : "Average 5-Session Greatest % Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
                                 "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
                                     "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                                         "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix10, format.percent)
                                          , color = select == "VIX" ? color.new(color.yellow, 50) : color.new(color.orange, 50), textcolor = color.white, style = label.style_label_left, size = labelSize == "Tiny" ? size.tiny : labelSize == "Small" ? size.small : size.auto)
            

if bar_index > bar_index[1] and vixCounter == 1
    label.delete(labelX1[1])
    line.delete(lineF1[1])
    lineF1 := line.new(bar_index, close, bar_index + 5,  close * .96, color = color.white, style = line.style_dashed)
    labelX1 := label.new(bar_index + 5, close * .96, text = select == "VIX" ?  "VIX Current Close Price: $" + str.tostring(VorVFclose, format.mintick) 
         + "\nVIX Upper Band Price: $" + str.tostring(upper, format.mintick) + "\n\nFollowing a VIX Close Above Upper Band, " + "\n\nNext Session %Gain/Loss: " 
             + str.tostring(runningVIX, format.percent) + "\n5-Session %Gain/Loss: " + str.tostring(runningVIX, format.percent) + "\n10-Session %Gain/Loss: " 
                 + str.tostring(runningVIX, format.percent) : "VixFix Current Close Price: $" + str.tostring(VorVFclose, format.mintick) + "\nVixFix Upper Band Price: $" 
                     + str.tostring(upper, format.mintick) + "\n\nFollowing a VixFix Close Above Upper Band, " + "\n\nNext Session %Gain/Loss: " 
                         + str.tostring(runningVIX, format.percent)  + "\n5-Session %Gain/Loss: " + str.tostring(runningVIX, format.percent) + "\n10-Session %Gain/Loss: " 
                             + str.tostring(runningVIX, format.percent), color = select == "VIX" ? color.new(color.red, 50) : color.new(color.red, 50), textcolor = color.white, style = label.style_label_left, 
                                 tooltip = select == "VIX" ? "Average 5-Session Greatest % Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
                                     "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
                                         "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                                             "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix10, format.percent) :  
                                                 "Average 5-Session Greatest % Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
                                                     "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
                                                         "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                                                             "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix10, format.percent)) 
    
    if lossBox == "On"
        
        lossBox5 := box.new(indexCounter, assetClose * (1 + (vixLowest/100)), bar_index, (assetClose * (1 + (vixLowest/100) ) * 1.005), bgcolor = color.new(color.orange, 75), border_color = color.new(color.orange, 75),text = "Average Greatest 5-Session % Loss", text_color = color.white)
        lossBox10 := box.new(indexCounter, assetClose * (1 + (vixLowestTen/100)), bar_index, (assetClose * (1 + (vixLowestTen/100) ) * 1.005), bgcolor = color.new(color.white, 25), border_color = color.new(color.black, 0), text = "Average Greatest 10-Session % Loss", text_color = color.black)
    labelX := label.new(bar_index, high * 1.05, text = select == "VIX" ? "VIX Closed Above Upper Band" : "VixFix Closed Above Upper Band", color = select == "VIX" ? color.new(color.blue, 50) : color.new(color.purple, 50), textcolor = color.white, 
     tooltip = select == "VIX" ? "Average 5-Session Greatest % Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
         "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
             "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                 "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix10, format.percent) :  
                     "Average 5-Session Greatest % Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
                         "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
                             "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                                 "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix10, format.percent))
    lineF := line.new(bar_index, high * 1.05, bar_index, high, style = line.style_dashed, color = color.white)
if bar_index > bar_index[1] and vixCounter > 1 and vixCounter <= 5
    label.delete(labelX1[1])
    line.delete(lineF1[1])
    label.delete(labelX[1])
    line.delete(lineF[1])
    lineF1 := line.new(bar_index, close, bar_index+ 5,  close * .96, color = color.white, style = line.style_dashed)
    labelX1 := label.new(bar_index + 5,  close * .96, text = select == "VIX" ?  "VIX Current Close Price: $" + str.tostring(VorVFclose, format.mintick) + "\nVIX Upper Band Price: $" + str.tostring(upper, format.mintick)
             + "\n\nFollowing a VIX Close Above Upper Band, " + "\n\nNext Session %Gain/Loss: " + str.tostring(sessionCalc, format.percent)
                 + "\n5-Session %Gain/Loss: " + str.tostring(runningVIX, format.percent) + "\n10-Session %Gain/Loss: " + str.tostring(runningVIX, format.percent)
                     : "VixFix Current Close Price: $" + str.tostring(VorVFclose, format.mintick) + "\nVixFix Upper Band Price: $" + str.tostring(upper, format.mintick) 
                         + "\n\nFollowing a VixFix Close Above Upper Band, " + "\n\nNext Session %Gain/Loss: " + str.tostring(sessionCalc, format.percent)
                             + "\n5-Session %Gain/Loss: " + str.tostring(runningVIX, format.percent) + "\n10-Session %Gain/Loss: " + str.tostring(runningVIX, format.percent)
                                 , color = select == "VIX" ? color.new(color.red, 50) : color.new(color.red, 50), textcolor = color.white, style = label.style_label_left, tooltip = select == "VIX" ? "Average 5-Session Greatest % Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
                                     "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
                                         "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                                             "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix10, format.percent) :  
                                                 "Average 5-Session Greatest % Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
                                                     "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
                                                         "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                                                             "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix10, format.percent))
    
    if lossBox == "On"
        
        box.delete(lossBox5[1])
        lossBox5 := box.new(indexCounter, assetClose * (1 + (vixLowest/100)), bar_index, (assetClose * (1 + (vixLowest/100) ) * 1.005), bgcolor = color.new(color.orange, 75), border_color = color.new(color.orange, 75),text = "Average Greatest 5-Session % Loss", text_color = color.white)
        box.delete(lossBox10[1])
        lossBox10 := box.new(indexCounter, assetClose * (1 + (vixLowestTen/100)), bar_index, (assetClose * (1 + (vixLowestTen/100) ) * 1.005), bgcolor = color.new(color.white, 25), border_color = color.new(color.black, 0), text = "Average Greatest 10-Session % Loss", text_color = color.black)

    labelX := label.new(bar_index, high * 1.05, text = select == "VIX" ? "VIX Closed Above Upper Band" : "VixFix Closed Above Upper Band", color = select == "VIX" ? color.new(color.blue, 50) : color.new(color.purple, 50), textcolor = color.white, 
     tooltip = select == "VIX" ? "Average 5-Session Greatest % Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
         "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
             "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                 "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix10, format.percent) :  
                     "Average 5-Session Greatest % Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
                         "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
                             "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                                 "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix10, format.percent))
    lineF := line.new(bar_index, high * 1.05, bar_index, high, style = line.style_dashed, color = color.white)

if bar_index > bar_index[1] and vixCounter > 5 and vixCounter < 10
    label.delete(labelX1[1])
    line.delete(lineF1[1])
    label.delete(labelX[1])
    line.delete(lineF[1])
    lineF1 := line.new(bar_index, close, bar_index + 5,  close * .96, color = color.white, style = line.style_dashed)
    labelX1 := label.new(bar_index + 5,  close * .96, text = select == "VIX" ?  "VIX Current Close Price: $" + str.tostring(VorVFclose, format.mintick) + "\nVIX Upper Band Price: $" + str.tostring(upper, format.mintick)
         + "\n\nFollowing a VIX Close Above Upper Band, " + "\n\nNext Session %Gain/Loss: " + str.tostring(sessionCalc, format.percent)
             + "\n5-Session %Gain/Loss: " + str.tostring(fiveSessionCalc, format.percent) + "\n10-Session %Gain/Loss: " + str.tostring(runningVIX, format.percent)
                 : "VixFix Current Close Price: $" + str.tostring(VorVFclose, format.mintick) + "\nVixFix Upper Band Price: $" + str.tostring(upper, format.mintick) 
                     + "\n\nFollowing a VixFix Close Above Upper Band, " + "\n\nNext Session %Gain/Loss: " + str.tostring(sessionCalc, format.percent)
                         + "\n5-Session %Gain/Loss: " + str.tostring(fiveSessionCalc, format.percent) + "\n10-Session %Gain/Loss: " + str.tostring(runningVIX, format.percent)
                             ,color = select == "VIX" ? color.new(color.red, 50) : color.new(color.red, 50), textcolor = color.white, style = label.style_label_left, tooltip = select == "VIX" ? "Average 5-Session Greatest % Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
                                 "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
                                     "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                                         "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix10, format.percent) :  
                                             "Average 5-Session Greatest % Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
                                                 "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
                                                     "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                                                         "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix10, format.percent)) 

    
    if lossBox == "On"
        
        box.delete(lossBox5[1])
        lossBox5 := box.new(indexCounter, assetClose * (1 + (vixLowest/100)), bar_index, (assetClose * (1 + (vixLowest/100) ) * 1.005), bgcolor = color.new(color.orange, 75), border_color = color.new(color.orange, 75),text = "Average Greatest 5-Session % Loss", text_color = color.white)
        box.delete(lossBox10[1])
        lossBox10 := box.new(indexCounter, assetClose * (1 + (vixLowestTen/100)), bar_index, (assetClose * (1 + (vixLowestTen/100) ) * 1.005), bgcolor = color.new(color.white, 25), border_color = color.new(color.black, 0), text = "Average Greatest 10-Session % Loss", text_color = color.black)

    labelX := label.new(bar_index, high * 1.05, text = select == "VIX" ? "VIX Closed Above Upper Band" : "VixFix Closed Above Upper Band", color = select == "VIX" ? color.new(color.blue, 50) : color.new(color.purple, 50), textcolor = color.white, 
     tooltip = select == "VIX" ? "Average 5-Session Greatest % Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
         "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
             "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                 "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix10, format.percent) :  
                     "Average 5-Session Greatest % Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
                         "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
                             "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                                 "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix10, format.percent))
    lineF := line.new(bar_index, high * 1.05, bar_index, high, style = line.style_dashed, color = color.white)

if bar_index > bar_index[1] and vixCounter >= 11
    label.delete(labelX1[1])
    line.delete(lineF1[1])
    lineF1 := line.new(bar_index , close, bar_index + 5,  close * .96, color = color.white, style = line.style_dashed)
    labelX1 := label.new(bar_index + 5,  close * .96, text = select == "VIX" ?  "VIX Current Close Price: $" + str.tostring(VorVFclose, format.mintick) + "\nVIX Upper Band Price: $" + str.tostring(upper, format.mintick)
         + "\n\nFollowing a VIX Close Above Upper Band, " + "\n\nNext Session %Gain/Loss: " + str.tostring(sessionCalc, format.percent)
             + "\n5-Session %Gain/Loss: " + str.tostring(fiveSessionCalc, format.percent) + "\n10-Session %Gain/Loss: " + str.tostring(tenSessionCalc, format.percent)
                 : "VixFix Current Close Price: $" + str.tostring(VorVFclose, format.mintick) + "\nVixFix Upper Band Price: $" + str.tostring(upper, format.mintick) 
                     + "\n\nFollowing a VixFix Close Above Upper Band, " + "\n\nNext Session %Gain/Loss: " + str.tostring(sessionCalc, format.percent)
                         + "\n5-Session %Gain/Loss: " + str.tostring(fiveSessionCalc, format.percent) + "\n10-Session %Gain/Loss: " + str.tostring(tenSessionCalc, format.percent)
                             , color = select == "VIX" ? color.new(color.red, 50) : color.new(color.red, 50), textcolor = color.white, style = label.style_label_left, tooltip = select == "VIX" ? "Average 5-Session Greatest % Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
                                 "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
                                     "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                                         "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix10, format.percent) :  
                                             "Average 5-Session Greatest % Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
                                                 "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
                                                     "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                                                         "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix10, format.percent)) 
    
    label.delete(labelX[1])
    line.delete(lineF[1])
    lineF := line.new(bar_index - 9, high * 1.05, bar_index - 9, high, style = line.style_dashed, color = color.white)
    labelX := label.new(bar_index - 9, high * 1.05, text = select == "VIX" ? "VIX Closed Above Upper Band" : "VixFix Closed Above Upper Band", color = color.new(color.blue, 75), textcolor = color.white, 
         tooltip = str.tostring(year(time)) + "/" + str.tostring(month(time)) + "/" + str.tostring(dayofmonth(time)) + "\nNext Session %Gain/Loss: " + str.tostring(sessionCalc, format.percent) + "\n5-Session %Gain/Loss: " + 
             str.tostring(fiveSessionCalc, format.percent) + "\10-Session %Gain/Loss: " + str.tostring(tenSessionCalc, format.percent))
   
    if lossBox == "On"
       
        box.delete(lossBox5[1])
        lossBox5 := box.new(indexCounter, assetClose * (1 + (vixLowest/100)), bar_index, (assetClose * (1 + (vixLowest/100) ) * 1.005), bgcolor = color.new(color.orange, 75), border_color = color.new(color.orange, 75),text = "Average Greatest 5-Session % Loss", text_color = color.white)
        box.delete(lossBox10[1])
        lossBox10 := box.new(indexCounter, assetClose * (1 + (vixLowestTen/100)), bar_index, (assetClose * (1 + (vixLowestTen/100) ) * 1.005), bgcolor = color.new(color.white, 25), border_color = color.new(color.black, 0), text = "Average Greatest 10-Session % Loss", text_color = color.black)

if bar_index > bar_index[1] and vixCounter > 0 and vixCounter < 10 
    
    line.delete(lineF2[1])
    line.delete(lineF3[1])
    lineF2 := line.new(indexCounter, assetClose * 1.10, bar_index, assetClose * 1.10, color = na)
    lineF3 := line.new(indexCounter, assetClose * .90, bar_index, assetClose * .90, color = na)
    lineF4 := linefill.new(lineF2, lineF3, color = color.new(color.red, 90))

if vixCounter == 0
    
    line.delete(lineF3)
    line.delete(lineF2)
    linefill.delete(lineF4)
    

if VorVFclose > upper and vixCounter == 0
    labelX := label.new(bar_index, high * 1.05, text = select == "VIX" ? "VIX Closed Above Upper Band" : "VixFix Closed Above Upper Band", color = select == "VIX" ? color.new(color.blue, 50) : color.new(color.purple, 50), textcolor = color.white, 
     tooltip = select == "VIX" ? "Average 5-Session Greatest % Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
         "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VIX Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
             "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                 "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VIX Close Above the Upper Band: " + str.tostring(finVix10, format.percent) :  
                     "Average 5-Session Greatest % Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(vixLowest, format.percent) + 
                         "\n_______________________________________\nAverage Next Session Close-to-Close Gain/Loss \nFollowing a VixFix Close Above the Upper Band: " + str.tostring(finVix, format.percent) + 
                             "\n_______________________________________\nAverage 5-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix5, format.percent) +
                                 "\n_______________________________________\nAverage 10-Session Gain/Loss Following\na VixFix Close Above the Upper Band: " + str.tostring(finVix10, format.percent))
    lineF := line.new(bar_index, high * 1.05, bar_index, high, style = line.style_dashed, color = color.white)
    
if vixCounter == 10
    label.delete(labelX)
    labelX := label.new(indexCounter, high[bar_index - indexCounter] * 1.05, text = select == "VIX" ? "VIX Closed Above Upper Band\n" 
         + str.tostring(year(time[bar_index - indexCounter])) + "/" + str.tostring(month(time[bar_index - indexCounter])) + "/" + str.tostring(dayofmonth(time[bar_index - indexCounter])) + "\n_______________________________________\n" + 
             "Next Session %Gain/Loss: " + str.tostring(sessionCalc, format.percent) + "\n5-Session %Gain/Loss: " + str.tostring(fiveSessionCalc, format.percent) 
                  + "\n10-Session %Gain/Loss: " + str.tostring(runningVIX, format.percent) 
                     : "VixFix Closed Above Upper Band\n"  + str.tostring(year(time[bar_index - indexCounter])) + "/" + str.tostring(month(time[bar_index - indexCounter])) + "/" + str.tostring(dayofmonth(time[bar_index - indexCounter])) + "\n_______________________________________\n" + 
                         "Next Session %Gain/Loss: " + str.tostring(sessionCalc, format.percent) + "\n5-Session %Gain/Loss: " + str.tostring(fiveSessionCalc, format.percent) 
                             + "\n10-Session %Gain/Loss: " + str.tostring(runningVIX, format.percent), color = select == "VIX" ? color.new(color.blue, 50) : color.new(color.purple, 50), textcolor = color.white, size = labelSize == "Tiny" ? size.tiny : labelSize == "Small" ? size.small : size.auto)
    line.delete(lineF)
    lineF := line.new(indexCounter, high[bar_index - indexCounter] * 1.05, indexCounter, high[bar_index - indexCounter], style = line.style_dashed, color = color.white)


// _______________________________________________________
//
// VIX Box Plot and Color Cycle                      
// _______________________________________________________    
    
// Color Array Cycle 
if barstate.isfirst
    
    array.push(colorChange, BaW == "Color" ? color.new(color.red, 87) : color.new(color.white, 90))
    array.push(colorChange, BaW == "Color" ?color.new(color.green, 87): color.new(color.gray, 90))
    array.push(colorChange,BaW == "Color" ? color.new(color.yellow, 87): color.new(color.black, 90))
    array.push(colorChange,BaW == "Color" ?color.new(color.blue, 87): color.new(color.white, 90))
    array.push(colorChange,BaW == "Color" ?color.new(color.purple, 87): color.new(color.gray, 90))
    array.push(colorChange, BaW == "Color" ?color.new(color.orange, 87):  color.new(color.black, 90))
    array.push(colorChange2,BaW == "Color" ?color.new(color.red, 25): color.new(color.white, 90))
    array.push(colorChange2,BaW == "Color" ?color.new(color.green, 25): color.new(color.gray, 90))
    array.push(colorChange2, BaW == "Color" ?color.new(color.yellow, 25): color.new(color.black, 90))
    array.push(colorChange2,BaW == "Color" ?color.new(color.blue, 25): color.new(color.white, 90))
    array.push(colorChange2,BaW == "Color" ?color.new(color.purple, 25): color.new(color.gray, 90))
    array.push(colorChange2, BaW == "Color" ?color.new(color.orange, 25):  color.new(color.black, 90))

// Cycles Colors
aGet = array.get(colorChange, bar_index % array.size(colorChange))
aGet2 = array.get(colorChange2, bar_index % array.size(colorChange2))


// Plots Color Commentary
if bar_index > bar_index[1] 
    line.delete(red[1])
    line.delete(green[1])
    line.delete(yellow[1])
    line.delete(blue[1])
    line.delete(purple[1])
    line.delete(orange[1])
    line.delete(lineF5[1])
    line.delete(lineF6[1])
    line.delete(lineF7[1])
    label.delete(TV[1])
    
    linefill.delete(redx[1])
    linefill.delete(greenx[1])
    linefill.delete(yellowx[1])
    linefill.delete(bluex[1])
    linefill.delete(purplex[1])
    linefill.delete(orangex[1])
    
    red := line.new(bar_index + 100, box.get_bottom(vixBox), bar_index + 100, label.get_y(labelX3) * .85, color = na)
    green := line.new(bar_index + 112, box.get_bottom(vixBox), bar_index + 112, label.get_y(labelX3) * .85, color = na)
    yellow := line.new(bar_index + 125, box.get_bottom(vixBox), bar_index + 125, label.get_y(labelX3) * .85, color = na)
    blue := line.new(bar_index + 137, box.get_bottom(vixBox), bar_index + 137, label.get_y(labelX3) * .85, color = na)
    purple := line.new(bar_index + 150, box.get_bottom(vixBox), bar_index + 150, label.get_y(labelX3) * .85, color = na)
    orange := line.new(bar_index + 162, box.get_bottom(vixBox), bar_index + 162, label.get_y(labelX3) * .85, color = na)
    redx := linefill.new(red, green, color = tvCol == "On" ? aGet : na)
    greenx := linefill.new(green, yellow, color = tvCol == "On" ? aGet[1] : na)
    yellowx := linefill.new(yellow, blue, color = tvCol == "On" ? aGet[2] : na)
    bluex := linefill.new(blue, purple, color = tvCol == "On" ? aGet[3] : na)
    purplex := linefill.new(purple, orange, color = tvCol == "On" ? aGet[4] : na)
    lineF6 := line.new(bar_index + 100, box.get_bottom(vixBox), bar_index + 100, label.get_y(labelX3) * .85, color = color.white, style = line.style_dashed)
    lineF5 := line.new(bar_index + 175, box.get_bottom(vixBox), bar_index + 175, label.get_y(labelX3) * .85, color = color.white, style = line.style_dashed)
    lineF7 := line.new(bar_index + 100, line.get_y2(lineF6), bar_index + 175, line.get_y2(lineF5), color = color.white, style = line.style_dashed)
    orangex := linefill.new(orange, lineF5, color = tvCol == "On" ? aGet[5] : na)
 
// Plots VIX/VixFix TV
if bar_index > bar_index[1] 
    if vixTV == "No" or vixTV == "Yes"
       
        array.push(vA, line.new(bar_index + 150, highestSmooth + vixPlotCalc[1] + (highestSmooth[1] - highestSmooth), bar_index + 151, highestSmooth + vixPlotCalc , color = VorVFclose >= upper and select == "VIX" and BaW == "Color" ? color.red : VorVFclose 
             >= upper and select == "VixFix" and BaW == "Color" ? color.blue : select == "VIX" and VorVFclose < upper and BaW == "Color" ? color.green : select == "VixFix" and VorVFclose < upper and BaW == "Color" ? color.green : BaW == "Black and White" ? color.white : color.green))
        array.push(lowerA, line.new(bar_index + 150, highestSmooth + lowerPlotCalc[1] + (highestSmooth[1] - highestSmooth), bar_index + 151, highestSmooth + lowerPlotCalc , color = select == "VIX" and BaW == "Color" ? color.blue : select == "VixFix" and BaW == "Color" ? color.red : BaW == "Black and White" ? color.gray : color.blue))
        array.push(upperA, line.new(bar_index + 150, highestSmooth + upperPlotCalc[1] + (highestSmooth[1] - highestSmooth), bar_index + 151, highestSmooth + upperPlotCalc , color = select == "VIX" and BaW == "Color" ? color.blue : select == "VixFix" and BaW == "Color" ? color.red :  BaW == "Black and White" ? color.gray : color.blue))
        array.push(aFill, linefill.new(array.get(lowerA, array.size(lowerA) - 1), array.get(upperA, array.size(upperA) - 1), select == "VIX" and BaW == "Color"? color.new(color.blue,75) : select == "VixFix" and BaW == "Color" ? color.new(color.red, 75) : BaW == "Black and White" ? color.new(color.gray, 75) : color.new(color.blue, 75)))
        array.push(trackLow, math.min(highestSmooth + vixPlotCalc[1] + (highestSmooth[1] - highestSmooth), highestSmooth + lowerPlotCalc[1] + (highestSmooth[1] - highestSmooth)))
        array.push(trackHigh, math.max(highestSmooth + vixPlotCalc[1] + (highestSmooth[1] - highestSmooth), highestSmooth + upperPlotCalc[1] + (highestSmooth[1] - highestSmooth)))

    
    if array.size(vA) > 50
        line.delete(array.shift(vA))
        line.delete(array.shift(lowerA))
        line.delete(array.shift(upperA))
        linefill.delete(array.shift(aFill))
        array.shift(trackLow)
        array.shift(trackHigh)

// Advisory when VIX/VixFix is Pushing Statistics off Chart  
if bar_index > bar_index[1] and vClose > 50 and hide == "No"
    label.delete(saveLabel[1])
    saveLabel := label.new(bar_index + 115, array.max(trackHigh) * .98, color = na, text = "Try Putting the Color Commentary \nin a Table (User Input Tab Option)", textcolor = color.red, size = labelSize == "Tiny" ? size.tiny : labelSize == "Small" ? size.small : size.auto)

if vClose < 50
    label.delete(saveLabel[1])

// VIX Box Plot
if bar_index > bar_index[1] and vixTV == "No"
    vixBox := box.new(bar_index + 100, array.max(trackHigh) * 1.01, bar_index + 175, array.min(trackLow) * .99, border_width = 1, border_style = line.style_solid, border_color = color.white)

// Plot VIX Statistics
if bar_index > bar_index[1] and vixTV == "No"
    label.delete(vixLabel[1])
    box.delete(vixBox[1])
    
    vixLabel := label.new(bar_index + 151, box.get_top(vixBox) * 1.01, color = select == "VIX" ? color.new(color.purple, 50)
         : color.new(#000000, 50), 
             text = select == "VIX" ? "VIX Current Close Price: $" + str.tostring(VorVFclose, format.mintick)
                 + "\nVIX Upper Band Price: $" + str.tostring(upper, format.mintick) : "VixFix Current Close Price: $" + str.tostring(VorVFclose, format.mintick)
                     + "\nVixFix Upper Band Price: $" + str.tostring(upper, format.mintick)
                         , textcolor = color.white)
    
    if vixTV == "No" and hide == "No"    
        
        line.delete(vixLine3[1])
        label.delete(labelX3[1])
        label.delete(x[1])
        label.delete(x1[1])
        label.delete(x2[1])
        label.delete(x3[1])
        label.delete(x4[1])
        label.delete(x5[1])
        label.delete(x6[1])
        label.delete(x7[1])
        label.delete(x8[1])
        label.delete(x9[1])
        label.delete(x10[1])
        label.delete(x11[1])
        label.delete(x12[1])
        label.delete(x13[1])
        label.delete(x14[1])
        
        vixLine3 := line.new(bar_index + 151,  box.get_top(vixBox) , bar_index + 151, label.get_y(vixLabel), style =line.style_dashed, color = color.white)

        // Plots "Color Commentary"
        
        if BaW == "Color"
           
            x := label.new(bar_index + 122, array.min(trackLow) * .965, text = "C", color = na, textcolor = tvCol == "On" ? aGet2 : na)
            x1 := label.new(bar_index + 124,  array.min(trackLow) * .965, text = "o", color = na, textcolor = tvCol == "On" ? aGet2[1] : na)
            x2 := label.new(bar_index + 126,  array.min(trackLow) * .965, text = "l", color = na, textcolor = tvCol == "On" ? aGet2[2] : na)
            x3 := label.new(bar_index + 128,  array.min(trackLow) * .965, text = "o", color = na, textcolor = tvCol == "On" ? aGet2[3] : na)
            x4 := label.new(bar_index + 130,  array.min(trackLow) * .965, text = "r", color = na, textcolor = tvCol == "On" ? aGet2[4] : na)
            x5 := label.new(bar_index + 132,  array.min(trackLow) * .965, text = " C", color = na, textcolor = tvCol == "On" ? aGet2[5] : na)
            x6 := label.new(bar_index + 134,  array.min(trackLow) * .965, text = "o", color = na, textcolor = tvCol == "On" ? aGet2[0] : na)
            x7 := label.new(bar_index + 136,  array.min(trackLow) * .965, text = "m", color = na, textcolor = tvCol == "On" ? aGet2[1] : na)
            x8 := label.new(bar_index + 138,  array.min(trackLow) * .965, text = " m", color = na, textcolor = tvCol == "On" ? aGet2[2] : na)
            x9 := label.new(bar_index + 140,  array.min(trackLow) * .965, text = "  e", color = na, textcolor = tvCol == "On" ? aGet2[3] : na)
            x10 := label.new(bar_index + 142,  array.min(trackLow) * .965, text = " n", color = na, textcolor = tvCol == "On" ? aGet2[4] : na)
            x11 := label.new(bar_index + 144,  array.min(trackLow) * .965, text = " t", color = na, textcolor = tvCol == "On" ? aGet2[5] : na)
            x12 := label.new(bar_index + 146,  array.min(trackLow) * .965, text = " a", color = na, textcolor = tvCol == "On" ? aGet2[0] : na)
            x13 := label.new(bar_index + 148,  array.min(trackLow) * .965, text = " r", color = na, textcolor = tvCol == "On" ? aGet2[1] : na)
            x14 := label.new(bar_index + 150,  array.min(trackLow) * .965, text = " y", color = na, textcolor = tvCol == "On" ? aGet2[2] : na)
            
        // Plots Additional VIX Statistics
        
        labelX3 := label.new(bar_index + 136,  array.min(trackLow) * .85, text =  "VIX Gain/loss: " +
          str.tostring(vgl, format.percent) + vixGainLossString + "\n\nVixFix Gain/Loss: " + str.tostring(vfgl, format.percent) + vixFixGainLossString + "\n\nS&P  500 Put/Call Ratio: " 
             + str.tostring(spx, format.mintick) + "\n" + pcString + "\n\n50-Session Average \nS&P  500 Put/Call Ratio: " + str.tostring(ratioSMA, format.mintick)
                 , style = label.style_label_center, color = color.new(color.white, 100), textcolor = color.white)
       
        label.delete(labelX4[1])
        labelX4 := label.new(bar_index + 136, label.get_y(labelX3) * .75, text = vClose > 30 ? "VIX IS PRICED AT $" + str.tostring(vClose, format.mintick) + " \n(STAY ALERT)" : na, 
             color = na, textcolor = color.red)
    
// Plots "Tradingview TV" Label
if bar_index > bar_index[1] and vixTV == "No"
    TV := label.new(bar_index + 117, box.get_top(vixBox), text = select == "VIX" ? "Tradingview TV: VIX" : "Tradingview TV: VixFix", color = na, textcolor = color.white, size = labelSize == "Tiny" ? size.tiny : labelSize == "Small" ? size.small : size.auto)

// Colors Bars Where VIX/VixFix Closed Above Upper Band
box.set_bgcolor(vixBox, color.new(#000000, 0))

// _______________________________________________________
//
// VIX > 40 Box Plot                          
// _______________________________________________________   

lowYaxis = ta.lowest(low, 50)
lowYaxis1 = ta.lowest((year(time) > timestamp(1989, 12, 31) ? bar_index - y4 : 1)) 

if bar_index - y4 > 5000
    y4 := bar_index

if ta.crossover(vClose, 40) and y5 == 0
   
    y1 := high
    y4 := bar_index
    y5 := 1
    y3 := close
    j := line.new(y4, y1, y4, high)
    j1 := line.new(y4, y1, y4, lowYaxis)
    j2 := line.new(y4, lowYaxis , bar_index, lowYaxis)
    j3 := line.new(bar_index + 1, high, bar_index + 1, lowYaxis)
    j4 := linefill.new(j2, j3, color.new(color.blue, 75))
    y6 += 1
    nx := line.new(y4, lowYaxis, y4 - 1, line.get_y2(j2) * .98, color = color.white, style = line.style_dashed)
    nx1 := line.new(y4, lowYaxis, y4, line.get_y2(j2) * .98, color = color.white, style = line.style_dashed)
    n := label.new(y4 - 1, line.get_y2(j2) * .98, text = "VIX > 40", color= color.new(color.blue, 50), size = size.auto, style = label.style_label_up, textcolor = color.white)
    n1 := label.new(bar_index, line.get_y2(j2) * .98, text = close <= y3 ? "Loss: " + str.tostring(((close /y3) - 1) * 100, format.percent) : "Gain: " 
         + str.tostring(((close /y3) - 1) * 100, format.percent), color= color.new(color.blue, 50), textcolor = color.white, size = labelSize == "Tiny" ? size.tiny : labelSize == "Small" ? size.small : size.auto, style = label.style_label_up)

if y5 == 1 and y5[1] == 1
   
    line.delete(j[1])
    line.delete(j1[1])
    line.delete(j2[1])
    line.delete(j3[1])
    linefill.delete(j4[1])
    line.delete(nx[1])
    line.delete(nx1)
    label.delete(n[1])
    label.delete(n1[1])
    
    j2 := line.new(y4, lowYaxis, bar_index + 1, lowYaxis)
    j1 := line.new(y4, y1, y4, lowYaxis)
    j := line.new(y4, y1, bar_index + 1, high)
    j3 := line.new(bar_index + 1, high, bar_index + 1, lowYaxis)
    j4 := linefill.new(j1, j3, color.new(color.blue, 75))
    nx := line.new(y4, lowYaxis, y4 - 1, line.get_y2(j2) * .98, color = color.white, style = line.style_dashed)
    nx1 := line.new(bar_index, lowYaxis, bar_index, line.get_y2(j2) * .98, color = color.white, style = line.style_dashed)
    n := label.new(y4 - 1, line.get_y2(j2) * .98, text = "VIX > 40", color= color.new(color.blue, 50), size = size.auto, style = label.style_label_up, textcolor = color.white)
    n1 := label.new(bar_index, line.get_y2(j2) * .98, text = close <= y3 ? "Loss: " + str.tostring(((close /y3) - 1) * 100, format.percent) : "Gain: " 
         + str.tostring(((close /y3) - 1) * 100, format.percent), color= color.new(color.blue, 50), textcolor = color.white, size = labelSize == "Tiny" ? size.tiny : labelSize == "Small" ? size.small : size.auto, style = label.style_label_up)
    y6 += 1

if ta.crossunder(vClose, 40) and y5 == 1 
   
    label.delete(n1)
    line.delete(nx1)
   
    nx2 := line.new(bar_index + 1, lowYaxis, bar_index + 2, line.get_y2(j2) * .98, color = color.white, style = line.style_dashed)
    n4 := label.new(math.round(math.avg(y4, bar_index)), line.get_y2(j2) * .95, text = close <= y3 ? "Greatest Loss: " + str.tostring(((lowYaxis1 / y3) - 1) * 100, format.percent) : na, color= color.new(color.blue, 50), textcolor = color.white, size = labelSize == "Tiny" ? size.tiny : labelSize == "Small" ? size.small : size.auto, style = label.style_label_up)
    nx3 := line.new(math.round(math.avg(y4, bar_index)), line.get_y2(j2),math.round(math.avg(y4, bar_index)), line.get_y2(j2) * .95 , color = color.white, style = line.style_dashed)
    n2 := label.new(bar_index + 2, line.get_y2(nx2), text = "VIX < 40", color = color.new(color.blue, 50), textcolor = color.white, size = size.auto, style = label.style_label_up)
    y5 := 0
    y6 := 0
if y6 == 0 and y6[1] < 10
    
    line.delete(j)
    line.delete(j1)
    line.delete(j2)
    line.delete(j3)
    linefill.delete(j4)
    line.delete(nx)
    line.delete(nx1)
    line.delete(nx2)
    label.delete(n)
    label.delete(n1)
    label.delete(n2)
    label.delete(n4)
    line.delete(nx3)

if y6 == 0 
    line.delete(nx1)

// _______________________________________________________
//
// Array Delete                              
// _______________________________________________________    
    

if array.size(label.all) > labelCount
    label.delete(array.get(label.all, 0))
    int(na)

// _______________________________________________________
//
// Barcolor                                 
// _______________________________________________________    

col1 = input.color(#0020ff, title = "VIX Closed Above Upper Band Bar Color")
col2 = input.color(#5d00ff, title = "VixFix Closed Above Upper Band Bar Color")

barcolor(VorVFclose > upper and select == "VIX" ? col1 : VorVFclose > upper and select == "VixFix" ? col2 : na, title = "VIX and VixFix > Upper Band Bar Color")

// _______________________________________________________
//
// Table (Optional)                          
// _______________________________________________________  

table1 = hide == "Yes" ? table.new(position.bottom_right, 10, 10) : na

if hide == "Yes" 
    table.cell(table1, 0, 0, text =  "VIX Gain/loss: " +
     str.tostring(vgl, format.percent) + vixGainLossString + "\n\nVixFix Gain/Loss: " + str.tostring(vfgl, format.percent) + vixFixGainLossString + "\n\nS&P  500 Put/Call Ratio: " 
         + str.tostring(spx, format.mintick) + "\n" + pcString + "\n\n50-Session Average \nS&P  500 Put/Call Ratio: " + str.tostring(ratioSMA, format.mintick), text_color = color.white) 
else if hide == "No"
    na

