Volatility Inverse Correlation Candle
// 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("Volatility Inverse Correlation Candle", overlay=true)

fromDay = input.int(defval=1, title='From Day', minval=1, maxval=31, group='Time Condition')
fromMonth = input.int(defval=1, title='From Month', minval=1, maxval=12, group='Time Condition')
fromYear = input.int(defval=2000, title='From Year', minval=1970, group='Time Condition')
//monday and session 
// To Date Inputs
toDay = input.int(defval=31, title='To Day', minval=1, maxval=31, group='Time Condition')
toMonth = input.int(defval=12, title='To Month', minval=1, maxval=12, group='Time Condition')
toYear = input.int(defval=2029, title='To Year', minval=1970, group='Time Condition')

startDate = timestamp(fromYear, fromMonth, fromDay, 00, 00)
finishDate = timestamp(toYear, toMonth, toDay, 00, 00)
time_cond = time >= startDate and time <= finishDate

// plot(close)


input_symbol = input.symbol("TVC:VIX")
ha = input.bool(true,title="Allow HA Mode Calulation")

security_candle_open = ha ? request.security(ticker.heikinashi(input_symbol), timeframe.period, open) : request.security((input_symbol), timeframe.period, open) 
security_candle_close = ha ? request.security(ticker.heikinashi(input_symbol), timeframe.period, close) : request.security((input_symbol), timeframe.period, close) 

chart_candle_open = ha ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, open) : request.security((syminfo.tickerid), timeframe.period, open)
chart_candle_close = ha ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close) : request.security((syminfo.tickerid), timeframe.period, close)

var int total_positive = 0
var int total_negative = 0
var int count_positive = 0
var int count_negative = 0


if(time_cond)
    //long vix => short spx
    if(security_candle_open > security_candle_close[1] and chart_candle_open <chart_candle_close  )
        count_positive:= count_positive +1
    if(security_candle_open > security_candle_close[1] )
        total_positive := total_positive+1

        //short vix => long spx
    if(security_candle_open < security_candle_close[1] and chart_candle_open > chart_candle_close  )
        count_negative:= count_negative +1
    if(security_candle_open < security_candle_close[1]  )
        total_negative:=total_negative+1


//with moving average
// ma_out = ta.sma(open, input(2))
// plot(ma_out, color=color.white, linewidth=4)

// plot_short_spx = (security_candle_open > security_candle_close and open >close and open < ma_out)
// plotshape(plot_short_spx, style=shape.xcross, color=color.red, location=location.abovebar)

// plot_long_spx = (security_candle_open < security_candle_close and open < close and open > ma_out)
// plotshape(plot_long_spx, style=shape.xcross, color=color.green, location=location.belowbar)


// //long vix => short spx
// if(security_candle_open > security_candle_close and open >close and open < ma_out)
//     count_positive:= count_positive +1
// if(security_candle_open > security_candle_close and open < ma_out)
//     total_positive := total_positive+1

// //short vix => long spx
// if(security_candle_open < security_candle_close and open < close and open > ma_out)
//     count_negative:= count_negative +1
// if(security_candle_open < security_candle_close and open > ma_out)
//     total_negative:=total_negative+1




////////////////////////////////////////////////////////////////////////////
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, 15, 15, border_width=1)


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, 2, text)


if barstate.islast


    table.cell(table_atr, 0, 0, 'Total Candles VIX Long', text_color=color.white, text_size=size.normal, bgcolor=color.purple)
    table_fillCell(table_atr, 0, 1, total_positive, 'Text', color.white)
    table.cell(table_atr, 1, 0, 'Total Correlation VIX LONG SPX SHORT', text_color=color.white, text_size=size.normal, bgcolor=color.purple)
    table_fillCell(table_atr, 1, 1, count_positive, 'Text', color.white)
    table.cell(table_atr, 2, 0, '% Correlation VIX LONG SPX SHORT', text_color=color.white, text_size=size.normal, bgcolor=color.purple)
    table_fillCell(table_atr, 2, 1, count_positive/total_positive, 'Text', color.white)


    table.cell(table_atr, 0, 2, 'Total Candles VIX Short', text_color=color.white, text_size=size.normal, bgcolor=color.orange)
    table_fillCell(table_atr, 0, 3, total_negative, 'Text', color.white)
    table.cell(table_atr, 1, 2, 'Total Correlation VIX SHORT SPX LONG', text_color=color.white, text_size=size.normal, bgcolor=color.orange)
    table_fillCell(table_atr, 1, 3, count_negative, 'Text', color.white)
    table.cell(table_atr, 2, 2, '% Correlation VIX SHORT SPX LONG', text_color=color.white, text_size=size.normal, bgcolor=color.orange)
    table_fillCell(table_atr, 2, 3, count_negative/total_negative, 'Text', color.white)


