Artharjan INDIA VIX v/s Nifty Volatility Dashboard
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rsdesai005

//@version=5
indicator(title= "Artharjan INDIA VIX v/s Nifty Volatility Dashboard", shorttitle = 'AVIX', overlay=true)

comparativeTickerId = input.symbol(defval="NSE:INDIAVIX", title="Comparative Symbol")
NiftyTickerId = input.symbol(defval="NSE:NIFTY", title="NIFTY 50 Benchmark Index")

// The following built-in color variables can be used to avoid hexadecimal color literals: color.black, color.silver, 
//color.gray, color.white, color.maroon, color.red, color.purple, color.fuchsia, color.green, color.lime, color.olive, color.yellow, color.navy, color.blue, color.teal, color.aqua, color.orange.
TableLocation = input.string("top_right", title='Select Table Position',options=["top_left","top_center","top_right","middle_left","middle_center","middle_right","bottom_left","bottom_center","bottom_right"] , tooltip="Default location is at Centre Top")

TableTextSize = input.string("Auto", title='Select Table Text Size',options=["Auto","Huge","Large","Normal","Small","Tiny"] , tooltip="Default Text Size is Auto")

thickness = 2
TableWidth = 0
pos1 = str.pos(comparativeTickerId, ":")
pos2 = str.pos(NiftyTickerId, ":")


AnnualVol = request.security(comparativeTickerId, "D", close)
AnnualVolPrev = request.security(comparativeTickerId, "D", close[1])

NiftyCurrentValue = request.security(NiftyTickerId, "D", close)
NiftyPrevValue = request.security(NiftyTickerId, "D", close[1])

VixBGColor = AnnualVol > AnnualVolPrev? #06c806 : #ff0000
VixFontColor = AnnualVol > AnnualVolPrev? color.black : color.white

NiftyBGColor = NiftyCurrentValue > NiftyPrevValue? #06c806 : #ff0000
NiftyFontColor = NiftyCurrentValue > NiftyPrevValue? color.black : color.white

QuarterlyVol = AnnualVol / math.sqrt(4)
MonthlyVol = AnnualVol / math.sqrt(12)
WeeklyVol = AnnualVol / math.sqrt(52)
DailyVol = AnnualVol / math.sqrt(252)
HourlyVol = AnnualVol / math.sqrt(252 * 375/60)


NiftyYMax = NiftyCurrentValue * ( 1 + AnnualVol/100)
NiftyYMin = NiftyCurrentValue * ( 1 - AnnualVol/100)
NiftyQMax = NiftyCurrentValue * ( 1 + QuarterlyVol/100)
NiftyQMin = NiftyCurrentValue * ( 1 - QuarterlyVol/100)
NiftyMMax = NiftyCurrentValue * ( 1 + MonthlyVol/100)
NiftyMMin = NiftyCurrentValue * ( 1 - MonthlyVol/100)
NiftyWMax = NiftyCurrentValue * ( 1 + WeeklyVol/100)
NiftyWMin = NiftyCurrentValue * ( 1 - WeeklyVol/100)
NiftyDMax = NiftyCurrentValue * ( 1 + DailyVol/100)
NiftyDMin = NiftyCurrentValue * ( 1 - DailyVol/100)
NiftyHMax = NiftyCurrentValue * ( 1 + HourlyVol/100)
NiftyHMin = NiftyCurrentValue * ( 1 - HourlyVol/100)

AssignedPosition = switch TableLocation
    "top_left" => position.top_left
    "top_center" => position.top_center
    "top_right" => position.top_right
    "middle_left" => position.middle_left
    "middle_center" => position.middle_center
    "middle_right" => position.middle_right
    "bottom_left" => position.bottom_left
    "bottom_center" => position.bottom_center
    "bottom_right" => position.bottom_right

CellTextSize = switch TableTextSize
    "Auto" => size.auto
    "Huge" => size.huge
    "Large" => size.large
    "Normal" => size.normal
    "Small" => size.small
    "Tiny" => size.tiny
    
//var table t = table.new(position.bottom_center, 4, 8, border_width=thickness)
var table t = table.new(AssignedPosition, 4, 9, frame_width=thickness, border_width=thickness,frame_color=color.navy, border_color=color.white)


if barstate.islast
    table.cell(t, 0, 0, 'Reference Symbol', width=TableWidth, text_color=color.white, bgcolor=#340370, text_size=CellTextSize)
    table.cell(t, 0, 1, str.substring(comparativeTickerId,pos1+1), width=TableWidth, text_color=color.white, bgcolor=#388e3c, text_size=CellTextSize)
    table.cell(t, 0, 2, 'Forecast Period', width=TableWidth, text_color=color.white, bgcolor=#340370, text_size=CellTextSize)
    table.cell(t, 0, 3, 'Yearly', width=TableWidth, text_color=color.white, bgcolor=#388e3c, text_size=CellTextSize)
    table.cell(t, 0, 4, 'Quarterly', width=TableWidth, text_color=color.white, bgcolor=#388e3c, text_size=CellTextSize)
    table.cell(t, 0, 5, 'Monthly', width=TableWidth, text_color=color.white, bgcolor=#388e3c, text_size=CellTextSize)
    table.cell(t, 0, 6, 'Weekly', width=TableWidth, text_color=color.white, bgcolor=#388e3c, text_size=CellTextSize)
    table.cell(t, 0, 7, 'Daily', width=TableWidth, text_color=color.white, bgcolor=#388e3c, text_size=CellTextSize)
    table.cell(t, 0, 8, 'Hourly', width=TableWidth, text_color=color.white, bgcolor=#388e3c, text_size=CellTextSize)

    table.cell(t, 1, 0, 'Current Value', width=TableWidth, text_color=color.white, bgcolor=#340370, text_size=CellTextSize)
    table.cell(t, 1, 1, str.tostring(math.round(AnnualVol,2)) + " %", width=TableWidth, text_color=VixFontColor, bgcolor=VixBGColor, text_size=CellTextSize)
    table.cell(t, 1, 2, 'Forecast Volatility', width=TableWidth, text_color=color.white, bgcolor=#340370, text_size=CellTextSize)
    table.cell(t, 1, 3, "+/- " + str.tostring(math.round(AnnualVol, 2)) + " %", width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
    table.cell(t, 1, 4, "+/- " + str.tostring(math.round(QuarterlyVol, 2)) + " %", width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
    table.cell(t, 1, 5, "+/- " + str.tostring(math.round(MonthlyVol, 2)) + " %", width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
    table.cell(t, 1, 6, "+/- " + str.tostring(math.round(WeeklyVol, 2)) + " %", width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
    table.cell(t, 1, 7, "+/- " + str.tostring(math.round(DailyVol, 2)) + " %", width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
    table.cell(t, 1, 8, "+/- " + str.tostring(math.round(HourlyVol, 2)) + " %", width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
  
    table.cell(t, 2, 0, 'Benchmark Index', width=TableWidth, text_color=color.white, bgcolor=#340370, text_size=CellTextSize)
    table.cell(t, 2, 1, str.substring(NiftyTickerId,pos2+1), width=TableWidth, text_color=color.white, bgcolor=#388e3c, text_size=CellTextSize)
    table.cell(t, 2, 2, 'Min Range', width=TableWidth, text_color=color.white, bgcolor=#340370, text_size=CellTextSize)
    table.cell(t, 2, 3, str.tostring(math.round(NiftyYMin)), width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
    table.cell(t, 2, 4, str.tostring(math.round(NiftyQMin)), width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
    table.cell(t, 2, 5, str.tostring(math.round(NiftyMMin)), width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
    table.cell(t, 2, 6, str.tostring(math.round(NiftyWMin)), width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
    table.cell(t, 2, 7, str.tostring(math.round(NiftyDMin)), width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
    table.cell(t, 2, 8, str.tostring(math.round(NiftyHMin)), width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
  
    table.cell(t, 3, 0, 'Current Value', width=TableWidth, text_color=color.white, bgcolor=#340370, text_size=CellTextSize)
    table.cell(t, 3, 1, str.tostring(math.round(NiftyCurrentValue)), width=TableWidth, text_color=NiftyFontColor, bgcolor=NiftyBGColor, text_size=CellTextSize)
    table.cell(t, 3, 2, 'Max Range', width=TableWidth, text_color=color.white, bgcolor=#340370, text_size=CellTextSize)
    table.cell(t, 3, 3, str.tostring(math.round(NiftyYMax)), width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
    table.cell(t, 3, 4, str.tostring(math.round(NiftyQMax)), width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
    table.cell(t, 3, 5, str.tostring(math.round(NiftyMMax)), width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
    table.cell(t, 3, 6, str.tostring(math.round(NiftyWMax)), width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
    table.cell(t, 3, 7, str.tostring(math.round(NiftyDMax)), width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
    table.cell(t, 3, 8, str.tostring(math.round(NiftyHMax)), width=TableWidth, text_color=color.navy, bgcolor=#bbd9fb, text_size=CellTextSize)
  