Hi everyone, I'm currently trying to create a Retool app that fetches real-time data from multiple sources. I'm facing an issue with referencing my API queries within a JavaScript query.
Here's a quick rundown of what I've done so far:
- I've set up three API queries (
BTCZAR
,BTCUSD
, andUSDZAR
) to fetch BTC/ZAR price from the VALR exchange, BTC/USD price from Kraken, and USD/ZAR exchange rate from an exchange rate API. These queries seem to run fine individually, returning the expected data. - I've created a JavaScript query, "calculateArbitrage", which is supposed to extract the necessary data from the API responses, perform arbitrage calculations, and store the data in temporary state variables (
btc_zar
,btc_usd
,usd_zar
).
Here's the code I've been trying to use:
const btc_zar_value = BTCZAR.data.lastTradedPrice;
const btc_usd_value = BTCUSD.data.result.XXBTZUSD.c[0];
const usd_zar_value = USDZAR.data.rates.ZAR;
btc_zar.setValue(btc_zar_value);
btc_usd.setValue(btc_usd_value);
usd_zar.setValue(usd_zar_value);
const zar_usd = 1 / usd_zar_value;
const btc_usd_converted = btc_zar_value * zar_usd;
const arbitrage_percentage = ((btc_usd_value - btc_usd_converted) / btc_usd_converted) * 100;
return {
'Opportunity': arbitrage_percentage > 0 ? 'Buy on VALR, Sell on Kraken' : 'Buy on Kraken, Sell on VALR',
'Potential Profit (%)': Math.abs(arbitrage_percentage)
};
However, when I try to run the "calculateArbitrage" query, I'm getting an error message saying that BTCZAR
is not defined. It seems like I'm not able to access the data from my API queries within the JavaScript query.
Any help on how to correctly reference my API queries within the JavaScript query would be greatly appreciated. Thank you!