General Idea
This indicator fetches and displays economic data from the World Bank's public API. It allows you to visualize various macroeconomic indicators (like GDP, inflation, trade balance, government spending, etc.) for major countries and regions. The indicator pulls annual data points from the World Bank database and plots them on your chart as a continuous line, making it easy to track long-term economic trends alongside your price action.
How It Works
The indicator connects to the World Bank's "Economy & Growth" topic database, which contains over 200 different economic metrics. When you select a specific indicator (like "GDP growth %" or "Inflation rate") and a country, it retrieves the historical annual data for that combination. Since World Bank data is published yearly while your chart might be daily or intraday, the indicator intelligently maps these annual data points onto your chart's timeframe and fills the gaps between data points to create a smooth, step-like line.
Usage
Choose your desired economic indicator from the dropdown menu – you'll find metrics covering everything from GDP and consumption to trade balances and reserves. Then select the country or region you want to track. The indicator will plot the data as a green line in a lower panel. This is particularly useful for fundamental analysis, allowing you to correlate price movements with underlying economic conditions. For example, you might overlay a stock index with GDP growth data, or compare currency pairs against trade balance figures. Keep in mind that World Bank data updates annually, so the most recent value will extend forward until new data becomes available.
const countries = {
"Australia": "AUS",
"Canada": "CAN",
"China": "CHN",
"European Union": "EUU",
"Japan": "JPN",
"Mexico": "MEX",
"United Kingdom": "GBR",
"United States": "USA",
"World": "WLD"
};
const validIndicatorIds = ["BG.GSR.NFSV.GD.ZS","BM.GSR.CMCP.ZS","BM.GSR.GNFS.CD","BM.GSR.FCTY.CD","BM.GSR.INSF.ZS","BM.GSR.MRCH.CD","BM.GSR.ROYL.CD","BM.GSR.NFSV.CD","BM.GSR.TRAN.ZS","BM.GSR.TOTL.CD","BM.KLT.DINV.CD.WD","BM.KLT.DINV.WD.GD.ZS","BM.GSR.TRVL.ZS","BM.TRF.PRVT.CD","BM.TRF.PWKR.CD.DT","BN.CAB.XOKA.CD","BN.CAB.XOKA.GD.ZS","BN.GSR.FCTY.CD","BN.FIN.TOTL.CD","BN.GSR.GNFS.CD","BN.GSR.MRCH.CD","BN.KAC.EOMS.CD","BN.KLT.DINV.CD","BN.KLT.PTXL.CD","BN.RES.INCL.CD","BN.TRF.CURR.CD","BX.GSR.CCIS.CD","BX.GSR.CCIS.ZS","BX.GSR.FCTY.CD","BX.GSR.GNFS.CD","BX.GSR.CMCP.ZS","BX.GSR.INSF.ZS","BX.GSR.MRCH.CD","BX.GSR.ROYL.CD","BX.GSR.TOTL.CD","BX.GSR.NFSV.CD","BX.GSR.TRAN.ZS","BX.GSR.TRVL.ZS","BX.PEF.TOTL.CD.WD","BX.KLT.DINV.WD.GD.ZS","BX.KLT.DINV.CD.WD","BX.TRF.CURR.CD","BX.TRF.PWKR.DT.GD.ZS","BX.TRF.PWKR.CD.DT","FI.RES.XGLD.CD","FI.RES.TOTL.CD","GC.XPN.TOTL.GD.ZS","GC.DOD.TOTL.GD.ZS","FP.CPI.TOTL.ZG","NE.CON.GOVT.KD","NE.CON.GOVT.CD","NE.CON.GOVT.KD.ZG","NE.CON.GOVT.CN","GC.REV.XGRT.GD.ZS","NE.CON.GOVT.KN","BN.TRF.KOGT.CD","NE.CON.GOVT.ZS","NE.CON.PRVT.CN","NE.CON.PRVT.CD","NE.CON.PRVT.KD","NE.CON.PRVT.KD.ZG","NE.CON.PRVT.KN","NE.CON.PRVT.CN.AD","NE.CON.PRVT.PC.KD.ZG","NE.CON.PRVT.PC.KD","NE.CON.PRVT.ZS","NE.CON.PRVT.PP.KD","NE.CON.PRVT.PP.CD","NE.CON.TOTL.CD","NE.CON.TOTL.CN","NE.CON.TOTL.KN","NE.CON.TOTL.KD","NE.CON.TOTL.ZS","NE.DAB.DEFL.ZS","NE.CON.TOTL.KD.ZG","NE.DAB.TOTL.CN","NE.DAB.TOTL.KN","NE.DAB.TOTL.CD","NE.DAB.TOTL.ZS","NE.DAB.TOTL.KD","NE.EXP.GNFS.CD","NE.EXP.GNFS.KD","NE.EXP.GNFS.CN","NE.EXP.GNFS.KD.ZG","NE.GDI.FTOT.CD","NE.GDI.FTOT.CN","NE.GDI.FTOT.KD","NE.EXP.GNFS.KN","NE.GDI.FTOT.KD.ZG","NE.GDI.FTOT.ZS","NE.GDI.FTOT.KN","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ADJ.NNTY.KD","NY.ADJ.NNAT.GN.ZS","NY.ADJ.NNTY.CD","NY.ADJ.NNTY.KD.ZG","NY.ADJ.NNTY.PC.KD","NY.ADJ.NNTY.PC.KD.ZG","NY.ADJ.SVNG.GN.ZS","NY.ADJ.SVNG.CD","NY.ADJ.NNTY.PC.CD","NY.ADJ.SVNX.CD","NY.ADJ.SVNX.GN.ZS","NY.GDP.DEFL.ZS","NY.GDP.DEFL.KD.ZG","NY.EXP.CAPM.KN","NY.GDP.DISC.CN","NY.GDP.DISC.KN","NY.GDP.FCST.CD","NY.GDP.MKTP.CD","NY.GDP.FCST.KD","NY.GDP.FCST.CN","NY.GDP.MKTP.CN","NY.GDP.MKTP.CN.AD","NY.GDP.MKTP.KD","NY.GDP.MKTP.KD.ZG","NY.GDP.MKTP.KN","NY.GDP.MKTP.PP.CD","NY.GDP.PCAP.CD","NY.GDP.MKTP.PP.KD","NY.GDP.PCAP.CN","NY.GDP.PCAP.KD","NY.GDP.PCAP.KD.ZG","NY.GDP.PCAP.PP.KD","NY.GDS.TOTL.CD","NY.GDP.PCAP.PP.CD","NY.GDP.PCAP.KN","NY.GDS.TOTL.CN","NY.GDS.TOTL.ZS","NY.GDY.TOTL.KN","NY.GNP.ATLS.CD","NY.GNP.MKTP.CN","NY.GNP.MKTP.CN.AD","NY.GNP.MKTP.CD","NY.GNP.MKTP.KD","NY.GNP.MKTP.PP.KD","NY.GNP.PCAP.CD","NY.GNP.MKTP.KD.ZG","NY.GNP.MKTP.PP.CD","NY.GNP.MKTP.KN","NY.GNP.PCAP.CN","NY.GNP.PCAP.KD","NY.GNP.PCAP.KD.ZG","NY.GNP.PCAP.PP.CD","NY.GNP.PCAP.PP.KD","NY.GNS.ICTR.CD","NY.GNS.ICTR.GN.ZS","NY.GNS.ICTR.CN","NY.GNP.PCAP.KN","NY.GNS.ICTR.ZS","NY.GSR.NFCY.CD","NY.GSR.NFCY.CN","NY.GSR.NFCY.KN","NY.TAX.NIND.CD","NY.TAX.NIND.CN","NY.TRF.NCTR.CD","NY.TTF.GNFS.KN","NY.TRF.NCTR.KN","NY.TRF.NCTR.CN","PA.NUS.ATLS","PA.NUS.PPP","PA.NUS.PRVT.PP"];
const data = await request.http(`https://api.worldbank.org/v2/topic/3/indicator?per_page=1000&format=json`);
const currentTopicIndicators = data[1].filter(indicator => validIndicatorIds.includes(indicator.id));
const indicatorTitles = currentTopicIndicators.map(indicator => indicator.name);
const currentIndicatorTitle = input.select('Indicator', indicatorTitles[0], indicatorTitles);
const currentIndicator = currentTopicIndicators.find(indicator => indicator.name == currentIndicatorTitle);
const indicatorId = currentIndicator.id;
const indicatorNote = (str => str.length > 80 ? str.slice(0, 80 - 3) + '...' : str)(currentIndicator.sourceNote);
describe_indicator(`World Bank Indicators - Economy & Growth`, 'lower', { citation: indicatorNote, shortName: `WBI Economy` });
const country = input.select('Country', 'United States', Object.keys(countries));
const indicatorData = await request.http(`https://api.worldbank.org/v2/country/${countries[country]}/indicator/${indicatorId}?format=json&per_page=20000`);
assert(indicatorData[1] && indicatorData[1].some(d => d.value !== null && d.value !== ''), `The selected indicator doesn't return any data for "${country}".`);
const indicatorTimestamps = indicatorData[1].map(record => {
const year = parseInt(record.date);
const milliseconds = Date.UTC(year, 0, 1);
return milliseconds / 1000;
}).reverse();
const indicatorValues = indicatorData[1].map(record => +record.value || null).reverse();
const lastIndexWithValue = indicatorValues.reduce((a, v, i) => a = v !== null ? i : a, 0);
const isLastValueOlderThanCandles = indicatorTimestamps[lastIndexWithValue] < time[0];
if (isLastValueOlderThanCandles) {
indicatorTimestamps.push(time[0]);
indicatorValues.push(indicatorValues[lastIndexWithValue]);
}
const indicatorLine = interpolate_sparse_series(land_points_onto_series(indicatorTimestamps, interpolate_sparse_series(indicatorValues), time, 'ge'), 'constant');
paint(indicatorLine, 'Line', '#469e48');