In the following examples, we will explore how to create a live price ticker which uses the latest order book data on the exchange to calculate the current price of an asset.

What is a price ticker?

To put it simply, a price ticker is the latest price for an asset on a given exchange. In our examples, we will be using the midpoint between the current best bid and ask order for a trading pair to determine the ticker price.

The reason we use the midpoint between the best bid and ask order instead of simply using the last executed trade for the pair is because the cryptocurrency market is volatile and assets can have large spreads. As a result, using the last trade as the ticker can produce a poor experience for investors who are trying to track their portfolio value in real-time. The midpoint produces less drastic changes in value from tick to tick.

How do we generate a live price ticker?

We will cover two different ways to generate a live price ticker. The first strategy will be to access the data directly from the exchange and calculate our own price ticker based on the information we want. Using an open source like CCXT, we have fine control over the data we access.

The second strategy will be to use the Shrimpy Universal Crypto Trading APIs. These APIs provide a unified way of accessing data across every major exchange without requiring changes to the data format.

CCXT Example

In our first example, we will use the CCXT Python library to access the ticker data from HitBTC. Fetching all individual tickers from the exchange can take up to 5 minutes when using CCXT. Since there are hundreds of markets and we need to abide by the rate limits in place by the exchange, CCXT will automatically throttle the requests to make sure we don’t go over those limits.

Install CCXT

Let’s start by installing the CCXT library for Python.

pip install ccxt

Example

After installing CCXT, we can start building our script. A basic ticker script is outlined below. The script will access the live order book ticker for each individual asset pair. Once the data is retrieved, it will calculate the price of each asset in terms of USDT.

Note: This script will take up to 5 minutes to run, that is completely expected.

import ccxt hitbtc = ccxt.hitbtc() ticker_data = [] # fetch the BTC/USDT ticker for use in converting assets to price in USDT bitcoin_ticker = hitbtc.fetch_ticker('BTC/USDT') # calculate the ticker price of BTC in terms of USDT by taking the midpoint of the best bid and ask bitcoinPriceUSDT = (float(bitcoin_ticker['info']['ask']) + float(bitcoin_ticker['info']['bid'])) / 2 # fetch the tickers for each asset on HitBTC # this will take as long as 5 minutes for trading_pair in hitbtc.load_markets(): base = trading_pair.split('/')[0] quote = trading_pair.split('/')[1] if quote == 'BTC': pair_ticker = hitbtc.fetch_ticker(trading_pair) pair_ticker['base'] = base ticker_data.append(pair_ticker) prices = [] # create the price tickers for each asset, removing unnecessary data for ticker in ticker_data: price = {} price['symbol'] = ticker['base'] price['price'] = ((float(ticker['info']['ask']) + float(ticker['info']['bid'])) / 2) * bitcoinPriceUSDT prices.append(price) # additional processing is required for assets without BTC pairs # additional processing is required to calculate the 24-hour price change

Results

After running this script, the results in “prices” will be in the following format.

[ { "symbol": "LTC", "price": "60.683353731" }, { "symbol": "DASH", "price": "69.435138045" }, ... ]