TOP 4 STABLECOIN MARKET CAP
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Springtimestn

//@version=5
indicator("TOP 4 STABLECOIN MARKET CAP", "TOP 4 stUSD MC", overlay=true)

toCompactString(value) =>
    formatted_value = ""
    
    if math.abs(value) >= 1000000000
        formatted_value := str.tostring(math.round(value / 1000000000, 2)) + "B"
    else if math.abs(value) >= 1000000
        formatted_value := str.tostring(math.round(value / 1000000, 2)) + "M"
    else
        formatted_value := str.tostring(math.round(value, 2))
    
    formatted_value

// USDT
sym_usdt = input.symbol("CRYPTOCAP:USDT", "USDT Market Cap", group = "TOKEN MARKET CAP")
sym_usdt_color = color.new(color.red, 0)
sym_usdt_mc = request.security(sym_usdt, timeframe.period, close)
sym_usdt_mc_past = ta.valuewhen(true, sym_usdt_mc, 1)
usdt_mc_change = sym_usdt_mc - sym_usdt_mc_past

// USDC
sym_usdc = input.symbol("CRYPTOCAP:USDC", "USDC Market Cap", group = "TOKEN MARKET CAP")
sym_usdc_color = color.new(color.blue, 0)
sym_usdc_mc = request.security(sym_usdc, timeframe.period, close)
sym_usdc_mc_past = ta.valuewhen(true, sym_usdc_mc, 1)
usdc_mc_change = sym_usdc_mc - sym_usdc_mc_past

// TUSD
sym_tusd = input.symbol("CRYPTOCAP:TUSD", "TUSD Market Cap", group = "TOKEN MARKET CAP")
sym_tusd_color = color.new(color.yellow, 0)
sym_tusd_mc = request.security(sym_tusd, timeframe.period, close)
sym_tusd_mc_past = ta.valuewhen(true, sym_tusd_mc, 1)
tusd_mc_change = sym_tusd_mc - sym_tusd_mc_past

// BUSD
sym_busd = input.symbol("GLASSNODE:BUSD_SUPPLY", "BUSD Market Cap", group = "TOKEN MARKET CAP")
sym_busd_color = color.new(color.lime, 0)
sym_busd_mc = request.security(sym_busd, timeframe.period, close)
sym_busd_mc_past = ta.valuewhen(true, sym_busd_mc, 1)
busd_mc_change = sym_busd_mc - sym_busd_mc_past

// stUSD
total_mc = sym_usdt_mc + sym_usdc_mc + sym_tusd_mc + sym_busd_mc
total_color = color.new(color.white, 0)
total_mc_past = sym_usdt_mc_past + sym_usdc_mc_past + sym_tusd_mc_past + sym_busd_mc_past
total_mc_change = total_mc - total_mc_past

usdt_scaling_factor = math.round(total_mc / sym_usdt_mc, 1)
usdc_scaling_factor = math.round(total_mc / sym_usdc_mc, 1)
tusd_scaling_factor = math.round(total_mc / sym_tusd_mc, 1)
busd_scaling_factor = math.round(total_mc / sym_busd_mc, 1)

usdt_move = input.float(0.0, "Move USDT Chart", minval=-0.1, maxval=0.1, step=0.01, group = "MOVE CHART STEP TO CLEAR CHART")
usdc_move = input.float(0.01, "Move USDC Chart", minval=-0.1, maxval=0.1, step=0.01, group = "MOVE CHART STEP TO CLEAR CHART")
tusd_move = input.float(0.02, "Move TUSD Chart", minval=-0.1, maxval=0.1, step=0.01, group = "MOVE CHART STEP TO CLEAR CHART")
busd_move = input.float(0.03, "Move BUSD Chart", minval=-0.1, maxval=0.1, step=0.01, group = "MOVE CHART STEP TO CLEAR CHART")

sym_usdt_mc_scaled = sym_usdt_mc * usdt_scaling_factor - total_mc * usdt_move
sym_usdc_mc_scaled = sym_usdc_mc * usdc_scaling_factor - total_mc * usdc_move
sym_tusd_mc_scaled = sym_tusd_mc * tusd_scaling_factor - total_mc * tusd_move
sym_busd_mc_scaled = sym_busd_mc * busd_scaling_factor - total_mc * busd_move

var label sym_usdt_label = na
var label sym_usdc_label = na
var label sym_tusd_label = na
var label sym_busd_label = na
var label total_label = na

if barstate.islast
    if na(sym_usdt_label)
        labelUsdt = "USDT: " + toCompactString(sym_usdt_mc) + " (" + toCompactString(usdt_mc_change) + ")"
        sym_usdt_label := label.new(x=bar_index, y=sym_usdt_mc_scaled, text=labelUsdt, textcolor=total_color, textalign=text.align_center, size=size.small)
    else
        label.set_text(sym_usdt_label, "USDT: " + toCompactString(sym_usdt_mc) + " (" + toCompactString(usdt_mc_change) + ")")
        label.set_y(sym_usdt_label, sym_usdt_mc_scaled)

if barstate.islast
    if na(sym_usdc_label)
        labelUsdc = "USDC: " + toCompactString(sym_usdc_mc) + " (" + toCompactString(usdc_mc_change) + ")"
        sym_usdc_label := label.new(x=bar_index, y=sym_usdc_mc_scaled, text=labelUsdc, textcolor=total_color, textalign=text.align_left, size=size.small)
    else
        label.set_text(sym_usdc_label, "USDC: " + toCompactString(sym_usdc_mc) + " (" + toCompactString(usdc_mc_change) + ")")
        label.set_y(sym_usdc_label, sym_usdc_mc_scaled)

if barstate.islast
    if na(sym_tusd_label)
        labelTusd = "TUSD: " + toCompactString(sym_tusd_mc) + " (" + toCompactString(tusd_mc_change) + ")"
        sym_tusd_label := label.new(x=bar_index, y=sym_tusd_mc_scaled, text=labelTusd, textcolor=total_color, textalign=text.align_left, size=size.small)
    else
        label.set_text(sym_tusd_label, "TUSD: " + toCompactString(sym_tusd_mc) + " (" + toCompactString(tusd_mc_change) + ")")
        label.set_y(sym_tusd_label, sym_tusd_mc_scaled)

if barstate.islast
    if na(sym_busd_label)
        labelBusd = "BUSD: " + toCompactString(sym_busd_mc) + " (" + toCompactString(busd_mc_change) + ")"
        sym_busd_label := label.new(x=bar_index, y=sym_busd_mc_scaled, text=labelBusd, textcolor=total_color, textalign=text.align_left, size=size.small)
    else
        label.set_text(sym_busd_label, "BUSD: " + toCompactString(sym_busd_mc) + " (" + toCompactString(busd_mc_change) + ")")
        label.set_y(sym_busd_label, sym_busd_mc_scaled)

if barstate.islast
    if na(total_label)
        labelTotal = "T.stUSD: " + toCompactString(total_mc) + " (" + toCompactString(total_mc_change) + ")"
        total_label := label.new(x=bar_index, y=total_mc, text=labelTotal, textcolor=total_color, textalign=text.align_left, size=size.small)
    else
        label.set_text(total_label, "T.stUSD: " + toCompactString(total_mc) + " (" + toCompactString(total_mc_change) + ")")
        label.set_y(total_label, total_mc)

plot(sym_usdt_mc_scaled, title="USDT", color=sym_usdt_color, display=display.pane)
plot(sym_usdc_mc_scaled, title="USDC", color=sym_usdc_color, display=display.pane)
plot(sym_tusd_mc_scaled, title="TUSD", color=sym_tusd_color, display=display.pane)
plot(sym_busd_mc_scaled, title="BUSD", color=sym_busd_color, display=display.pane)
plot(total_mc, title="TOP 4 STABLECOIN MARKET CAP", color=total_color,display=display.pane)






