【问题标题】:How to use Coinbase API to determine whether the market is up or down by a certain percentage?如何使用 Coinbase API 判断市场是上涨还是下跌一定百分比?
【发布时间】:2021-10-11 21:39:58
【问题描述】:

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

请参阅下面的我需要的示例更新

过去 24 小时市场上涨 1.87%

【问题讨论】:

    标签: endpoint cryptocurrency coinbase-api


    【解决方案1】:

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

    【讨论】:

    • 在上面展开...(我的代码很丑,但它是凌晨 1:30,请原谅我)这是一个示例,其中包含两天现货价格的 API“Fetch”和上面的代码来计算% 差异...link
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 2015-07-06
    • 2020-10-18
    相关资源
    最近更新 更多