Global Liquidity
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rodopacapital

//@version=5
indicator("Global Liquidity", overlay=false)

// Calculate the global liquidity value using the formula

us = request.security("FRED:M2SL", 'M', close) 
eu = request.security("ECONOMICS:EUM2", 'M', close) * request.security("FX:EURUSD", timeframe.period, close)
jp = request.security("ECONOMICS:JPM2", 'M', close) / request.security("FX:USDJPY", timeframe.period, close)
cn = request.security("ECONOMICS:CNM2", 'M', close) / request.security("FX:USDCNH", timeframe.period, close) 
gb = request.security("ECONOMICS:GBM2", 'M', close) * request.security("FX:GBPUSD", timeframe.period, close)
ind = request.security("ECONOMICS:INM2", 'M', close) / request.security("FX_IDC:USDINR", timeframe.period, close)
can = request.security("ECONOMICS:CAM2", 'M', close) / request.security("FX:USDCAD", timeframe.period, close)
kor = request.security("ECONOMICS:KRM2", 'M', close) / request.security("FX_IDC:USDKRW", timeframe.period, close)
ru = request.security("ECONOMICS:RUM2", 'M', close) / request.security("FX_IDC:USDRUB", timeframe.period, close)
br = request.security("ECONOMICS:BRM2", 'M', close) / request.security("FX_IDC:USDBRL", timeframe.period, close)
au = request.security("ECONOMICS:AUM1", 'M', close) / request.security("FX_IDC:USDAUD", timeframe.period, close)
mx = request.security("ECONOMICS:MXM2", 'M', close) / request.security("FX:USDMXN", timeframe.period, close)
id = request.security("ECONOMICS:IDM2", 'M', close) / request.security("FX_IDC:USDIDR", timeframe.period, close)
sa = request.security("ECONOMICS:SAM2", 'M', close) 
tr = request.security("ECONOMICS:TRM2", 'M', close) / request.security("FX_IDC:USDTRY", timeframe.period, close)
ch = request.security("ECONOMICS:CHM2", 'M', close) / request.security("FX_IDC:USDCHF", timeframe.period, close)
th = request.security("ECONOMICS:THM1", 'M', close) / request.security("FX_IDC:USDTHB", timeframe.period, close)
ar = request.security("ECONOMICS:ARM2", 'M', close) / request.security("FX_IDC:USDARS", timeframe.period, close)
ng = request.security("ECONOMICS:NGM2", 'M', close) / request.security("FX_IDC:USDNGN", timeframe.period, close)
za = request.security("ECONOMICS:ZAM2", 'M', close) / request.security("FX_IDC:USDZAR", timeframe.period, close)
hk = request.security("ECONOMICS:HKM2", 'M', close) / 7.80



// Inflation denominated Global liquidity 
global_liquidity_inflation_denominated = (us + eu + jp + cn + gb + ind + can + kor + ru + br + au + mx + id + sa + tr + ch + th + ar + ng + za + hk )/ 10000000000000 * request.security("FRED:CUUR0000SA0R", 'D', close)
euroatlantic_liquidity_inflation_denominated = (us + eu + gb + jp + can + au) / 10000000000000 * request.security("FRED:CUUR0000SA0R", 'D', close)
rest_of_the_world_liquidity_inflation_denominated  = global_liquidity_inflation_denominated - euroatlantic_liquidity_inflation_denominated
west_vs_east_spread = euroatlantic_liquidity_inflation_denominated/rest_of_the_world_liquidity_inflation_denominated

// Global liquidity in trillion dollars
global_liquidity = (us + eu + jp + cn + gb + ind + can + kor + ru + br + au + mx + id + sa + tr + ch + th + ar + ng + za + hk )/ 1000000000000 
euroatlantic_liquidity= (us + eu + gb + jp + can + au) / 1000000000000 
rest_of_the_world_liquidity  = global_liquidity - euroatlantic_liquidity



// Plot the global liquidity value as a candlestick chart
plot(euroatlantic_liquidity_inflation_denominated, "West denominated", color = color.blue, trackprice=true)
plot(rest_of_the_world_liquidity_inflation_denominated, "East denominated", color = color.red)
plot(euroatlantic_liquidity, "West in trillions of USD", color = color.blue, trackprice=true, display = display.none)
plot(rest_of_the_world_liquidity, "East in trillions of USD", color = color.red, display = display.none)
plot(west_vs_east_spread, "West/East", color = color.green, display = display.none)
plot(global_liquidity, "Global Liquidity in trillions of USD", color = color.yellow, display = display.none)


