Major Central Bank Assets [tedtalksmacro]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tedtalksmacro

//@version=5
indicator("Major Central Bank Assets [tedtalksmacro]", overlay = false, format = format.price)

// Calculate Japan Liquidity
bojassetsopen = request.security("FRED:JPNASSETS * FX_IDC:JPYUSD", "D", open, currency = currency.USD)
bojassetsclose = request.security("FRED:JPNASSETS * FX_IDC:JPYUSD", "D", close, currency = currency.USD)
japan_liquidity = bojassetsclose / 1000000000000

//calculate US liquidity
balance_sheet = request.security("USCBBS", 'D', close)
treasury_gen = request.security("WTREGEN", 'D', close)
rrp = request.security("RRPONTTLD", 'D', close)
usd_liquidity = (balance_sheet) / 1000000000000
net_usd_liquidity = (balance_sheet-treasury_gen-rrp) / 1000000000000

// Calculate Euro Zone Liquidity
ecbassetsopen   = request.security("ECBASSETSW * FX:EURUSD", "D", open)
ecbassetsclose   = request.security("ECBASSETSW * FX:EURUSD", "D", close)
euro_liquidity = ecbassetsclose / 1000000000000


// Calculate Chinese Liquidity
pbocassetsopen = request.security("CNCBBS * FX_IDC:CNYUSD", "D", open, currency = currency.USD)
pbocassetsclose = request.security("CNCBBS * FX_IDC:CNYUSD", "D", close, currency = currency.USD)
china_liquidity = pbocassetsclose / 1000000000000

// Calculate average assets to smooth out global net effect
global = (china_liquidity + usd_liquidity + euro_liquidity + japan_liquidity) / 4

// plot liquidity
plot(euro_liquidity, style = plot.style_areabr, linewidth=2, color= color.new(color.green, 40), title = 'ECB Assets [tln USD]')
plot(usd_liquidity, style = plot.style_areabr, linewidth=2, color= color.new(color.red, 40), title = 'Fed Assets [tln USD]')
plot(china_liquidity, style = plot.style_areabr, linewidth=2, color= color.new(color.yellow, 40), title = 'PBoC Assets [tln USD]')
plot(japan_liquidity, style = plot.style_areabr, linewidth=2, color= color.new(color.orange, 40), title = 'BOJ Assets [tln USD]')
plot(global, style = plot.style_line, linewidth=2, color= color.new(color.black, 0), title = 'Average Assets [tln USD]')
plot(net_usd_liquidity, style = plot.style_line, linewidth=2, color= color.new(color.blue, 40), title = 'Net USD Liquidity [tln USD]')