Crypto Market Overview
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kikfraben
// Updated last on: Nov 6 2023
//@version=5

indicator("Crypto Market Overview", "CMO", true)

// Define Lookback Period of % Change
period = input(1, "Lookback Period")

// Define Currency of Table
cur = input.string("USD", "Currency", options = ["USD", "EUR", "CNY", "JPY", "AUD", "RUB", "INR", "AED"])

// Get Major Market Cap Data
btc_cap = request.security("CRYPTOCAP:BTC", "D", close, currency = cur) // BTC
eth_cap = request.security("CRYPTOCAP:ETH", "D", close, currency = cur) // ETH
total_cap = request.security("CRYPTOCAP:TOTAL", "D", close, currency = cur) // TOTAL

// Get Stablecoin Market Cap Data
usdt_cap = request.security("CRYPTOCAP:USDT", "D", close, currency = cur) // USDT
usdc_cap = request.security("CRYPTOCAP:USDC", "D", close, currency = cur) // USDC
dai_cap = request.security("CRYPTOCAP:DAI", "D", close, currency = cur) // DAI
tusd_cap = request.security("CRYPTOCAP:TUSD", "D", close, currency = cur) // TUSD
busd_cap = request.security("CRYPTOCAP:BUSD", "D", close, currency = cur) // BUSD
stable_cap = usdt_cap + usdc_cap + dai_cap + tusd_cap + busd_cap

// Get Shitcoin Market Cap Data
shit_cap = total_cap - btc_cap - eth_cap - stable_cap

// Get Major Dominance Data
btc_dom = request.security("CRYPTOCAP:BTC.D", "D", close, currency = cur) // BTC.D
eth_dom = request.security("CRYPTOCAP:ETH.D", "D", close, currency = cur) // ETH.D
total_dom = "100%"

// Get Stablecoin Dominance Data
usdt_dom = request.security("CRYPTOCAP:USDT.D", "D", close, currency = cur) // USDT.D
usdc_dom = request.security("CRYPTOCAP:USDC.D", "D", close, currency = cur) // USDC.D
dai_dom = request.security("CRYPTOCAP:DAI.D", "D", close, currency = cur) // DAI.D
tusd_dom = request.security("CRYPTOCAP:TUSD.D", "D", close, currency = cur) // TUSD.D
busd_dom = request.security("CRYPTOCAP:BUSD.D", "D", close, currency = cur) // BUSD.D
stable_dom = usdt_dom + usdc_dom + dai_dom + tusd_dom + busd_dom

// Get Shitcoin Dominance Data
shit_dom = 100 - btc_dom - eth_dom - stable_dom

// Format Market Cap Data & Get Market Cap Rate of Change
f_btc_cap = str.tostring(btc_cap / 1000000000, ".00") + "B"
f_eth_cap = str.tostring(eth_cap / 1000000000, ".00") + "B"
f_total_cap = str.tostring(total_cap / 1000000000, ".00") + "B"
f_stable_cap = str.tostring(stable_cap / 1000000000, ".00") + "B"
f_shit_cap = str.tostring(shit_cap / 1000000000, ".00") + "B"

roc_btc_cap = ta.roc(btc_cap, period)
roc_eth_cap = ta.roc(eth_cap, period)
roc_total_cap = ta.roc(total_cap, period)
roc_stable_cap = ta.roc(stable_cap, period)
roc_shit_cap = ta.roc(shit_cap, period)

f_btc_cap_roc = str.tostring(roc_btc_cap, format.percent) 
f_eth_cap_roc = str.tostring(roc_eth_cap, format.percent)
f_stable_cap_roc = str.tostring(roc_stable_cap, format.percent)
f_shit_cap_roc = str.tostring(roc_shit_cap, format.percent)
f_total_cap_roc = str.tostring(roc_total_cap, format.percent)

// Format Dominance & Get Dominance Rate of Change
roc_btc_dom = ta.roc(btc_dom, period)
roc_eth_dom = ta.roc(eth_dom, period)
roc_stable_dom = ta.roc(stable_dom, period)
roc_shit_dom = ta.roc(shit_dom, period)

f_btc_dom = str.tostring(btc_dom, format.percent) 
f_eth_dom = str.tostring(eth_dom, format.percent)
f_stable_dom = str.tostring(stable_dom, format.percent)
f_shit_dom = str.tostring(shit_dom, format.percent)

f_btc_dom_roc = str.tostring(roc_btc_dom, format.percent)
f_eth_dom_roc = str.tostring(roc_eth_dom, format.percent)
f_stable_dom_roc = str.tostring(roc_stable_dom, format.percent)
f_shit_dom_roc = str.tostring(roc_shit_dom, format.percent)

// Color Conditions
up_col = #3fa8c9
down_col = #c93f3f
bg_col = color.new(color.white, 95)
txt_col = color.white
txt_col1 = color.new(color.white, 25)
txt_col2 = color.new(color.green, 35)

table_col = color.new(color.white, 70)

// Get Table
main = table.new(position = position.middle_center, columns = 6,rows = 8, frame_color = table_col, border_color = table_col, frame_width = 2, border_width = 2)
// Merge Cells
table.merge_cells(main, 0, 0, 4, 0)
// Layout
table.cell(main, 0, 0, "Crypto Market Overview", text_color = color.yellow, bgcolor = bg_col)
table.cell(main, 0, 1, "Asset", text_color = txt_col, bgcolor = bg_col)
table.cell(main, 1, 1, "Market Cap", text_color = txt_col, bgcolor = bg_col)
table.cell(main, 2, 1, "Cap RoC", text_color = txt_col, bgcolor = bg_col)
table.cell(main, 3, 1, "Dominance", text_color = txt_col, bgcolor = bg_col)
table.cell(main, 4, 1, "Dom RoC", text_color = txt_col, bgcolor = bg_col)
table.cell(main, 0, 2, "Bitcoin", text_color = txt_col1, bgcolor = bg_col)
table.cell(main, 0, 3, "Ethereum", text_color = txt_col1, bgcolor = bg_col)
table.cell(main, 0, 4, "Stablecoins", text_color = txt_col1, bgcolor = bg_col)
table.cell(main, 0, 5, "Shitcoins", text_color = txt_col1, bgcolor = bg_col)
table.cell(main, 0, 6, "Total", text_color = txt_col, bgcolor = bg_col)
// BTC
table.cell(main, 1, 2, f_btc_cap, text_color = roc_btc_cap > 0 ? up_col : down_col, bgcolor = bg_col)
table.cell(main, 2, 2, f_btc_cap_roc, text_color = roc_btc_cap > 0 ? up_col : down_col, bgcolor = bg_col)
table.cell(main, 3, 2, f_btc_dom, text_color = roc_btc_dom > 0 ? up_col : down_col, bgcolor = bg_col)
table.cell(main, 4, 2, f_btc_dom_roc, text_color = roc_btc_dom > 0 ? up_col : down_col, bgcolor = bg_col)
// ETH
table.cell(main, 1, 3, f_eth_cap, text_color = roc_eth_cap > 0 ? up_col : down_col, bgcolor = bg_col)
table.cell(main, 2, 3, f_eth_cap_roc, text_color = roc_eth_cap > 0 ? up_col : down_col, bgcolor = bg_col)
table.cell(main, 3, 3, f_eth_dom, text_color = roc_eth_dom > 0 ? up_col : down_col, bgcolor = bg_col)
table.cell(main, 4, 3, f_eth_dom_roc, text_color = roc_eth_dom > 0 ? up_col : down_col, bgcolor = bg_col)
// Stablecoins
table.cell(main, 1, 4, f_stable_cap, text_color = roc_stable_cap > 0 ? up_col : down_col, bgcolor = bg_col)
table.cell(main, 2, 4, f_stable_cap_roc, text_color = roc_stable_cap > 0 ? up_col : down_col, bgcolor = bg_col)
table.cell(main, 3, 4, f_stable_dom, text_color = roc_stable_dom > 0 ? up_col : down_col, bgcolor = bg_col)
table.cell(main, 4, 4, f_stable_dom_roc, text_color = roc_stable_dom > 0 ? up_col : down_col, bgcolor = bg_col)
// Shitcoins
table.cell(main, 1, 5, f_shit_cap, text_color = roc_shit_cap > 0 ? up_col : down_col, bgcolor = bg_col)
table.cell(main, 2, 5, f_shit_cap_roc, text_color = roc_shit_cap > 0 ? up_col : down_col, bgcolor = bg_col)
table.cell(main, 3, 5, f_shit_dom, text_color = roc_shit_dom > 0 ? up_col : down_col, bgcolor = bg_col)
table.cell(main, 4, 5, f_shit_dom_roc, text_color = roc_shit_dom > 0 ? up_col : down_col, bgcolor = bg_col)
// Total
table.cell(main, 1, 6, f_total_cap, text_color = txt_col, bgcolor = bg_col)
table.cell(main, 2, 6, f_total_cap_roc, text_color = txt_col, bgcolor = bg_col)
table.cell(main, 3, 6, total_dom, text_color = txt_col, bgcolor = bg_col)
table.cell(main, 4, 6, text = "na", text_color = txt_col, bgcolor = bg_col)