Kimchi Premium watch
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Quu
//@version=5
indicator('Kimchi Premium watch', scale=scale.none, overlay = true)

dollar = input.symbol(title='Dollar/KRW', defval='USDKRW')
dollar_value = request.security(dollar, timeframe.period, close)

btcusdt = input.symbol(title='BTCUSDT', defval='BINANCE:BTCUSDT')
btcusdt_value = request.security(btcusdt, timeframe.period, close)

usdtusd = input.symbol(title='USDTUSD', defval='BITSTAMP:USDTUSD')
usdt_usd_value = request.security(usdtusd, timeframe.period, close)

btckrw = input.symbol(title='BTCKRW', defval='UPBIT:BTCKRW')
btckrw_value = request.security(btckrw, timeframe.period, close)

converted = btcusdt_value * usdt_usd_value * dollar_value

premium_btc = (btckrw_value - converted) / converted * 100

symbol = syminfo.ticker

cleaned_symbol = symbol
ends_with_perp = str.endswith(symbol, ".P")
if ends_with_perp
    cleaned_symbol := str.replace(symbol, ".P", "")

ends_with_busd = str.endswith(cleaned_symbol, "BUSD")
ends_with_usd = str.endswith(cleaned_symbol, "USD")
ends_with_usdt = str.endswith(cleaned_symbol, "USDT")
ends_with_krw = str.endswith(cleaned_symbol, "KRW")

tic = "unknown"
tic_text = "Not supported"
tic_size = size.large
if ends_with_busd
    tic := str.replace(cleaned_symbol, "BUSD", "")
else if ends_with_usd
    tic := str.replace(cleaned_symbol, "USD", "")
else if ends_with_usdt
    tic := str.replace(cleaned_symbol, "USDT", "")
else if ends_with_krw
    tic := str.replace(cleaned_symbol, "KRW", "")

binance_busd = request.security("BINANCE:"+tic+"BUSD", timeframe.period, close, ignore_invalid_symbol = true)
binance_usdt = request.security("BINANCE:"+tic+"USDT", timeframe.period, close, ignore_invalid_symbol = true)
coinbase_usd = request.security("COINBASE:"+tic+"USD", timeframe.period, close, ignore_invalid_symbol = true)
upbit_krw = request.security("UPBIT:"+tic+"KRW", timeframe.period, close, ignore_invalid_symbol = true)

curr_krw_data = request.security(tic+"KRW", timeframe.period, close, ignore_invalid_symbol = true)
curr_usd_data = request.security(tic+"USD", timeframe.period, close, ignore_invalid_symbol = true)
curr_usdt_data = request.security(tic+"USDT", timeframe.period, close, ignore_invalid_symbol = true)
curr_busd_data = request.security(tic+"BUSD", timeframe.period, close, ignore_invalid_symbol = true)

premium_curr = 0.1
not_supported = false
if ends_with_busd
    busd_data = close
    if not na(upbit_krw)
        curr_converted = busd_data * dollar_value
        premium_curr := (upbit_krw - curr_converted) / curr_converted * 100
    else if not na(curr_krw_data)
        curr_converted = busd_data * dollar_value
        premium_curr := (curr_krw_data - curr_converted) / curr_converted * 100
    else
        not_supported := true

else if ends_with_usd
    usd_data = close
    if not na(upbit_krw)
        curr_converted = usd_data * dollar_value
        premium_curr := (upbit_krw - curr_converted) / curr_converted * 100
    else if not na(curr_krw_data)
        curr_converted = usd_data * dollar_value
        premium_curr := (curr_krw_data - curr_converted) / curr_converted * 100
    else
        not_supported := true

else if ends_with_usdt
    usdt_data = close
    if not na(upbit_krw)
        curr_converted = usdt_data * usdt_usd_value * dollar_value
        premium_curr := (upbit_krw - curr_converted) / curr_converted * 100
    else if not na(curr_krw_data)
        curr_converted = usdt_data * usdt_usd_value * dollar_value
        premium_curr := (curr_krw_data - curr_converted) / curr_converted * 100
    else
        not_supported := true

else if ends_with_krw
    krw_data = close
    if not na(binance_usdt)
        curr_converted = binance_usdt * usdt_usd_value * dollar_value
        premium_curr := (krw_data - curr_converted) / curr_converted * 100
    else if not na(curr_usdt_data)
        curr_converted = curr_usdt_data * usdt_usd_value * dollar_value
        premium_curr := (krw_data - curr_converted) / curr_converted * 100
    else
        not_supported := true

else
    not_supported := true

if not_supported
    tic_text := "Not supported"
    tic_size := size.normal
else
    tic_text := str.tostring(math.round(premium_curr * 100) / 100) + " %"
    tic_size := size.large

var premium_table = table.new(position = position.top_right, columns = 2, rows = 4, bgcolor = color.rgb(95,95,95), border_width = 1, frame_color = color.yellow, border_color = color.yellow, frame_width = 1)
table.merge_cells(premium_table, 0, 0, 1, 0)
if tic == 'BTC'
    table.merge_cells(premium_table, 0, 1, 0, 2)
    table.merge_cells(premium_table, 1, 1, 1, 2)
table.cell(table_id = premium_table, column = 0, row = 0, text = "Kimchi premium", text_color = color.white, bgcolor = color.black)
table.cell(table_id = premium_table, column = 0, row = 1, text = "BTC", text_color = color.white)
table.cell(table_id = premium_table, column = 0, row = 2, text = tic, text_color = color.white)

table.cell(table_id = premium_table, column = 1, row = 1, text = str.tostring(math.round(premium_btc * 100) / 100) + " %", text_color = color.white, text_size = size.large, text_halign = text.align_right)
table.cell(table_id = premium_table, column = 1, row = 2, text = str.tostring(tic_text), text_color = color.white, text_size = tic_size, text_halign = text.align_right)