How To Use Coinbase API To Determine Whether The Market Is Up Or Down By A Certain Percentage?

- 1 answer

I need help identifying any API endpoint for crypto market updates i.e. an API that will aggregate market price movements then indicate whether the market is up or down and by what percentage.

Refer below for sample update that I need

In the past 24 hours Market is up 1.87%



Using the Coinbase API as you specify in your question, you can request the spot price for specific days from the following endpoint:

Once you've requested the data for today and yesterday's price, it's pretty simple to do a calculation to yield the format you're after:

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>

Basically you want to take the ratio of today's price to yesterday's price, multiply by 100 and subtract 100 to get the percent difference, then determine whether that difference is positive or negative to figure out whether the price went "up" or "down".

The above code gives the following output, as specified:

In the past 24 hours, the market is down 1.82%.