BankNifty targets using VIX Version 2
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © nanujogi.

// Credit: cusomFonts thanks flies to kaigouthro.
// https://www.tradingview.com/script/vmuJYczR-font/

//@version=5

MAXLABELS = 125

indicator("BankNifty targets using VIX Version 2", shorttitle = "VIX", overlay = true, max_bars_back = MAXLABELS, max_labels_count = MAXLABELS)

import kaigouthro/font/5 as customFonts


// STEP 1. Make an input with drop-down menu
sizeOption = input.string(defval="Normal", title="Font Size", options=["Auto", "Huge", "Large", "Normal", "Small", "Tiny"], tooltip = "Select your font size")
     
// STEP 2. Convert the input to valid label sizes
labelSize = (sizeOption == "Huge") ? size.huge :
     (sizeOption == "Large") ? size.large :
     (sizeOption == "Small") ? size.small :
     (sizeOption == "Tiny") ? size.tiny :
     (sizeOption == "Auto") ? size.auto :
         size.normal


FontOption = input.string(defval="Default", title="Select Fonts", options=["Sans", "Sans Italic", "Sans Bold", "Sans Bold Italic", "Sans-Serif", "Sans-Serif Italic", "Sans-Serif Bold", "Sans-Serif Bold Italic", 
     "Fraktur", "Fraktur Bold", 
     "Script", "Script Bold",  
     "Double-Struck", 
     "Monospace", 
     "Regional Indicator", 
     "Small", 
     "Full Width", 
     "Circled", 
     "Default"], tooltip = "Select your font")
     
FontSelected = (FontOption == "Sans") ? 1 :
     (FontOption == "Sans Italic") ? 2 :
     (FontOption == "Sans Bold") ? 3 :
     (FontOption == "Sans Bold Italic") ? 4 :
     (FontOption == "Sans-Serif") ? 5 :
     (FontOption == "Sans-Serif Italic") ? 6 :
     (FontOption == "Sans-Serif Bold") ? 7 :
     (FontOption == "Sans-Serif Bold Italic") ? 8 :
     (FontOption == "Fraktur") ? 9 :
     (FontOption == "Fraktur Bold") ? 10 :
     (FontOption == "Script") ? 11 :
     (FontOption == "Script Bold") ? 12 :
     (FontOption == "Double-Struck") ? 13 :
     (FontOption == "Monospace") ? 14 :
     (FontOption == "Regional Indicator") ? 15 :
     (FontOption == "Small") ? 16 :
     (FontOption == "Full Width") ? 17 :
     (FontOption == "Circled") ? 18 :
         3 // Default

/// Static Strings used in Table using HardCode Font Sans Bold i.e. Default
str_Index = "Index"
str_BankNifty = "BankNifty"
str_Nifty = "Nifty"
str_Daily_High_and_Low = "Daily High & Low"
str_Weekly_High_and_Low = "Weekly High & Low"
str_Monthly_High_and_Low = "Monthly High & Low"
str_Yearly_High_and_Low = "Yearly High & Low"
str_H = "H"
str_L = "L"

str_Basis = "Basis"
str_Basis_Current = "Current"
str_Basis_High = "High"
str_Basis_Low = "Low"
str_percentage_Left = "% Left"

str_dottedLines =  "‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒"
str_Abbrevation = "𝗩𝗛 = VIX High\n" + 
 "\𝗛 > 𝗩𝗛𝗗 = High is greater than VIX High Difference meaning how many points higher it went above VIX High.\n" +
 "\𝗛𝗗% = High Difference in % (Percentage)\n" + 
  str_dottedLines + 
  "\n𝗩 = VIX Current INDIA VIX\n" + "𝗩𝗗 = VIX Difference in points\n" + "𝗩𝗗% = VIX Difference in % (Percentage)\n" +
  str_dottedLines +
 "\n𝗛𝗕𝗗 = High Basis Difference i.e. When High was being Made in Spot the Future price difference is shown.\n" +
 "\𝗕𝗟 = Basis Low i.e. When Low was made Basis difference is shown.\n" + "𝗕𝗖 = Basis Close\n" + 
  str_dottedLines +
 "𝗩𝗟 = VIX Low\n" + 
 "\𝗟 < 𝗩𝗟𝗗 = Low is less then VIX Low meaning how many points lower it went below VIX Low.\n" +
 "\𝗟𝗗% = Low Difference in % (Percentage)\n"

color_for_higher_then_average = color.aqua

color AvgHigh_VixHigh_color = na
color AvgHigh_VixLow_color = na

color AvgLow_VixHigh_color = na
color AvgLow_VixLow_color = na

color Txt_Color_AvgHigh_VixHigh = na
color Txt_Color_AvgHigh_VixLow = na

color Txt_Color_AvgLow_VixHigh = na
color Txt_Color_AvgLow_VixLow = na



//  strtooltipVixHigh = "\n\nV\nI\nX\n\nH\nI\nG\nH"
// strtooltipVixLow = "\n\nV\nI\nX\n\nL\nO\nW"

show_Averages = input.bool(true, "Show Averages of High & Low") // Show averages
show_vix = input.bool(true, "Show Vix") // Show VIX
show_Vix_calculated_high = input.bool(true, "Show calculated VIX High", tooltip = "Show calculated VIX High for the current Day") // Show calculated VIX High for the current Day
show_Vix_calculated_low = input.bool(true,  "Show calculated VIX Low ", tooltip = "Show calculated VIX Low for the current Day")

show_previous_day_vix_high  = input.bool(true, "Show Previous Day Vix High", tooltip = "Show previous day calculated VIX High")
show_previous_day_vix_low = input.bool(true,   "Show Previous Day Vix Low ", tooltip = "Show previous day calculated VIX Low")

show_Table = input.bool(true, "Show Table", tooltip = "Show calculated Table") // Show calculated Table

show_NiftyVIX = input.bool(false, "Show Nifty VIX", tooltip = "Select this if you need to see Nifty VIX inside table") 
show_data_inToolTip = input.bool(false, "Show data in Tool Tip", tooltip = "Select this if you need to Data as ToolTip so there is less clutter on screen") 


DtimeFrame = math.sqrt(365)     // 19.10 approx values
WtimeFrame = math.sqrt(52)      // 7.21 approx values
MtimeFrame = math.sqrt(12)      // 3.16 approx values
YtimeFrame = math.sqrt(1)

bndHighReached = false
bndLowReached = false
isit_BankNifty = false

if ticker.standard() == "NSE:BANKNIFTY"
    isit_BankNifty := true

//// Implemented Day Time Frame checks. Now will show an Yellow Box in Middle if user is has selected One Hour Time Frame.
/// Thanks flies out to BobRivera990 for shareing source code of Weekly Volume Heatmap.

var string show_user_correct_timeframe = "Please use Day Time Frame on BankNifty Spot !!!"

var string msg_user_to_select_BankNifty = "Please select BankNifty Spot !!!"

var bool correct_TimeFrame = timeframe.multiplier == 1

var table dialogBox = table.new(position.middle_center, 1, 1, bgcolor = color.yellow)

showDialog(_dialogBox, _msg) =>
    table.cell(_dialogBox, 0, 0, _msg, text_color = color.red, text_size = size.large)
    
// if not correct_TimeFrame 
//     showDialog(dialogBox, show_user_correct_timeframe)

if not isit_BankNifty 
    showDialog(dialogBox, msg_user_to_select_BankNifty)

[vixDaily, pdayVix, vixClose] = request.security("NSE:INDIAVIX", "D", [close, close[1], close])

[niftyDaily, ndHigh, ndLow, ndPHigh, ndPLow, npClose] = request.security("NSE:NIFTY", "D", [close, high, low, high[1], low[1], close[1]])
[bankniftyDaily, bndHigh, bndLow, bndPHigh, bndPLow,bnpClose] = request.security("NSE:BANKNIFTY", "D", [close, high, low, high[1], low[1], close[1]])

// For Basis calculation we need Future Price
[futureBNclose, futureBNHigh, futureBNLow] = request.security("NSE:BANKNIFTY1!", "D", [close, high, low])

niftyWeekly = request.security("NSE:NIFTY", "W", close)
bankniftyWeekly = request.security("NSE:BANKNIFTY", "W", close)

niftyMonthly = request.security("NSE:NIFTY", "M", close)
bankniftyMonthly = request.security("NSE:BANKNIFTY", "M", close)

niftyYearly = request.security("NSE:NIFTY", "365", close)
bankniftyYearly = request.security("NSE:BANKNIFTY", "365", close)

string  tableYposInput = input.string("top", "Table position", inline = "11", options = ["top", "middle", "bottom"])
string  tableXposInput = input.string("right", "", inline = "11", options = ["left", "center", "right"])

dRange = vixDaily / DtimeFrame
wRange = vixDaily / WtimeFrame
mRange = vixDaily / MtimeFrame
yRange = vixDaily /YtimeFrame

// Secret Calculation 😄

vixdNiftyHigh = niftyDaily + ((niftyDaily * dRange)/100)
vixdNiftyLow = niftyDaily - ((niftyDaily * dRange)/100)

vixwNiftyHigh = niftyWeekly + ((niftyWeekly * wRange)/100)
vixwNiftyLow = niftyWeekly - ((niftyWeekly * wRange)/100)

mNiftyHigh = niftyMonthly + ((niftyMonthly * mRange)/100)
mNiftyLow = niftyMonthly - ((niftyMonthly * mRange)/100)

yNiftyHigh = niftyYearly + ((niftyYearly * yRange)/100)
yNiftyLow = niftyYearly - ((niftyYearly * yRange)/100)

vixdBankNiftyHigh = bankniftyDaily + ((bankniftyDaily * dRange)/100)
vixdBankNiftyLow = bankniftyDaily - ((bankniftyDaily * dRange)/100)

wBankNiftyHigh = bankniftyWeekly + ((bankniftyWeekly * wRange)/100)
wBankNiftyLow = bankniftyWeekly - ((bankniftyWeekly * wRange)/100)

mBankNiftyHigh = bankniftyMonthly + ((bankniftyMonthly * mRange)/100)
mBankNiftyLow = bankniftyMonthly - ((bankniftyMonthly * mRange)/100)

yBankNiftyHigh = bankniftyYearly + ((niftyYearly * yRange)/100)
yBankNiftyLow = bankniftyYearly - ((niftyYearly * yRange)/100)

// Let's Plot them.
plot(show_vix ? vixDaily : na, color=color.yellow, title="India VIX")
plot(show_Vix_calculated_high ? vixdBankNiftyHigh : na, title="Nifty Daily High", color=color.green)
plot(show_Vix_calculated_low ? vixdBankNiftyLow : na, title ="Nifty Daily Low", color=color.red)

// Calculation for how far is the targets of Daily, Weekly, Monthly and Yearly. 
how_far_is_high_and_low_daily = (vixdBankNiftyHigh * 100 / bankniftyDaily) - 100
how_far_is_high_and_low_weekly = (wBankNiftyHigh * 100 / bankniftyDaily) - 100
how_far_is_high_and_low_monthly = (mBankNiftyHigh * 100 / bankniftyDaily)- 100
how_far_is_high_and_low_yearly = (yBankNiftyHigh * 100 / bankniftyDaily) - 100

basisHigh = futureBNHigh - bndHigh // difference between Future High - Spot High = Basis
basisclose = futureBNclose - bankniftyDaily // difference between Future Close - Spot Close = Closing Basis Difference
basisLow = futureBNLow - bndLow // difference between Future Low - Spot Low = Low Basis Difference

var table vixTable = table.new(tableYposInput + "_" + tableXposInput, columns=5, rows=5, border_color = color.white, frame_color = color.white, frame_width = 1, border_width = 1)


if barstate.islast and show_Table // You can disable the table on screen if required.
   // We only populate the table on the last bar.
    table.cell(vixTable, 0, 0, text=customFonts.uni(str_Index, 3), text_color = color.rgb(210,219,35), text_halign = text.align_center, text_size = size.normal, bgcolor = color.black, tooltip="Daily EMA")
    table.cell(vixTable, 1, 0, text=customFonts.uni(str_Daily_High_and_Low, 3), text_color = color.rgb(210,219,35), text_halign = text.align_center, text_size = size.normal, bgcolor = color.black, tooltip="Daily High & Low")
    table.cell(vixTable, 2, 0, text=customFonts.uni(str_Weekly_High_and_Low, 3), text_color = color.rgb(210,219,35), text_halign = text.align_center, text_size = size.normal, bgcolor = color.black, tooltip="Weekly High & Low")
    table.cell(vixTable, 3, 0, text=customFonts.uni(str_Monthly_High_and_Low, 3), text_color = color.rgb(210,219,35), text_halign = text.align_center, text_size = size.normal, bgcolor = color.black, tooltip="Monthly High & Low ")
    table.cell(vixTable, 4, 0, text=customFonts.uni(str_Yearly_High_and_Low, 3), text_color = color.rgb(210,219,35), text_halign = text.align_center, text_size = size.normal, bgcolor = color.black, tooltip="Yearly High & Low")

    if show_NiftyVIX
        table.cell(vixTable, 0, 1, text=customFonts.uni(str_Nifty, 3), text_color=color.rgb(210,219,35), text_size = size.normal, bgcolor = color.black)
        table.cell(vixTable, 1, 1, text_halign = text.align_center, text_size = size.normal, text = customFonts.uni(str_H, 3) + str.format("{0, number, #.##}", vixdNiftyHigh) + "\n" + customFonts.uni(str_L, 3) + " " + str.format("{0, number, #.##}", vixdNiftyLow), text_color = #00A2ED)
        table.cell(vixTable, 2, 1, text_halign = text.align_center, text_size = size.normal, text = customFonts.uni(str_H, 3) + str.format("{0, number, #.##}", vixwNiftyHigh) + "\n" + customFonts.uni(str_L, 3) + " " + str.format("{0, number, #.##}", vixwNiftyLow), text_color = color.lime)
        table.cell(vixTable, 3, 1, text_halign = text.align_center, text_size = size.normal, text = customFonts.uni(str_H, 3) + str.format("{0, number, #.##}", mNiftyHigh) + "\n" + customFonts.uni(str_L, 3) + " " + str.format("{0, number, #.##}", mNiftyLow), text_color = color.orange)
        table.cell(vixTable, 4, 1, text_halign = text.align_center, text_size = size.normal, text = customFonts.uni(str_H, 3) + str.format("{0, number, #.##}", yNiftyHigh) + "\n" + customFonts.uni(str_L, 3) + " " + str.format("{0, number, #.##}", yNiftyLow), text_color = color.green)

    table.cell(vixTable, 0, 2, text=customFonts.uni(str_BankNifty, 3), text_color=color.rgb(210,219,35), text_size = size.normal,bgcolor = color.black)
    table.cell(vixTable, 1, 2, text_halign = text.align_center, text_size = size.normal, text = customFonts.uni(str_H, 3) + str.format("{0, number, #.##}", vixdBankNiftyHigh) +"\n" + customFonts.uni(str_L, 3) + " "  + str.format("{0, number, #.##}", vixdBankNiftyLow), text_color = #00A2ED)
    table.cell(vixTable, 2, 2, text_halign = text.align_center, text_size = size.normal, text = customFonts.uni(str_H, 3) + str.format("{0, number, #.##}", wBankNiftyHigh) +  "\n" + customFonts.uni(str_L, 3) + " " + str.format("{0, number, #.##}", wBankNiftyLow), text_color = color.lime)
    table.cell(vixTable, 3, 2, text_halign = text.align_center, text_size = size.normal, text = customFonts.uni(str_H, 3) + str.format("{0, number, #.##}", mBankNiftyHigh) + "\n" + customFonts.uni(str_L, 3) + " "  + str.format("{0, number, #.##}", mBankNiftyLow), text_color = color.orange)
    table.cell(vixTable, 4, 2, text_halign = text.align_center, text_size = size.normal, text = customFonts.uni(str_H, 3) + str.format("{0, number, #.##}", yBankNiftyHigh) + "\n" + customFonts.uni(str_L, 3) + " " + str.format("{0, number, #.##}", yBankNiftyLow), text_color = color.green)

    table.cell(vixTable, 0, 3, text=customFonts.uni(str_percentage_Left, 3), text_color=color.rgb(210,219,35), text_size = size.normal,bgcolor = color.black)
    table.cell(vixTable, 1, 3, text= " ⦿ "  + str.format("{0, number, #.####}", how_far_is_high_and_low_daily) + "%", text_color=color.rgb(210,219,35), text_size = size.normal,bgcolor = color.black)
    table.cell(vixTable, 2, 3, text= " ⦿ "  + str.format("{0, number, #.##}", how_far_is_high_and_low_weekly) + "%", text_color=color.rgb(210,219,35), text_size = size.normal,bgcolor = color.black)
    table.cell(vixTable, 3, 3, text= " ⦿ "  + str.format("{0, number, #.####}", how_far_is_high_and_low_monthly) + "%", text_color=color.rgb(210,219,35), text_size = size.normal,bgcolor = color.black)
    table.cell(vixTable, 4, 3, text= " ⦿ "  + str.format("{0, number, #.##}", how_far_is_high_and_low_yearly) + "%", text_color=color.rgb(210,219,35), text_size = size.normal,bgcolor = color.black)

    table.cell(vixTable, 0, 4, text= customFonts.uni(str_Basis, 3), text_color=color.rgb(210,219,35), text_size = size.normal,bgcolor = color.black, tooltip = str_Abbrevation)
    table.cell(vixTable, 1, 4, text= " " + customFonts.uni(str_Basis_Current, 3) + str.format("{0, number, #.##}", basisclose) + "  ⦿  " +
     customFonts.uni(str_Basis_High, 3) + str.format("{0, number, #.##}", basisHigh) +  "  ⦿  " +
     customFonts.uni(str_Basis_Low, 3) + str.format("{0, number, #.##}", basisLow), 
     text_color=color.rgb(210,219,35), text_size = size.normal,bgcolor = color.black, text_halign=text.align_left)
    table.merge_cells(vixTable,1,4,4,4)

pdRange = pdayVix / DtimeFrame

PvixdbnHigh = bnpClose + ((bnpClose * pdRange)/100)
PvixdbnLow = bnpClose - ((bnpClose * pdRange)/100)

bndHighReached := bndHigh > PvixdbnHigh
bndLowReached := bndLow < PvixdbnLow

plot(show_previous_day_vix_high ? PvixdbnHigh : na, color=color.white, title = "Previous Day High")
plot(show_previous_day_vix_low ? PvixdbnLow : na ,color = color.white, title = "Previous Day Low")

// % calculation. How higher it went from previous day calculated high price.
get_high_difference = bndHigh - PvixdbnHigh
high_percentage_calculation = ((bndHigh * 100) / PvixdbnHigh) - 100

// // % calculation. How low it went from previous day calculated high price.
get_low_difference = bndLow - PvixdbnLow
low_percentage_calculation = ((bndLow * 100) / PvixdbnLow) - 100

// Date: 09-07-2023 added this option to show Averages of % High and % Low BankNifty went above VIX High and VIX Low

///// Show Averages STARTS: 
/// Whenever and VIX High or VIX Low is reached we are saving in an array the % of BankNifty went above or below VIX High or VIX Low.
// We then use array.avg inbuilt function to dislpay the averages. 

var high_Percent_Array = array.new_float(0)
var low_Percent_Array = array.new_float(0)

if bndHighReached
	array.push(high_Percent_Array, math.round(high_percentage_calculation, 2))

if bndLowReached
	array.push(low_Percent_Array, math.round(low_percentage_calculation, 2))

/// Show Averages ENDS

// calculation of VIX difference & percentage

vix_difference = vixClose - pdayVix // pdayVix = Previous Day Vix
vix_perecentage = ((vixClose * 100) / pdayVix) - 100

show_vix_upArrow = vixClose > pdayVix
show_vix_downArrow = vixClose < pdayVix

// Logs incorporated for Study date 31-08-2023

if bndHighReached
    log.warning ("BankNifty went Above \n
      Calculated VIX High was {0,number,#.##}\n 
      High it went {1, number, #.##} it was {2, number, #.##} points higher the VIX High\n
      High Difference in (%) : {3, number, #.##}(%) \n
      VIX is : {4, number, #.##} Basis : {5, number, #.##}", PvixdbnHigh, bndHigh, get_high_difference, high_percentage_calculation, vixDaily, basisclose)

if bndLowReached
    log.info ("BankNifty went Below \n
      Calculated VIX Low was {0,number,#.##}\n 
      Low it went {1, number, #.##} it was {2, number, #.##} points higher the VIX Low\n
      Low Difference in (%) : {3, number, #.##}(%) \n
      VIX is : {4, number, #.##} Basis : {5, number, #.##}", PvixdbnLow, bndLow, get_low_difference, low_percentage_calculation, vixDaily, basisclose)


showStr_avg_High = show_Averages ? "\n\nAvg. High : " + str.format("{0, number, #.##}", array.avg(high_Percent_Array)) : " "
showStr_avg_Low = show_Averages ? "\n\nAvg Low : " + str.format("{0, number, #.##}", array.avg(low_Percent_Array)) : " "

// Presentation Matters 😄

str_vix_High_upArrow = "VH:" + str.format("{0, number, #.##}", PvixdbnHigh) +"" + "\n" + "H>HVD: " + 
     str.format("{0, number, #.##}", get_high_difference) + "⬆" + "\n" + "HD% : " + str.format("{0, number, #.##}", high_percentage_calculation)+"%"+ 
     "\n\nV: " + str.format("{0, number, #.##}", vixDaily) + " ⦿ VD: " + str.format("{0, number, #.####}", vix_difference) + "⬆" +
     "\nVD%: " + str.format("{0, number, #.##}", vix_perecentage)+"%" + // str_formatHighTime +
     "\nHBD: " + str.format("{0, number, #.##}", basisHigh) +
     " ⦿ BL: " + str.format("{0, number, #.##}", basisLow) +
      "\nBC: " + str.format("{0, number, #.##}", basisclose) + showStr_avg_High

      
str_vix_High_downArrow = "VH:" + str.format("{0, number, #.##}", PvixdbnHigh) +"" + "\n" + "H>VHD: " + 
     str.format("{0, number, #.##}", get_high_difference) + "⬆" + "\n" + "HD% : " + str.format("{0, number, #.##}", high_percentage_calculation)+"%"+ 
     "\n\nV: " + str.format("{0, number, #.##}", vixDaily) + " ⦿ VD: " + str.format("{0, number, #.####}", vix_difference) + "⬇" +
     "\nVD%: " + str.format("{0, number, #.##}", vix_perecentage)+"%"+ // str_formatHighTime + 
     "\nHBD: " + str.format("{0, number, #.##}", basisHigh) +
     " ⦿ BL: " + str.format("{0, number, #.##}", basisLow) +
     "\nBC: " + str.format("{0, number, #.##}", basisclose) + showStr_avg_High

// Colors when Vix High % is more then Average Price
AvgHigh_VixHigh_color := high_percentage_calculation > array.avg(high_Percent_Array) ? #0A3161 : #014421
AvgHigh_VixLow_color := high_percentage_calculation > array.avg(high_Percent_Array)  ? #0A3161 : #DFFE0D

Txt_Color_AvgHigh_VixHigh := high_percentage_calculation > array.avg(high_Percent_Array) ? color.white : #FADA5E
Txt_Color_AvgHigh_VixLow := high_percentage_calculation > array.avg(high_Percent_Array)  ? color.white : color.black
      
// Showing data inside ToolTip.
if show_vix_upArrow and bndHighReached and show_data_inToolTip
    label.new(bar_index, na, color = AvgHigh_VixHigh_color, style=label.style_label_down,text="VIX HIGH", //customFonts.uni(strtooltipVixHigh, FontSelected) ,
    // label.new(bar_index, na, color = #014421, style=label.style_label_down,text="V\nI\nX\n\nH\nI\nG\nH", //customFonts.uni(strtooltipVixHigh, FontSelected) ,
     textcolor = Txt_Color_AvgHigh_VixHigh, textalign = text.align_center, size = labelSize, yloc = yloc.abovebar, text_font_family = font.family_default, tooltip = str_vix_High_upArrow)

else if show_vix_downArrow and bndHighReached and show_data_inToolTip
    label.new(bar_index, na, color = AvgHigh_VixLow_color, style=label.style_label_down,text="VIX HIGH",// customFonts.uni(strtooltipVixHigh, FontSelected),
    // label.new(bar_index, na, color = #DFFE0D, style=label.style_label_down,text="V\nI\nX\n\nH\nI\nG\nH",// customFonts.uni(strtooltipVixHigh, FontSelected),
     textcolor = Txt_Color_AvgHigh_VixLow, textalign = text.align_center, size = labelSize, yloc = yloc.abovebar, text_font_family = font.family_default, tooltip = str_vix_High_downArrow)


// ORIGINAL 
if show_vix_upArrow and bndHighReached and not show_data_inToolTip
    label.new(bar_index, na, color = AvgHigh_VixHigh_color, style=label.style_label_down,text=customFonts.uni(str_vix_High_upArrow, FontSelected),
     textcolor = Txt_Color_AvgHigh_VixHigh, textalign = text.align_center, size = labelSize, yloc = yloc.abovebar, text_font_family = font.family_default)

else if show_vix_downArrow and bndHighReached and not show_data_inToolTip
    label.new(bar_index, na, color = AvgHigh_VixLow_color, style=label.style_label_down,text=customFonts.uni(str_vix_High_downArrow, FontSelected),
     textcolor = Txt_Color_AvgHigh_VixLow, textalign = text.align_center, size = labelSize, yloc = yloc.abovebar, text_font_family = font.family_default)

// String formating for showing VIX Low with upArrow for customFonts Library. VIX is higher then previous day hence upArrow
str_vix_low_upArrow = "VL: " + str.format("{0, number, #.##}", PvixdbnLow)+"" + "\n" + "L<VLD: " + 
     str.format("{0, number, #.##}", get_low_difference) + "⬇" + "\n" + "LD%: " + str.format("{0, number, #.##}", low_percentage_calculation)+"%"+ 
     "\n\nV: " + str.format("{0, number, #.##}", vixDaily) + " ⦿ VD: " + str.format("{0, number, #.####}", vix_difference) + "⬆" +
     "\nVD%: " + str.format("{0, number, #.##}", vix_perecentage)+"%" +
     "\nHBD: " + str.format("{0, number, #.##}", basisHigh) +
     " ⦿ BL: " + str.format("{0, number, #.##}", basisLow) +
      "\nBC: " + str.format("{0, number, #.##}", basisclose) + showStr_avg_Low

      
// String formating for showing VIX Low with Down Arrow for customFonts Library. VIX is Lower then previous day hence downArrow.
str_vix_low_downArrow = "VL:" + str.format("{0, number, #.##}", PvixdbnLow) + "" + "\n" + "L<VLD: " + 
     str.format("{0, number, #.##}", get_low_difference) + "⬇" + "\n" + "LD% : " + str.format("{0, number, #.##}", low_percentage_calculation)+"%"+ 
     "\n\nV: " + str.format("{0, number, #.##}", vixDaily) + " ⦿ VD: " + str.format("{0, number, #.####}", vix_difference) + "⬇" +
     "\nV%: " + str.format("{0, number, #.##}", vix_perecentage)+"%" +
     "\nHBD: " + str.format("{0, number, #.##}", basisHigh) +
     " ⦿ BL: " + str.format("{0, number, #.##}", basisLow) +
      "\nBC: " + str.format("{0, number, #.##}", basisclose) + showStr_avg_Low


// Colors when Vix Low > Average VIX Low %
AvgLow_VixHigh_color := low_percentage_calculation < array.avg(low_Percent_Array) ? #fe1493 : #8b0000
AvgLow_VixLow_color := low_percentage_calculation < array.avg(low_Percent_Array) ? #fe1493 : #D31A38 // 

if bndLowReached and show_vix_upArrow and show_data_inToolTip
    label.new(bar_index, na, color = AvgLow_VixHigh_color, style=label.style_label_up,text="VIX LOW", // customFonts.uni(strtooltipVixLow, FontSelected),
    // label.new(bar_index, na, color = #8b0000, style=label.style_label_up,text="V\nI\nX\n\nL\nO\nW",// customFonts.uni(strtooltipVixLow, FontSelected),
      textcolor = color.white, textalign = text.align_center, size = labelSize, yloc = yloc.belowbar, text_font_family = font.family_default, tooltip = str_vix_low_upArrow)
     
else if bndLowReached and show_vix_downArrow and show_data_inToolTip
    label.new(bar_index, na, color = AvgLow_VixLow_color, style=label.style_label_up,text="VIX LOW", // customFonts.uni(strtooltipVixLow, FontSelected),
    // label.new(bar_index, na, color = #D31A38, style=label.style_label_up,text="V\nI\nX\n\nL\nO\nW", // customFonts.uni(strtooltipVixLow, FontSelected),
     textcolor = color.white, textalign = text.align_center, size = labelSize, yloc = yloc.belowbar, text_font_family = font.family_default, tooltip = str_vix_low_downArrow)

if bndLowReached and show_vix_upArrow and not show_data_inToolTip
    label.new(bar_index, na, color = AvgLow_VixHigh_color, style=label.style_label_up,text=customFonts.uni(str_vix_low_upArrow, FontSelected),
     textcolor = color.white, textalign = text.align_center, size = labelSize, yloc = yloc.belowbar, text_font_family = font.family_default)
     
else if bndLowReached and show_vix_downArrow and not show_data_inToolTip
    label.new(bar_index, na, color = AvgLow_VixLow_color, style=label.style_label_up,text=customFonts.uni(str_vix_low_downArrow, FontSelected),
     textcolor = color.white, textalign = text.align_center, size = labelSize, yloc = yloc.belowbar, text_font_family = font.family_default)


// Variable for Alerts was an brain storming exercise how to implement frequence all option in Alerts. Enjoy 😄
varip close_above_PvixdbnHigh = bool(na)
varip close_below_PvixdbnHigh = bool(na)

varip close_below_PvixdbnLow = bool(na)
varip close_above_PvixdbnLow = bool(na)

varip a_hightarget = false
varip a_lowtarget = false

close_above_PvixdbnHigh := close > math.round(PvixdbnHigh) // CMP greater then calculated Vix High
// Once the price goes above Previous Day calculated High we set an condition true or false

close_below_PvixdbnHigh := close < math.round(PvixdbnHigh) // CMP going below calculated Vix High
// Once the price goes below Previous Day calculated High we set an condition true or false

// Similarly below when price goes below Previous Day Calculated Low we set and condition and when the price goes above the Low we set an condition.
close_below_PvixdbnLow := close < math.round(PvixdbnLow) // 
close_above_PvixdbnLow := close > math.round(PvixdbnLow) // 

// ❗️ Note : Alerts for Target Reached (High or Low) : Alerts are with frequence all means as many times the prices goes up and down you'll get the alerts continously.
//    at times it will be disable automatically so you need to reset it manually.

if a_hightarget and  close_above_PvixdbnHigh 
    a_hightarget := false
    alert(syminfo.tickerid + " ▲ CMP is Going UP than VIX HIGH : " + str.format("{0, number, #.##}", PvixdbnHigh) +  " ▲ " +
     " TF  = (" + timeframe.period + ") " + str.format_time(timenow, "dd-MM-yyyy HH:mm:ss", syminfo.timezone), alert.freq_all)
    
if a_hightarget == false and close_below_PvixdbnHigh
    a_hightarget := true
    alert(syminfo.tickerid + " ▼ CMP is going Down below VIX HIGH : " + str.format("{0, number, #.##}", PvixdbnHigh) +  " ▼ " + 
     " TF  = (" + timeframe.period + ") " + str.format_time(timenow, "dd-MM-yyyy HH:mm:ss", syminfo.timezone), alert.freq_all)
    
// Alerts for Low Target Reached
if a_lowtarget and  close_below_PvixdbnLow 
    a_lowtarget := false
    alert(syminfo.tickerid + " ▼ CMP is Going down below VIX LOW : " + str.format("{0, number, #.##}", PvixdbnLow) +  " ▼ " + 
     " TF  = (" + timeframe.period + ") " + str.format_time(timenow, "dd-MM-yyyy HH:mm:ss", syminfo.timezone), alert.freq_all)
    
if a_lowtarget == false and close_above_PvixdbnLow
    a_lowtarget := true
    alert(syminfo.tickerid + " ▲ CMP is going UP above VIX LOW : " + str.format("{0, number, #.##}", PvixdbnLow) +  " ▲ " + 
     " TF  = (" + timeframe.period + ") " + str.format_time(timenow, "dd-MM-yyyy HH:mm:ss", syminfo.timezone), alert.freq_all)
