【发布时间】:2021-10-11 21:39:58
【问题描述】:
我需要帮助识别加密市场更新的任何 API 端点,即一个 API,它将汇总市场价格变动,然后指示市场是上涨还是下跌以及百分比。
请参阅下面的我需要的示例更新
过去 24 小时市场上涨 1.87%
【问题讨论】:
标签: endpoint cryptocurrency coinbase-api
我需要帮助识别加密市场更新的任何 API 端点,即一个 API,它将汇总市场价格变动,然后指示市场是上涨还是下跌以及百分比。
请参阅下面的我需要的示例更新
过去 24 小时市场上涨 1.87%
【问题讨论】:
标签: endpoint cryptocurrency coinbase-api
使用您在问题中指定的 Coinbase API,您可以从以下端点请求特定日期的现货价格:
https://api.coinbase.com/v2/prices/BTC-USD/spot?date=2021-10-10
一旦您请求了今天和昨天价格的数据,就可以很简单地计算出您所需要的格式:
const response1 = JSON.parse('{"data":{"base":"BTC","currency":"USD","amount":"54963.29"}}').data.amount;
const response2 = JSON.parse('{"data":{"base":"BTC","currency":"USD","amount":"53965.18"}}').data.amount;
const ratio = parseFloat(response2) / parseFloat(response1);
const percentDifference = (ratio*100) - 100;
document.querySelector("#output").textContent = "In the past 24 hours, the market is " + (percentDifference > 0 ? "up" : "down") + " " + Math.abs(percentDifference).toFixed(2) + "%.";
<span id="output"></span>
基本上,您想取今天价格与昨天价格的比率,乘以 100 并减去 100 得到百分比差异,然后确定该差异是正数还是负数,以确定价格是“上涨”还是“上涨”下来”。
上面的代码给出了如下的输出:
In the past 24 hours, the market is down 1.82%.
【讨论】: