Central Bank Balance Sheets
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © andr3w321

//@version=5
indicator("Central Bank Balance Sheets", format=format.volume, precision=3)

display_option = input.string(title="Display", defval="Central Bank Assets", options=["Central Bank Assets", "Central Bank Assets as % of GDP", "Total Central Bank Assets"])

fed = request.security('USCBBS', timeframe.period, close)
ccb = request.security('CNCBBS', timeframe.period, close)
ecb = request.security('ECBASSETSW', timeframe.period, close)
jcb = request.security('JPCBBS', timeframe.period, close)

cny = request.security('CNYUSD', timeframe.period, close)
euro = request.security('EURUSD', timeframe.period, close)
jpy = request.security('JPYUSD', timeframe.period, close)

us_gdp = request.security('USGDP', timeframe.period, close)
china_gdp = request.security('CNGDP', timeframe.period, close)
eu_gdp = request.security('EUGDP', timeframe.period, close)
japan_gdp = request.security('JPGDP', timeframe.period, close)

total_assets = fed + ccb * cny + ecb * euro + jcb * jpy

us_val = fed
china_val = ccb * cny
eu_val = ecb * euro
japan_val = jcb * jpy

if display_option == "Central Bank Assets as % of GDP"
    us_val := us_val / us_gdp
    china_val := china_val / china_gdp
    eu_val := eu_val / eu_gdp
    japan_val := japan_val / japan_gdp

plot(display_option == "Central Bank Assets" or display_option == "Central Bank Assets as % of GDP" ? us_val : na, title='FED', color=color.blue)
plot(display_option == "Central Bank Assets" or display_option == "Central Bank Assets as % of GDP" ? china_val : na, title='China CB', color=color.orange)
plot(display_option == "Central Bank Assets" or display_option == "Central Bank Assets as % of GDP" ? eu_val : na, title='Europe CB', color=color.red)
plot(display_option == "Central Bank Assets" or display_option == "Central Bank Assets as % of GDP" ? japan_val : na, title='Japan CB', color=color.green)

plot(display_option == "Total Central Bank Assets" ? total_assets : na, title='Total Central Bank Assets', color=color.purple)

if display_option == "Central Bank Assets" or display_option == "Central Bank Assets as % of GDP"
    label1 = label.new(chart.right_visible_bar_time, us_val, "FED", xloc=xloc.bar_time, yloc=yloc.price, style=label.style_none)
    label2 = label.new(chart.right_visible_bar_time, china_val, "China", xloc=xloc.bar_time, yloc=yloc.price, style=label.style_none)
    label3 = label.new(chart.right_visible_bar_time, eu_val, "EU", xloc=xloc.bar_time, yloc=yloc.price, style=label.style_none)
    label4 = label.new(chart.right_visible_bar_time, japan_val, "Japan", xloc=xloc.bar_time, yloc=yloc.price, style=label.style_none)

    label.delete(label1[1])
    label.delete(label2[1])
    label.delete(label3[1])
    label.delete(label4[1])