ATR and IV Volatility Table
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © exlux99

//@version=5
indicator("ATR and IV Volatility Table", overlay=true)
plot_atr = input.bool(true, title="Plot Bottom and Top Levels from ATR", group='Plotting')
plot_iv= input.bool(false, title="Plot Bottom and Top Levels from IV", group='Plotting')

length = input.int(title="Length ATR", defval=14, minval=1)
smoothing = "RMA"
input_iv=input.float(24.0, title="IV Value")
true_iv = input_iv / math.sqrt(252)

ma_function(source, length) =>
	switch smoothing
		"RMA" => ta.rma(source, length)
		"SMA" => ta.sma(source, length)
		"EMA" => ta.ema(source, length)
		=> ta.wma(source, length)
//plot(ma_function(ta.tr(true), length), title = "ATR", color=color.new(#B71C1C, 0))
daily_atr=request.security(syminfo.tickerid, "D", ta.atr(length))
daily_close=request.security(syminfo.tickerid, "D", close)

var float gica = 0.0
gica:= daily_atr//ma_function(ta.tr(true),length)



////////////////////////////////////////////////////////////////////////////
posInput = input.string(title='Position', defval='Middle Right', options=['Bottom Left', 'Bottom Right', 'Top Left', 'Top Right', 'Middle Right'])
var pos = posInput == 'Bottom Left' ? position.bottom_left : posInput == 'Bottom Right' ? position.bottom_right : posInput == 'Top Left' ? position.top_left : posInput == 'Top Right' ? position.top_right : posInput == 'Middle Right' ? position.middle_right: na


var table table_atr = table.new(pos, 8, 8, border_width=3)


table_fillCell(_table, _column, _row, _value, _timeframe, _c_color) =>
    _transp = 70  
    _cellText = str.tostring(_value, '#.###')
    table.cell(_table, _column, _row, _cellText, bgcolor=color.new(_c_color, _transp), text_color=_c_color, width=5)
    table.cell_set_text_size(table_atr, 0, 0, size.normal)
    table.cell_set_text_size(table_atr, 0, 1, size.normal)
    table.cell_set_text_size(table_atr, 0, 2, size.normal)
    table.cell_set_text_size(table_atr, 0, 3, size.normal)
    table.cell_set_text_size(table_atr, 1, 0, size.normal)
    table.cell_set_text_size(table_atr, 1, 1, size.normal)
    table.cell_set_text_size(table_atr, 1, 2, size.normal)
    table.cell_set_text_size(table_atr, 1, 3, size.normal)
    table.cell_set_text_size(table_atr, 2, 0, size.normal)
    table.cell_set_text_size(table_atr, 2, 1, size.normal)
    table.cell_set_text_size(table_atr, 2, 2, size.normal)
    table.cell_set_text_size(table_atr, 2, 3, size.normal)
    //table.cell_set_text_size(table_atr, 0, 2, text)


if barstate.islast

    table.cell(table_atr, 0, 0, 'Daily ATR Value', text_color=color.white, text_size=size.normal, bgcolor=color.purple)
    table_fillCell(table_atr, 0, 1, gica, 'Text', color.white)
    table.cell(table_atr, 0, 2, 'Daily IV Value %', text_color=color.white, text_size=size.normal, bgcolor=color.blue)
    table_fillCell(table_atr, 0, 3, true_iv, 'Text', color.white)




    table.cell(table_atr, 1, 0, 'CLOSE + ATR Value', text_color=color.white, text_size=size.normal, bgcolor=color.purple)
    table_fillCell(table_atr, 1, 1, gica+close, 'Text', color.white)
    table.cell(table_atr, 1, 2, 'CLOSE + IV Value', text_color=color.white, text_size=size.normal, bgcolor=color.blue)
    table_fillCell(table_atr, 1, 3, close*(true_iv/100)+close, 'Text', color.white)



     
    table.cell(table_atr, 2, 0, 'CLOSE - ATR Value', text_color=color.white, text_size=size.normal, bgcolor=color.purple)
    table_fillCell(table_atr, 2, 1, close-gica, 'Text', color.white)
    table.cell(table_atr, 2, 2, 'CLOSE - IV Value', text_color=color.white, text_size=size.normal, bgcolor=color.blue)
    table_fillCell(table_atr, 2, 3, close-close*(true_iv/100), 'Text', color.white)
    
plot(plot_atr? gica+daily_close : na, title='Daily TOP ATR')
plot(plot_atr ? daily_close-gica :na, title='Daily BOT ATR')

plot(plot_iv? daily_close*(true_iv/100)+daily_close : na, title='Daily TOP IV')
plot( plot_iv? daily_close-daily_close*(true_iv/100) : na, title='Daily BOT IV')