VIX - SKEW Divergence
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © valpatrad

//@version=5
indicator('VIX - SKEW Divergence')


gaps = input.bool(false, 'Gaps', tooltip='If not checked, gaps are filled with the latest available value.')



////////////////////////////
//                        //
//          Menu          //
//                        //
////////////////////////////

	//Lines
show_vix = input.bool(true,'VIX                 ', group='Lines', inline='VIX')
col_vix = input.color(#FF0000, '', group='Lines', inline='VIX')
w_vix = input.int(1, '', 1, 4, group='Lines', inline='VIX', tooltip='The numeric value refers to the width of the line.')
show_skew = input.bool(true, 'SKEW              ', group='Lines', inline='SKEW')
col_skew = input.color(#00FF00, '', group='Lines', inline='SKEW')
w_skew = input.int(1, '', 1, 4, group='Lines', inline='SKEW')
show_div = input.bool(true, 'Divergence      ', inline='Divergence', group='Lines')
col_div = input.color(#B2B5BE, '', inline='Divergence', group='Lines')
w_div = input.int(1, '', 1, 4, inline='Divergence', group='Lines')
show_avg_div = input.bool(true, 'Avg Divergence', inline='Avg Divergence', group='Lines')
col_avg_div = input.color(#0000FF, '', inline='Avg Divergence', group='Lines')
w_avg_div = input.int(1, '', 1, 4, inline='Avg Divergence', group='Lines')

	//VIX - SKEW Divergence Area
show_vix_skew_area = input.bool(true, 'VIX - SKEW Area', group='VIX - SKEW Divergence Area', tooltip='Fill the area between the VIX and the SKEW.')
data_min = input.int(40, 'Lowest Color ', 0, group='VIX - SKEW Divergence Area', inline='Low')
data_max = input.int(170, 'Highest Color', 0, group='VIX - SKEW Divergence Area', inline='High')
col_min = input.color(color.new(#00FF00, 50), '', group='VIX - SKEW Divergence Area', inline='Low')
col_max = input.color(color.new(#FF0000, 50), '', group='VIX - SKEW Divergence Area', inline='High')

	//VIX Area
show_vix_area = input.bool(false, 'VIX Area', group='VIX Area', tooltip='Fill the VIX area.')
col_lvl_1 = input.color(color.new(#00FF00, 60), 'Level 1', group='VIX Area', inline='Level 1', tooltip='These numbers represent the thresholds and each one must be higher than the previous.')
col_lvl_2 = input.color(color.new(#FFFA00, 60), 'Level 2', group='VIX Area', inline='Level 2')
col_lvl_3 = input.color(color.new(#FF9600, 60), 'Level 3', group='VIX Area', inline='Level 3')
col_lvl_4 = input.color(color.new(#FF0000, 60), 'Level 4', group='VIX Area', inline='Level 4')
lvl_1 = input.int(12, '', 0, group='VIX Area', inline='Level 1')
lvl_2 = input.int(20, '', 0, group='VIX Area', inline='Level 2')
lvl_3 = input.int(37, '', 0, group='VIX Area', inline='Level 3')

	//Average Divergence
show_div_avg = input.bool(true, 'Avg Divergence Area', group='Average Divergence', inline='Area')
col_div_avg = input.color(color.new(#787B86, 50), '', group='Average Divergence', inline='Area', tooltip='Fill the area between the divergence and the average divergence.')
avg_type = input.string('Median', 'Avg Type', ['Median', 'MA', 'EMA', 'WMA'], group='Average Divergence', inline='Avg')
avg_len = input.int(30, 'Length', 1, group='Average Divergence', inline='Avg')



/////////////////////////////
//                         //
//        Variables        //
//                         //
/////////////////////////////

vix = ticker.new('CBOE', 'VIX', session.extended)
skew = ticker.new('CBOE', 'SKEW', session.extended)
data_vix = request.security(vix, '', close, gaps ? barmerge.gaps_on : barmerge.gaps_off)
data_skew = request.security(skew, '', close, gaps ? barmerge.gaps_on : barmerge.gaps_off)
div = data_skew-data_vix
avg(p) =>
	if avg_type == 'Median'
		ta.median(div, p)
	else if avg_type == 'MA'
		ta.sma(div, p)
	else if avg_type == 'EMA'
		ta.ema(div, p)
	else if avg_type == 'WMA'
		ta.wma(div, p)



////////////////////////////
//                        //
//          Plot          //
//                        //
////////////////////////////

p0 = plot(show_vix_area ? 0 : na, editable=false, display=display.none)
p1 = plot(data_vix, '', col_vix, w_vix, plot.style_linebr, editable=false, display=display.none)
p2 = plot(data_skew, '', col_skew, w_skew, plot.style_linebr, editable=false, display=display.none)
p3 = plot(div, '', col_div, w_div, plot.style_linebr, editable=false, display=display.none)
p4 = plot(avg(avg_len), '', col_avg_div, w_avg_div, plot.style_linebr, editable=false, display=display.none)
fill(p1, p2, show_vix_skew_area and data_max > data_min ? color.from_gradient(data_skew - data_vix, data_min, data_max, col_min, col_max) : na)
fill(p0, p1, data_vix > lvl_3 ? col_lvl_4 : data_vix > lvl_2 ? col_lvl_3 : data_vix > lvl_1 ? col_lvl_2 : col_lvl_1)
fill(p3, p4, show_div_avg ? col_div_avg : na)
plot(show_vix ? data_vix : na, '', col_vix, w_vix, plot.style_linebr, editable=false)
plot(show_skew ? data_skew : na, '', col_skew, w_skew, plot.style_linebr, editable=false)
plot(show_div ? div : na, '', col_div, w_div, plot.style_linebr, editable=false)
plot(show_avg_div ? avg(avg_len) : na, '', col_avg_div, w_avg_div, plot.style_linebr, editable=false)



///////////////////////////
//                       //
//         Table         //
//                       //
///////////////////////////

	//Menu
tab_vix = input.bool(true, 'VIX                 ', group='Table', inline='VIX')
tab_vix_txt = input.color(#FFFFFF, '', group='Table', inline='VIX', tooltip='The color refers to the text.')
tab_skew = input.bool(true, 'SKEW              ', group='Table', inline='SKEW')
tab_skew_txt = input.color(#FFFFFF, '', group='Table', inline='SKEW')
tab_div = input.bool(true, 'Divergence      ', group='Table', inline='Divergence')
tab_div_txt = input.color(#FFFFFF, '', group='Table', inline='Divergence')
tab_avg_div = input.bool(true, 'Avg Divergence', group='Table', inline='Avg Divergence')
tab_avg_div_txt = input.color(#FFFFFF, '', group='Table', inline='Avg Divergence')
bkgd_tr_1 = input.int(50, 'BKGD', 0, 100, group='Table', inline='Transparency 1')
txt_tr_1 = input.int(30, 'Text', 0, 100, group='Table', inline='Transparency 1', tooltip='Transparency settings for the first column.')
bkgd_tr_2 = input.int(70, 'BKGD', 0, 100, group='Table', inline='Transparency 2')
txt_tr_2 = input.int(0, 'Text', 0, 100, group='Table', inline='Transparency 2', tooltip='Transparency settings for the second column.')
width_bord = input.int(1, 'Border Width', 0, group='Table')
in_font = input.string('Default', 'Text Font' , ['Default', 'Monospace'], group='Table')
font = switch in_font
	'Default' => font.family_default
	'Monospace' => font.family_monospace
input_tab_size = input.string('Auto', 'Size', group='Table', options=['Auto', 'Tiny', 'Small', 'Normal', 'Large', 'Huge'])
tab_size = switch input_tab_size
    'Auto'   => size.auto
    'Tiny'   => size.tiny
    'Small'  => size.small
    'Normal' => size.normal
    'Large'  => size.large
    'Huge'   => size.huge
input_tab_pos = input.string('Bottom Right', 'Position', group='Table', options=['Top Left', 'Top Center', 'Top Right', 'Middle Left', 'Middle Center', 'Middle Right', 'Bottom Left', 'Bottom Center', 'Bottom Right'])
tab_pos = switch input_tab_pos
    '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

	//Variables
chg_vix = (data_vix-data_vix[1])/data_vix[1]*100
chg_skew = (data_skew-data_skew[1])/data_skew[1]*100
chg_div = (div-div[1])/div[1]*100
avg_div = div-avg(avg_len)

	//Plot
tab = table.new(tab_pos, 2, 4, border_width=width_bord)
_tab(a, r, b, txt, _txt_col, _bgcol) =>
	int row = 0
	float chg = 0
	string name = ''
	if a
		row := r
		chg := b
		name := txt
		table.cell(tab, 0, row, txt, text_color=color.new(_txt_col, txt_tr_1), text_size=tab_size, bgcolor=color.new(_bgcol, bkgd_tr_1), text_font_family=font)
		table.cell(tab, 1, row, name == 'Div-Avg' ? str.tostring(avg_div, '#.##') : chg > 0 ? '+' + str.tostring(chg, format.percent) : chg == 0 ? 'UNCH' : str.tostring(chg, format.percent), text_color=chg > 0 ? color.new(#4CAF50, txt_tr_2) : chg == 0 ? color.new(#FFFFFF, txt_tr_2) : color.new(#FF5252, txt_tr_2), text_size=tab_size, bgcolor=chg > 0 ? color.new(#4CAF504D, bkgd_tr_2) : chg == 0 ? color.new(#F1F1F1, bkgd_tr_2) : color.new(#FF52524D, bkgd_tr_2), text_font_family=font)
if not timeframe.isintraday
	_tab(tab_vix, 0, chg_vix, 'VIX', tab_vix_txt, col_vix)
	_tab(tab_skew, 1, chg_skew, 'SKEW', tab_skew_txt, col_skew)
	_tab(tab_div, 2, chg_div, 'Div', tab_div_txt, col_div)
	_tab(tab_avg_div, 3, avg_div, 'Div-Avg', tab_avg_div_txt, col_avg_div)