CCXT Live Crypto Exchange Price Ticker [Example Tutorial]

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"
  },
  ...
]

Note: These prices are in terms of USDT. Since HitBTC does not have a BTC/USD pair, we could only get as close as possible by calculating the price in terms of USDT. We have also excluded calculations for assets which do not have BTC pairs on HitBTC.


CCXT Pros

  • Open source.

  • Directly communicates with exchanges to retrieve the data.

  • Can customize and manage your own infrastructure.

CCXT Cons

  • Inconsistent data across exchanges.

  • Takes minutes to aggregate data across every market pair.

  • Rate limits are difficult to navigate.

  • Requires significant infrastructure to scale an application to thousands of people.

  • Needs significant processing and additional code to calculate desired data.

Shrimpy Example

Throughout the CCXT script, we were able to highlight major pain points in how CCXT manages data from exchanges. In our next example, we will eliminate these pain points by using Shrimpy. This will clean up the way we generate a live ticker which includes every access across an exchange.

Install Shrimpy

To use Shrimpy, start by installing the Python Shrimpy Library. If you are developing in Node.js, feel free to follow along using the Node.js Shrimpy Library.

pip install shrimpy-python

Example

Once the Shrimpy library has been installed, we can begin writing our script to get the ticker data. With Shrimpy, this requires only a few lines of code in order to process every asset on the exchange of your choice.

Before we begin the script, please notice the requirement for a public and secret API key. These keys are provided when you sign up for a free account with the Shrimpy Crypto Data & Trading APIs. After logging into your Shrimpy account, Master API keys can be generated by selecting the option to “Create Api Master Key”. Please sign up and generate your Shrimpy keys before continuing.

import shrimpy

# access your public and secret Shrimpy Master Keys
# by signing up for free at https://developers.shrimpy.io/
public_key = 'bea8edb348af226...'
secret_key = 'df84c39fb49026dcad9d99...'

client = shrimpy.ShrimpyApiClient(public_key, secret_key)

ticker = client.get_ticker('kucoin')

Results

The results that are returned for the Shrimpy ticker include the name of the currency, symbol, price in USD and BTC, along with the 24h change.

[
  {
    "name": "Bitcoin",
    "symbol": "BTC",
    "priceUsd": "3700.0089335",
    "priceBtc": "1",
    "percentChange24hUsd": "4.191224354581092",
    "lastUpdated": "2018-12-19T22:51:13.000Z"
  },
  {
    "name": "Ethereum",
    "symbol": "ETH",
    "priceUsd": "100.114205389399",
    "priceBtc": "0.027057825",
    "percentChange24hUsd": "5.432113558652999",
    "lastUpdated": "2018-12-19T22:51:13.000Z"
  },
  ...
]

Note: Notice how Shrimpy only needs a single request to access every ticker on an exchange. There are no aggregation steps, recalculation steps, or missing steps to calculate 24h performance. It’s simple and elegant.


Shrimpy Pros

  • Conveniently aggregate data across exchanges.

  • Instant data access with one request (does not take 5 minutes like the CCXT example).

  • Scalable to millions of users without needing any servers.

  • Data is consistent across every exchange.

  • High rate limits (1,000+ per minute).

Shrimpy Cons

  • You need a free Shrimpy Developer API key.

  • Supports 17 of the top exchanges, but not every exchange.

Try out the Shrimpy Trading & Data APIs today. It’s the easiest way to get started collecting data across every major exchange. Collect historical market data, access real-time websockets, execute advanced trading strategies, and manage an unlimited number of users.

Don’t forget to follow us on Twitter and Facebook for updates, and ask any questions to our amazing, active community on Telegram.

The Shrimpy Team

Additional Reads

CCXT Crypto Exchange Order Book Data [Example Tutorial]

Shrimpy vs CCXT: The Case for Centralization in a Decentralized Ecosystem

Trading Cryptocurrencies through APIs

CCXT Alternative - The Pro Cryptocurrency Trading API