Last Wednesday we moved from old system of price calculation to the completely new one. The results were visible straight away. But only for the little number of people. Then we thought — Hey, how on earth can an average user be aware of such update? That’s when we sat down and decided that we’ll share our know-how.
In the beginning, we’ll explain the process of calculation that was used before:
1. From every exchange that is listed on coinpaprika, we fetched the markets from their API. Every market has to contain 3 things:
Name of the market, Price with the given unit and the 24h volume. Here is an example that we will be working on:
2. For every market that was fetched, we were calculating both the price and the volume in both BTC and USD. For this calculation, we used prices calculated previously on coinpaprika (from the previous iteration of the algorithm). To simplify it we will focus only on USD calculation.
The previous price of USDT to USD was 1.01
3. For every market given, we were also calculating the price of the second currency in the pair.
The previous price of LTC to USD was 44.00
4. This way we have already a full market list for any given coin, here is an example:
For markets from the screen above, the price for LTC/BTC was calculated like in step #2 and XRP/LTC similar to the step #3 (Here LTC is the second currency in our pair)
5. After having a lot of markets with prices in USD it’s quite easy to calculate the overall price. It is the waged average of all markets with a given volume. From this calculation, excluded are detected outliers or markets with no fees and transaction mining. The final price is displayed on the main page and can be used for the next iteration of the algorithm
So where was the problem?
There were none for 98% of the time. But they started appearing as there were more volatile movements on the market. We witnessed few times that the price of BTC was different to USD than on other listing websites. For example, as there were massive drops on the market, our BTC/USD price was higher than on the market itself or in the competitors’ platforms.
What was the root of the problem?
As it is described in step #2 and #3 the price did not depend only on the present situation on the market but also depended on the price from the last iteration of the algorithm
“The previous price of USDT to USD was 1.01”
“The previous price of LTC to USD was 44.00”
That algorithm, to calculate the present price had to know the price from the previous iteration, and that price was also calculated in correlation to the previous iteration, etc… This resulted in the slow reaction of our displayed prices to the volatile movements on the market.
What has been changed?
Our attempt to limit these dependencies was to determine new conversion rates (reference prices) of currencies, which take into account only the current state of the entire market.
How we managed to do it?
We had to bring the price in each market to the common unit, and then set a weighted average — this way we would get a conversion rate that covers all markets. On the website we mainly use prices in USD and BTC, so we will be converting prices to these units. It is a multi-stage process, wherein the initial state we had no knowledge of how much one unit of a given currency costs. We set a reference point, which is Bitcoin for us (BTC markets for most currencies are usually the most liquid ones), for which in the initial state we calculate the price in USD as the weighted average price from all BTC / USD markets. Then we can start converting each market to BTC and USD prices:
1. In the first step, we calculate average currency prices using only the BTC and FIAT markets (by converting them first to USD at the exchange rate from https://openexchangerates.org/ and then to BTC at the average exchange rate from the BTC / USD markets) — as a result we receive information on how much BTC costs each currency, on average.
2. We also now know how much USD costs each currency, because we know the BTC / USD converter.
3. Some cryptocurrencies do not have markets with BTC or BTC markets are the minority of the whole volume, which makes the calculated average inaccurate.
4. In the next step, therefore, we use all previously calculated conversion rates to calculate new prices of cryptocurrencies in BTC and USD. By repeating these steps few times we have data calculated from all markets simultaneously.
By using this method the current price depends on these few iterations of bringing market prices to BTC and USD rather than the entire historical price chain.