What is the current price of Bitcoin? Writing simple real-time price index.

Thaddeus
Tardis.dev
3 min readJan 12, 2021

--

Cryptocurrency exchanges landscape is no doubt fast moving target with many competing exchanges across the globe. It’s fragmented nature while driving innovation, makes answering one would think simple question like “What is the current price of Bitcoin?” rather tricky due to price differences across exchanges, market data APIs downtimes etc.

In order to answer it, I’ll write simple price index CLI script providing real-time Bitcoin price in US Dollars using an volume weighted average from leading spot exchanges. Such indexes are often used by derivatives exchanges in order to reliably track instrument’s underlying price.

running Bitcoin price index CLI preview

I’m going to use Node.js with tardis-dev lib here, as good WebSocket and JSON support is required — exchanges APIs provide real-time market data via WebSocket as JSON messages (although market data format itself is different for each exchange).

If you’re not comfortable with Node.js, check out tardis-machine that provides the same real-time market data functionality, but as a locally runnable server you can host on your own and connect to it from any language that supports WebSocket or HTTP protocol.

Both tardis-dev and tardis-machine are written and maintained by me.

Let’s start by installing tardis-dev package that handles WebSocket connections management, data normalization and consolidated data streaming from multiple exchanges.

tardis-dev lib usage preview
npm install tardis-dev

Now, I’m going to define real-time Bitcoin index constituents exchanges and define few fields for each that will be necessary for index price calculation. In practice USD and USDT prices aren’t always the same, but for the sake of this simple tutorial let’s pretend they are.

With index constituents defined, I need to update their relevant fields with latest pricing and volume data as soon as there is any price change for given exchange — meaning someone sold or bought BTC for USD. In order to do so let’s subscribe to real-time trade feeds of constituent exchanges and consume those as async iterable by updating relevant constituents info.

… and the most important part: Bitcoin price index calculation itself.
There are many ways to do it and definitely writing robust and reliable one requires handling many edge cases and lots of real world testing.
My implementation is rather simple and not optimized one, but still:

  • excludes outliers — exchanges with highest and lowest price
  • excludes stale data exchange — exchanges with no trades within last 30 seconds
  • uses volume weighted average — exchanges with more volume have more weight

As a last step, I’m going to print current price index with it’s constituents every 200ms. Here’s final script, that you can copy and run locally:

running Bitcoin price index CLI preview

That’s it. Now we can more reliably track current price of Bitcoin.

--

--