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

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

// Calculate the global liquidity value using the formula

us = request.security("FRED:WALCL", 'M', close) 
us_RRP = request.security("FRED:RRPONTSYD", 'M', close)
us_TGA = request.security("FRED:WTREGEN", 'M', close)
eu = request.security("FRED:ECBASSETSW", 'M', close)* request.security("FX:EURUSD", timeframe.period, close)
jp = request.security("FRED:JPNASSETS", 'M', close)/ request.security("FX:USDJPY", timeframe.period, close)
cn_m2 = request.security("ECONOMICS:CNM2", 'M', close) / request.security("FX:USDCNH", timeframe.period, close) 
cn = cn_m2/9.22 //Approx of total assets from China's M2

us_funding_liquidity = (us - us_RRP - us_TGA)
global_ex_china = (us_funding_liquidity + eu + jp)/ 1000000000000
china = cn/1000000000000
global = global_ex_china + china

// Calculating percentage change from the previous period
percentChange = ((global - ta.valuewhen(true, global, 1)) / ta.valuewhen(true, global, 1)) * 100
cumulativePercentChange = ta.cum(percentChange)
length = 100 // Specify the length manually
lowestValue = ta.lowest(cumulativePercentChange, length)
highestValue = ta.highest(cumulativePercentChange, length)
oscillator = ((cumulativePercentChange - lowestValue) / (highestValue - lowestValue)) * 100

//plot(percentChange, "Percent change", color=color.blue, trackprice=true)
plot(global, "Global liquidity in $Bn", color=color.green, trackprice=true)

//plot(global_ex_china, "US-EU-JP", color =color.green, trackprice=true)
//plot(china, "China liquidity approx", color =color.red, trackprice=true)


