API should return High, Low, Average price charts

On e.g. Ethereum Price Today - ETH to USD Live - Crypto | Coinranking there’s High, Low, Average numbers that I think should be returned in the API.

1 Like

Hi @IcedQueen, I think that’s an interesting suggestion. Currently, we calculate these numbers in our front-end on Coinranking. For now, we would like to keep the endpoint clean and multi purpose. Maybe we could provide a code sample in the docs, with how to derive the high, low and average from the data yourself?

1 Like

Hey @mark
I think that’d be a great idea.
Although I’ve already found a way to do so, but for others I guess it’d be helpful.

In case my approach could help others (JAVASCRIPT):

First fetch {baseUrl}coin/:uuid/history as e.g. res.
Then turn it into json:

let result = await res.json()

Then:

let map = result.data.history.map(data => data.price)
let high = parseFloat(Math.max(...map))
let low = parseFloat(Math.min(...map))
let average = (high + low) / 2
1 Like

Hi @IcedQueen,

Thanks, that’s great. Others could use this as well :+1:

One of our devs made a suggestion for this code piece that will calculate the average of the full array, instead of the average of only the high and the low.

Here it is:

let prices = result.data.history.map((data) => data.price);
prices = prices.filter((price) => price !== null);
prices = prices.map((price) => parseFloat(price));

const priceSum = prices.reduce((a, b) => a + b, 0);
const averagePrice = pricesSum / prices.length;

Let me know what you think.

1 Like

Hey @mark

What’s the difference between calculating the average price between high and low and then the full array? :smile:

1 Like

Hey @IcedQueen, good question.

I think I can explain it best by giving an example:

Let’s say you have an array of five numbers: 1, 1, 3, 5, 2

Average of the full array
1 + 1 + 3 + 5 + 2 = 12 / 5 = 2.4

Average of the high and low
1 + 5 = 6 / 2 = 3

You see, the average is different with both calculations. The benefit of using the full array is that the average doesn’t get influenced too much by an extreme high or low.

You could also calculate the median. It all depends on what you want to accomplish.

Hope this helps!

Hey @mark

Thanks for the explaination :smile:

What approach is being used here:
1f75d65d55911a613ea420efea072975

Also another question; how can I tell if a coin is falling or rising in price using the API?

My pleasure!

When you request Bitcoin: https://api.coinranking.com/v2/coin/Qwsogvtv82FCd

You can look at this part of the response: "change":"1.71"

In my case it is 1.71, which means a +1.71% price change (in the last 24 hours).

On top of that, you could change the default 24 hour time period to let’s say 30 days by placing ?timePeriod=30d at the end of the URL.

Like this:
https://api.coinranking.com/v2/coin/Qwsogvtv82FCd?timePeriod=30d

Currently, possible time periods are: 1h, 3h, 12h, 24h, 7d, 30d, 3m, 1y, 3y, 5y

:+1:

Oh yeah, if change is e.g. -1.71 it’s falling and if it’s just 1.71 it’s rising, correct?

Also I meant how you were getting the Average number on the coin page?

1 Like

Yes, that’s correct. When it is -1.71 it is falling, 1.71 is rising.

Ah I see, for the average price we do the average of the full array.

:+1:

1 Like