如何使用 Coinbase API 来判断市场是上涨还是下跌一定百分比?

How to use Coinbase API to determine whether the market is up or down by a certain percentage?

我需要帮助来识别加密市场更新的任何 API 端点,即 API 将汇总市场价格变动,然后指示市场是上涨还是下跌以及上涨百分比。

参考下面我需要的示例更新

In the past 24 hours Market is up 1.87%

使用您在问题中指定的 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%.