Korea Premium
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © dokang
//@version=5
//  ________________________________________________
// |                                               |  
// |       ---------------------------------       | 
// |      | Developed by JangDoKang         |      | 
// |       ---------------------------------       |  
// |_______________________________________________|
indicator("Korea Premium", overlay = true)
usdkrw = request.security("FX_IDC:USDKRW", timeframe.period, close)
isShowPrice = input.bool(true, "Show Price")
isSimple = input.bool(false, "Simple Mode")
isKorean = input.bool(false, "한국어")
isFuture = input.bool(false, "Future")

title = isKorean ? isSimple ? "김프" : "김치프리미엄" : isSimple ? "PREMIUM" : "KOREA PREMIUM"
col0 = isKorean ? "코인" : "COIN"
col1 = isKorean ? "업비트" : "UPBIT"
col2 = isKorean ? "바이낸스" : "BINANCE"
col3 = isKorean ? "김프" : "PREMIUM"

binance_price_array = array.new<float>()
upbit_price_array = array.new<float>()
kimp_array = array.new<float>()

var base = syminfo.basecurrency

get_kimp(a, b) =>
    100*(a-b)/b
var id_array = array.from("BTC","ETH","XRP","ADA","DOGE","MATIC","SOL","TRX","DOT","SHIB")
var findIndex = array.indexof(id_array, base)

UPBIT_BTC_PRICE = request.security("UPBIT:BTCKRW", timeframe.period, close)
array.push(upbit_price_array, UPBIT_BTC_PRICE)
BINANCE_BTC_PRICE = request.security(isFuture ? "BINANCE:BTCUSDTPERP" : "BINANCE:BTCUSDT", timeframe.period, close) * usdkrw
array.push(binance_price_array, BINANCE_BTC_PRICE)
BTC_KIMP = get_kimp(UPBIT_BTC_PRICE, BINANCE_BTC_PRICE)
array.push(kimp_array, BTC_KIMP)

UPBIT_ETH_PRICE = request.security("UPBIT:ETHKRW", timeframe.period, close)
array.push(upbit_price_array, UPBIT_ETH_PRICE)
BINANCE_ETH_PRICE = request.security(isFuture ? "BINANCE:ETHUSDTPERP" : "BINANCE:ETHUSDT", timeframe.period, close) * usdkrw
array.push(binance_price_array, BINANCE_ETH_PRICE)
ETH_KIMP = get_kimp(UPBIT_ETH_PRICE, BINANCE_ETH_PRICE)
array.push(kimp_array, ETH_KIMP)

UPBIT_XRP_PRICE = request.security("UPBIT:XRPKRW", timeframe.period, close)
array.push(upbit_price_array, UPBIT_XRP_PRICE)
BINANCE_XRP_PRICE = request.security(isFuture ? "BINANCE:XRPUSDTPERP" : "BINANCE:XRPUSDT", timeframe.period, close) * usdkrw
array.push(binance_price_array, BINANCE_XRP_PRICE)
XRP_KIMP = get_kimp(UPBIT_XRP_PRICE, BINANCE_XRP_PRICE)
array.push(kimp_array, XRP_KIMP)

UPBIT_ADA_PRICE = request.security("UPBIT:ADAKRW", timeframe.period, close)
array.push(upbit_price_array, UPBIT_ADA_PRICE)
BINANCE_ADA_PRICE = request.security(isFuture ? "BINANCE:ADAUSDTPERP" : "BINANCE:ADAUSDT", timeframe.period, close) * usdkrw
array.push(binance_price_array, BINANCE_ADA_PRICE)
ADA_KIMP = get_kimp(UPBIT_ADA_PRICE, BINANCE_ADA_PRICE)
array.push(kimp_array, ADA_KIMP)

UPBIT_DOGE_PRICE = request.security("UPBIT:DOGEKRW", timeframe.period, close)
array.push(upbit_price_array, UPBIT_DOGE_PRICE)
BINANCE_DOGE_PRICE = request.security(isFuture ? "BINANCE:DOGEUSDTPERP" : "BINANCE:DOGEUSDT", timeframe.period, close) * usdkrw
array.push(binance_price_array, BINANCE_DOGE_PRICE)
DOGE_KIMP = get_kimp(UPBIT_DOGE_PRICE, BINANCE_DOGE_PRICE)
array.push(kimp_array, DOGE_KIMP)

UPBIT_MATIC_PRICE = request.security("UPBIT:MATICKRW", timeframe.period, close)
array.push(upbit_price_array, UPBIT_MATIC_PRICE)
BINANCE_MATIC_PRICE = request.security(isFuture ? "BINANCE:MATICUSDTPERP" : "BINANCE:MATICUSDT", timeframe.period, close) * usdkrw
array.push(binance_price_array, BINANCE_MATIC_PRICE)
MATIC_KIMP = get_kimp(UPBIT_MATIC_PRICE, BINANCE_MATIC_PRICE)
array.push(kimp_array, MATIC_KIMP)

UPBIT_SOL_PRICE = request.security("UPBIT:SOLKRW", timeframe.period, close)
array.push(upbit_price_array, UPBIT_SOL_PRICE)
BINANCE_SOL_PRICE = request.security(isFuture ? "BINANCE:SOLUSDTPERP" : "BINANCE:SOLUSDT", timeframe.period, close) * usdkrw
array.push(binance_price_array, BINANCE_SOL_PRICE)
SOL_KIMP = get_kimp(UPBIT_SOL_PRICE, BINANCE_SOL_PRICE)
array.push(kimp_array, SOL_KIMP)

UPBIT_TRX_PRICE = request.security("UPBIT:TRXKRW", timeframe.period, close)
array.push(upbit_price_array, UPBIT_TRX_PRICE)
BINANCE_TRX_PRICE = request.security(isFuture ? "BINANCE:TRXUSDTPERP" : "BINANCE:TRXUSDT", timeframe.period, close) * usdkrw
array.push(binance_price_array, BINANCE_TRX_PRICE)
TRX_KIMP = get_kimp(UPBIT_TRX_PRICE, BINANCE_TRX_PRICE)
array.push(kimp_array, TRX_KIMP)

UPBIT_DOT_PRICE = request.security("UPBIT:DOTKRW", timeframe.period, close)
array.push(upbit_price_array, UPBIT_DOT_PRICE)
BINANCE_DOT_PRICE = request.security(isFuture ? "BINANCE:DOTUSDTPERP" : "BINANCE:DOTUSDT", timeframe.period, close) * usdkrw
array.push(binance_price_array, BINANCE_DOT_PRICE)
DOT_KIMP = get_kimp(UPBIT_DOT_PRICE, BINANCE_DOT_PRICE)
array.push(kimp_array, DOT_KIMP)

UPBIT_SHIB_PRICE = request.security("UPBIT:SHIBKRW", timeframe.period, close)
array.push(upbit_price_array, UPBIT_SHIB_PRICE)
BINANCE_SHIB_PRICE = request.security(isFuture ? "BINANCE:SHIBUSDTPERP" : "BINANCE:SHIBUSDT", timeframe.period, close) * usdkrw
array.push(binance_price_array, BINANCE_SHIB_PRICE)
SHIB_KIMP = get_kimp(UPBIT_SHIB_PRICE, BINANCE_SHIB_PRICE)
array.push(kimp_array, SHIB_KIMP)

backgroundColor = #112D4E
cellColor = #F9F7F7
titleColor = #FF2E63
borderColor = #3F72AF
column_size = isSimple ? 1 : isShowPrice ? 4 : 2
row_size = 30
title_tooltip = "Developed by JangDoKang"
var myTable = table.new(position = position.top_right, columns = column_size, rows = row_size, border_width = 2, border_color = borderColor , frame_color= borderColor, frame_width = 2, bgcolor = backgroundColor)
if barstate.isfirst
    if not isSimple
        table.cell(myTable, 0, 0, text = title, bgcolor = titleColor, text_color = cellColor, tooltip=title_tooltip)
        if isShowPrice
            table.merge_cells(myTable, 0, 0, column_size -1, 0)
            table.cell(myTable, column = 0, row = 1, text=col0, text_color=cellColor)
            table.cell(myTable, column = 1, row = 1, text=col1, text_color=cellColor)
            table.cell(myTable, column = 2, row = 1, text=col2, text_color=cellColor)
            table.cell(myTable, column = 3, row = 1, text=col3, text_color=cellColor)
        else
            table.merge_cells(myTable, 0, 0, column_size -1, 0)
            table.cell(myTable, column = 0, row = 1, text=col0, text_color=cellColor)
            table.cell(myTable, column = 1, row = 1, text=col3, text_color=cellColor)
    else
        table.cell(myTable, 0, 0, text = col3, bgcolor = titleColor, text_color = cellColor, tooltip=title_tooltip)

if barstate.islast
    if not isSimple
        if isShowPrice
            for i = 0 to array.size(id_array)-1
                id = array.get(id_array, i)
                upbit_price = array.get(upbit_price_array, i)
                binance_price = array.get(binance_price_array, i) 
                kimp = array.get(kimp_array, i)
                table.cell(myTable, column = 0, row = i+2, text = id, text_color=cellColor, bgcolor = findIndex == i ? color.purple : na)
                table.cell(myTable, column = 1, row = i+2, text = str.tostring(upbit_price, "#.##"), text_color = cellColor, bgcolor = findIndex == i ? color.purple : na)
                table.cell(myTable, column = 2, row = i+2, text = str.tostring(binance_price, "#"), text_color = cellColor, bgcolor = findIndex == i ? color.purple : na)
                table.cell(myTable, column = 3, row = i+2, text = str.tostring(kimp, "#.##") + "%", text_color = kimp > 4 ? color.green : kimp > 2 ? color.yellow : kimp> 0 ? color.white : color.red , bgcolor = findIndex == i ? color.purple : na)
        else
            for i = 0 to array.size(id_array)-1
                id = array.get(id_array, i)
                upbit_price = array.get(upbit_price_array, i)
                binance_price = array.get(binance_price_array, i)
                kimp = array.get(kimp_array, i)
                table.cell(myTable, column = 0, row = i+2, text = id, text_color=cellColor, bgcolor = findIndex == i ? color.purple : na)
                table.cell(myTable, column = 1, row = i+2, text = str.tostring(kimp, "#.##") + "%", text_color = kimp > 4 ? color.green : kimp > 2 ? color.yellow : kimp> 0 ? color.white : color.red , bgcolor = findIndex == i ? color.purple : na)
    else
        table.cell(myTable, column = 0, row = 1, text = str.tostring(BTC_KIMP, "#.##")+"%", text_color = BTC_KIMP > 4 ? color.green : BTC_KIMP> 2 ? color.yellow : BTC_KIMP> 0 ? color.white : color.red, width=4)