VOLQ Sigma Table
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Cube_Lee

//@version=5
indicator("VOLQ Sigma Table", overlay = true)

// Function
truncate(number, decimals) =>
    factor = math.pow(10, decimals)
    int(number * factor) / factor

// Input
showtable = input.bool(true, "Show Table", tooltip = "VOLQ를 반영한 가격을 테이블에 표시합니다.", group = "VOLQ Table")
trunc = input.int(0, "Decimal display", tooltip = "테이블에 나타나는 가격의 소수점 자리수를 선택하세요", group = "VOLQ Table")
showline = input.bool(true, "Show VOLQ Line", tooltip = "예상 VOLQ를 선으로 표시합니다.", group = "VOLQ Line")
showplot = input.bool(false, "Show historic VOLQ band", tooltip = "과거 VOLQ 밴드를 차트에 표시합니다.")
src = input.string("last week close", "price source", options = ["week close", "last week close"], tooltip = "VOLQ % 를 이번 주 종가로 할 것인지 지난 주 종가로 할 것인지 선택합니다.")

// Get Data
vol1 = request.security("VOLQ", "W", close)
vol2 = request.security("VOLQ", "W", close[1])

// close1 = truncate(request.security(syminfo.tickerid, "D", close), trunc)
// close2 = truncate(request.security(syminfo.tickerid, "D", close[1]), trunc)
// close3 = truncate(request.security(syminfo.tickerid, "W", close), trunc)
// close4 = truncate(request.security(syminfo.tickerid, "W", close[1]), trunc)

//━━━━━━━━━━━━━━━━━━━━━//
// 마지막 금요일 구하기 //
//━━━━━━━━━━━━━━━━━━━━//
var float last_fri_close = 0.0
var int last_friday = 0

if dayofweek == dayofweek.friday and time(timeframe.period, "1600-1630")
    last_fri_close := truncate(close, trunc)
    last_friday := time

// 마지막 금요일 구하기 끝 //

float volq = switch src
    "week close" => vol1
    "last week close" => vol2


// float sym_close = switch src
//     "day close" => close1
//     "last day close" => close2
//     "week close" => close3
//     "last week close" => close4

    
// 변동성 반영한 주가지수 산출
sigma2_high_1day = truncate(last_fri_close + (last_fri_close * volq / math.sqrt(250) * 2 / 100), trunc)
sigma2_high_2day = truncate(last_fri_close + (last_fri_close * volq / math.sqrt(125) * 2 / 100), trunc)
sigma2_high_3day = truncate(last_fri_close + (last_fri_close * volq / math.sqrt(83) * 2 / 100), trunc)
sigma2_high_4day = truncate(last_fri_close + (last_fri_close * volq / math.sqrt(62.5) * 2 / 100), trunc)
sigma2_high_5day = truncate(last_fri_close + (last_fri_close * volq / math.sqrt(50) * 2 / 100), trunc)

sigma1_high_1day = truncate(last_fri_close + (last_fri_close * volq / math.sqrt(250) / 100), trunc)
sigma1_high_2day = truncate(last_fri_close + (last_fri_close * volq / math.sqrt(125) / 100), trunc)
sigma1_high_3day = truncate(last_fri_close + (last_fri_close * volq / math.sqrt(83) / 100), trunc)
sigma1_high_4day = truncate(last_fri_close + (last_fri_close * volq / math.sqrt(62.5) / 100), trunc)
sigma1_high_5day = truncate(last_fri_close + (last_fri_close * volq / math.sqrt(50) / 100), trunc)

sigma1_low_1day = truncate(last_fri_close - (last_fri_close * volq / math.sqrt(250) / 100), trunc)
sigma1_low_2day = truncate(last_fri_close - (last_fri_close * volq / math.sqrt(125) / 100), trunc)
sigma1_low_3day = truncate(last_fri_close - (last_fri_close * volq / math.sqrt(83) / 100), trunc)
sigma1_low_4day = truncate(last_fri_close - (last_fri_close * volq / math.sqrt(62.5) / 100), trunc)
sigma1_low_5day = truncate(last_fri_close - (last_fri_close * volq / math.sqrt(50) / 100), trunc)

sigma2_low_1day = truncate(last_fri_close - (last_fri_close * volq / math.sqrt(250) * 2 / 100), trunc)
sigma2_low_2day = truncate(last_fri_close - (last_fri_close * volq / math.sqrt(125) * 2 / 100), trunc)
sigma2_low_3day = truncate(last_fri_close - (last_fri_close * volq / math.sqrt(83) * 2 / 100), trunc)
sigma2_low_4day = truncate(last_fri_close - (last_fri_close * volq / math.sqrt(62.5) * 2 / 100), trunc)
sigma2_low_5day = truncate(last_fri_close - (last_fri_close * volq / math.sqrt(50) * 2 / 100), trunc)

// 테이블 컬러
bg_color_volq = color.rgb(0, 0, 0)
bg_color_title = color.rgb(0, 0, 0)
bg_color_high2 = color.rgb(129, 199, 132)
bg_color_high1 = color.rgb(200, 230, 201)
bg_color_close = color.rgb(255, 255, 255)
bg_color_low1 = color.rgb(252, 203, 205)
bg_color_low2 = color.rgb(247, 124, 128)

text_color_volq = color.red
text_color_title = color.rgb(216, 216, 216)
text_color_high = color.rgb(0, 0, 0)
text_color_close = color.blue
text_color_low = color.rgb(0, 0, 0)

// Draw Table
string close_text = switch src
    "week close" => "주간 종가"
    "last week close" => "지난 주 종가"

_textsize = size.normal

if barstate.islastconfirmedhistory and showtable
    t = table.new(position = position.top_right, columns = 7, rows = 6, border_color = color.white, border_width = 2)
    table.cell(table_id = t, column = 6, row = 0, text = "blank", bgcolor = color.rgb(54, 58, 69, 100), text_color = color.rgb(54, 58, 69, 100), text_size = _textsize)
    table.merge_cells(table_id = t, start_column = 6, start_row = 0, end_column = 6, end_row = 5)
    table.cell(table_id = t, column = 0, row = 0, text = "VOLQ:" + "\n" + str.tostring(volq) + "%", bgcolor = bg_color_volq, text_color = text_color_volq, text_size = _textsize)
    table.cell(table_id = t, column = 1, row = 0, text = "1 day", bgcolor = bg_color_title, text_color = text_color_title, text_size = _textsize)
    table.cell(table_id = t, column = 2, row = 0, text = "2 day", bgcolor = bg_color_title, text_color = text_color_title, text_size = _textsize)
    table.cell(table_id = t, column = 3, row = 0, text = "3 day", bgcolor = bg_color_title, text_color = text_color_title, text_size = _textsize)
    table.cell(table_id = t, column = 4, row = 0, text = "4 day", bgcolor = bg_color_title, text_color = text_color_title, text_size = _textsize)
    table.cell(table_id = t, column = 5, row = 0, text = "5 day", bgcolor = bg_color_title, text_color = text_color_title, text_size = _textsize)
    table.cell(table_id = t, column = 0, row = 1, text = "High 2σ", bgcolor = bg_color_high2, text_color = text_color_high, text_size = _textsize)
    table.cell(table_id = t, column = 1, row = 1, text = str.tostring(sigma2_high_1day), bgcolor = bg_color_high2, text_color = text_color_high, text_size = _textsize)
    table.cell(table_id = t, column = 2, row = 1, text = str.tostring(sigma2_high_2day), bgcolor = bg_color_high2, text_color = text_color_high, text_size = _textsize)
    table.cell(table_id = t, column = 3, row = 1, text = str.tostring(sigma2_high_3day), bgcolor = bg_color_high2, text_color = text_color_high, text_size = _textsize)
    table.cell(table_id = t, column = 4, row = 1, text = str.tostring(sigma2_high_4day), bgcolor = bg_color_high2, text_color = text_color_high, text_size = _textsize)
    table.cell(table_id = t, column = 5, row = 1, text = str.tostring(sigma2_high_5day), bgcolor = bg_color_high2, text_color = text_color_high, text_size = _textsize)
    table.cell(table_id = t, column = 0, row = 2, text = "High 1σ", bgcolor = bg_color_high1, text_color = text_color_high, text_size = _textsize)
    table.cell(table_id = t, column = 1, row = 2, text = str.tostring(sigma1_high_1day), bgcolor = bg_color_high1, text_color = text_color_high, text_size = _textsize)
    table.cell(table_id = t, column = 2, row = 2, text = str.tostring(sigma1_high_2day), bgcolor = bg_color_high1, text_color = text_color_high, text_size = _textsize)
    table.cell(table_id = t, column = 3, row = 2, text = str.tostring(sigma1_high_3day), bgcolor = bg_color_high1, text_color = text_color_high, text_size = _textsize)
    table.cell(table_id = t, column = 4, row = 2, text = str.tostring(sigma1_high_4day), bgcolor = bg_color_high1, text_color = text_color_high, text_size = _textsize)
    table.cell(table_id = t, column = 5, row = 2, text = str.tostring(sigma1_high_5day), bgcolor = bg_color_high1, text_color = text_color_high, text_size = _textsize)
    table.cell(table_id = t, column = 0, row = 3, text = str.tostring(close_text), bgcolor = bg_color_close, text_color = text_color_close, text_size = _textsize)
    table.cell(table_id = t, column = 1, row = 3, text = str.tostring(last_fri_close), bgcolor = bg_color_close, text_color = text_color_close, text_size = _textsize)
    table.merge_cells(table_id = t, start_column = 1, start_row = 3, end_column = 5, end_row = 3)
    table.cell(table_id = t, column = 0, row = 4, text = "Low 1σ", bgcolor = bg_color_low1, text_color = text_color_low, text_size = _textsize)
    table.cell(table_id = t, column = 1, row = 4, text = str.tostring(sigma1_low_1day), bgcolor = bg_color_low1, text_color = text_color_low, text_size = _textsize)
    table.cell(table_id = t, column = 2, row = 4, text = str.tostring(sigma1_low_2day), bgcolor = bg_color_low1, text_color = text_color_low, text_size = _textsize)
    table.cell(table_id = t, column = 3, row = 4, text = str.tostring(sigma1_low_3day), bgcolor = bg_color_low1, text_color = text_color_low, text_size = _textsize)
    table.cell(table_id = t, column = 4, row = 4, text = str.tostring(sigma1_low_4day), bgcolor = bg_color_low1, text_color = text_color_low, text_size = _textsize)
    table.cell(table_id = t, column = 5, row = 4, text = str.tostring(sigma1_low_5day), bgcolor = bg_color_low1, text_color = text_color_low, text_size = _textsize)
    table.cell(table_id = t, column = 0, row = 5, text = "Low 2σ", bgcolor = bg_color_low2, text_color = text_color_low, text_size = _textsize)
    table.cell(table_id = t, column = 1, row = 5, text = str.tostring(sigma2_low_1day), bgcolor = bg_color_low2, text_color = text_color_low, text_size = _textsize)
    table.cell(table_id = t, column = 2, row = 5, text = str.tostring(sigma2_low_2day), bgcolor = bg_color_low2, text_color = text_color_low, text_size = _textsize)
    table.cell(table_id = t, column = 3, row = 5, text = str.tostring(sigma2_low_3day), bgcolor = bg_color_low2, text_color = text_color_low, text_size = _textsize)
    table.cell(table_id = t, column = 4, row = 5, text = str.tostring(sigma2_low_4day), bgcolor = bg_color_low2, text_color = text_color_low, text_size = _textsize)
    table.cell(table_id = t, column = 5, row = 5, text = str.tostring(sigma2_low_5day), bgcolor = bg_color_low2, text_color = text_color_low, text_size = _textsize)

// volq line location
// day1 = input.time(timestamp("2022-10-30"), title = "VOLQ Line 표시 시작 날짜", confirm = true, group = "VOLQ Line")
day1 = last_friday + 235800 * 1000
day2 = day1 + 86400 * 1000
day3 = day2 + 86400 * 1000
day4 = day3 + 86400 * 1000
day5 = day4 + 86400 * 1000
day6 = day5 + 86400 * 1000

// Draw VOLQ Line 
linecolor   = color.yellow
LblBR       = color.black
txtColor    = color.white
if barstate.islast and showline
    if timeframe.isintraday or timeframe.isdaily
        // baseline
        line_base = line.new(x1=day1, y1 = last_fri_close, x2 = day6, y2 = last_fri_close, xloc = xloc.bar_time, extend = extend.none, color = color.gray, style = line.style_dotted)

        // 1 day
        line_h2_day1 = line.new(x1=day1, y1=sigma2_high_1day, x2=day2, y2=sigma2_high_1day, xloc=xloc.bar_time, extend=extend.none, color=linecolor, width=2)
        line.delete(line_h2_day1[1])
        line_h1_day1 = line.new(x1=day1, y1=sigma1_high_1day, x2=day2, y2=sigma1_high_1day, xloc=xloc.bar_time, extend=extend.none, color=linecolor)
        line.delete(line_h1_day1[1])
        line_l1_day1 = line.new(x1=day1, y1=sigma1_low_1day, x2=day2, y2=sigma1_low_1day, xloc=xloc.bar_time, extend=extend.none, color=linecolor)
        line.delete(line_l1_day1[1])
        line_l2_day1 = line.new(x1=day1, y1=sigma2_low_1day, x2=day2, y2=sigma2_low_1day, xloc=xloc.bar_time, extend=extend.none, color=linecolor, width=2)
        line.delete(line_l2_day1[1])

        // 2 day
        line_h2_day2 = line.new(x1=day2, y1=sigma2_high_2day, x2=day3, y2=sigma2_high_2day, xloc=xloc.bar_time, extend=extend.none, color=linecolor, width=2)
        line.delete(line_h2_day2[1])
        line_h1_day2 = line.new(x1=day2, y1=sigma1_high_2day, x2=day3, y2=sigma1_high_2day, xloc=xloc.bar_time, extend=extend.none, color=linecolor)
        line.delete(line_h1_day2[1])
        line_l1_day2 = line.new(x1=day2, y1=sigma1_low_2day, x2=day3, y2=sigma1_low_2day, xloc=xloc.bar_time, extend=extend.none, color=linecolor)
        line.delete(line_l1_day2[1])
        line_l2_day2 = line.new(x1=day2, y1=sigma2_low_2day, x2=day3, y2=sigma2_low_2day, xloc=xloc.bar_time, extend=extend.none, color=linecolor, width=2)
        line.delete(line_l2_day2[1])

        // 3 day
        line_h2_day3 = line.new(x1=day3, y1=sigma2_high_3day, x2=day4, y2=sigma2_high_3day, xloc=xloc.bar_time, extend=extend.none, color=linecolor, width=2)
        line.delete(line_h2_day3[1])
        line_h1_day3 = line.new(x1=day3, y1=sigma1_high_3day, x2=day4, y2=sigma1_high_3day, xloc=xloc.bar_time, extend=extend.none, color=linecolor)
        line.delete(line_h1_day3[1])
        line_l1_day3 = line.new(x1=day3, y1=sigma1_low_3day, x2=day4, y2=sigma1_low_3day, xloc=xloc.bar_time, extend=extend.none, color=linecolor)
        line.delete(line_l1_day3[1])
        line_l2_day3 = line.new(x1=day3, y1=sigma2_low_3day, x2=day4, y2=sigma2_low_3day, xloc=xloc.bar_time, extend=extend.none, color=linecolor, width=2)
        line.delete(line_l2_day3[1])

        // 4 day
        line_h2_day4 = line.new(x1=day4, y1=sigma2_high_4day, x2=day5, y2=sigma2_high_4day, xloc=xloc.bar_time, extend=extend.none, color=linecolor, width=2)
        line.delete(line_h2_day4[1])
        line_h1_day4 = line.new(x1=day4, y1=sigma1_high_4day, x2=day5, y2=sigma1_high_4day, xloc=xloc.bar_time, extend=extend.none, color=linecolor)
        line.delete(line_h1_day4[1])
        line_l1_day4 = line.new(x1=day4, y1=sigma1_low_4day, x2=day5, y2=sigma1_low_4day, xloc=xloc.bar_time, extend=extend.none, color=linecolor)
        line.delete(line_l1_day4[1])
        line_l2_day4 = line.new(x1=day4, y1=sigma2_low_4day, x2=day5, y2=sigma2_low_4day, xloc=xloc.bar_time, extend=extend.none, color=linecolor, width=2)
        line.delete(line_l2_day4[1])

        // 5 day
        line_h2_day5 = line.new(x1=day5, y1=sigma2_high_5day, x2=day6, y2=sigma2_high_5day, xloc=xloc.bar_time, extend=extend.none, color=linecolor, width=2)
        line.delete(line_h2_day5[1])
        line_h1_day5 = line.new(x1=day5, y1=sigma1_high_5day, x2=day6, y2=sigma1_high_5day, xloc=xloc.bar_time, extend=extend.none, color=linecolor)
        line.delete(line_h1_day5[1])
        line_l1_day5 = line.new(x1=day5, y1=sigma1_low_5day, x2=day6, y2=sigma1_low_5day, xloc=xloc.bar_time, extend=extend.none, color=linecolor)
        line.delete(line_l1_day5[1])
        line_l2_day5 = line.new(x1=day5, y1=sigma2_low_5day, x2=day6, y2=sigma2_low_5day, xloc=xloc.bar_time, extend=extend.none, color=linecolor, width=2)
        line.delete(line_l2_day5[1])

        // label
        label_h2_day5 = label.new(x=day6, y=sigma2_high_5day, text='High 2σ', xloc=xloc.bar_time, color=LblBR, textcolor = txtColor, style=label.style_label_left)
        label.delete(label_h2_day5[1])
        label_h1_day5 = label.new(x=day6, y=sigma1_high_5day, text='High 1σ', xloc=xloc.bar_time, color=LblBR, textcolor = txtColor, style=label.style_label_left)
        label.delete(label_h1_day5[1])
        label_l1_day5 = label.new(x=day6, y=sigma1_low_5day, text='Low 1σ', xloc=xloc.bar_time, color=LblBR, textcolor = txtColor, style=label.style_label_left)
        label.delete(label_l1_day5[1])
        label_l2_day5 = label.new(x=day6, y=sigma2_low_5day, text='Low 2σ', xloc=xloc.bar_time, color=LblBR, textcolor = txtColor, style=label.style_label_left)
        label.delete(label_l2_day5[1])

// VOLQ band plot
plot(sigma2_high_1day, "High 2σ", style = plot.style_line, color = showplot ? color.rgb(0, 255, 0, 50) : color.rgb(33, 149, 243, 100))
plot(sigma1_high_1day, "High 1σ", style = plot.style_line, color = showplot ? color.rgb(0, 255, 0, 70) : color.rgb(33, 149, 243, 100))
plot(sigma1_low_1day, "Low 1σ", style = plot.style_line, color = showplot ? color.rgb(255, 0, 0, 70) : color.rgb(33, 149, 243, 100))
plot(sigma2_low_1day, "Low 2σ", style = plot.style_line, color = showplot ? color.rgb(255, 0, 0, 50) : color.rgb(33, 149, 243, 100))