CCXT Crypto Exchange Order Book Data [Example Tutorial]

network-3443540_1280.jpg

Whether you’re creating a personal trading bot or developing a full trading service which will support thousands of users, exchange order book data will be at the top of your mind. It is one of the first elements that are required to successfully evaluating the market and executing a strategy.

Why is the order book important?

orderbook-new-copy-e1532630456792.png

The order book specifies all the available orders which are currently open on an exchange. Each order represents a person (or multiple people) who is looking to buy or sell an asset at the given price.

When trading, you can either place your own open order on the exchange for someone else to take, or you can take an available order on the exchange. Whichever strategy you use, it’s important to have the necessary information to make that decision.

How do we access an exchange’s order book?

One way to access the order book from an exchange is to directly request this information from the exchange. You can do this using CCXT by following these examples.

CCXT Example - Individual Pair

Our first example will request the current order book for the BTC/USDT trading pair on the Binance exchange. When using the CCXT library, it is only possible to access a single trading pair at a time. That means we won’t be able to fetch the entire order book across every trading pair, but need to systematically pull the order book for each individual pair.

Install CCXT

Before we can start, let’s install the CCXT python library.

pip install ccxt 

Example

Once installed, we are ready to write our first python script which will access the BTC/USDT order book data on Binance.

import ccxt

# retrieve data for the BTC/USDT pair on Binance
binance = ccxt.binance()
orderbook = binance.fetch_order_book('BTC/USDT')

Results

The request will return the bid and ask orders which are currently available on the exchange for the BTC/USDT trading pair.

The first element in each order is the price of the order in terms of the quote currency (in this case USDT). The second element in the order is the quantity of the order on the exchange in terms of the base currency (in this case BTC).

{
  'bids': [
    [8724.77, 0.149594],
    [8724.11, 2.537818],
    [8724.08, 0.030605],
    ...
    ],
  'asks': [
    [8725.61, 0.055265],
    [8725.7, 0.028131],
    [8725.81, 0.116984],
    ...
    ]
}

CCXT Example - Aggregated Pairs

Now that we have covered how to access the order book for a single trading pair using CCXT, we want to expand our approach to collecting order book data for every trading pair on the exchange.

Unfortunately, collecting this amount of data is a bit tricky for CCXT. The data for each market pair must be requested on an individual basis from Binance. Sending all of these requests can take up to 5 minutes or more since Binance has rate limits for how quickly you can access data.

import ccxt

binance = ccxt.binance()
aggregated_orderbook_data = []

# get the available markets for Binance
binance_markets = binance.load_markets()

# fetch the order books for each asset on Binance
# fetching all of these order books will take as long as 5 minutes
for pair in binance_markets:
    pair_orderbook = binance.fetch_order_book(pair)
    pair_orderbook['symbol'] = pair
    aggregated_orderbook_data.append(pair_orderbook)

Results

Each request we send will return the bid and ask depth market data for the individual trading pair. To provide a convenient way to identify the order books, we’ve gone ahead and added a key in each order book object to include the symbol. That way we don’t lose track of which data belongs to which market pair.

The result of the above example code will return the aggregated bid and ask orders for each trading pair available on the exchange.

[{
  'symbol': 'BTC/USD',
  'bids': [
    [8724.77, 0.149594],
    [8724.11, 2.537818],
    [8724.08, 0.030605],
    ...
    ],
  'asks': [
    [8725.61, 0.055265],
    [8725.7, 0.028131],
    [8725.81, 0.116984],
    ...
    ]
},{
  'symbol': 'LTC/BTC',
  'bids': [
    [8724.77, 0.149594],
    [8724.11, 2.537818],
    [8724.08, 0.030605],
    ...
    ],
  'asks': [
    [8725.61, 0.055265],
    [8725.7, 0.028131],
    [8725.81, 0.116984],
    ...
    ]
}]

Note: Use caution when accessing excessive data from the exchange. Continuously requesting order book data can clog up your trading service since it will restrict the number of requests you can make to perform other actions on the exchange.

Shrimpy Example - Individual Pair

Due to the limitations with CCXT, our team has developed a professional grade alternative. The Shrimpy APIs don’t restrict access to data like CCXT. The following example demonstrates how you can access the order book for a single asset using the Shrimpy Developer APIs.

Note: In order to finish these examples, please sign up for a free account with the Shrimpy Crypto Data & Trading APIs. Once you log into your Shrimpy account, generate new API keys by selecting the option to “Create Api Master Key”. These keys will be used in the following scripts.

Install Shrimpy

Before jumping into our Shrimpy example, let’s start by installing the Shrimpy Python Library.

pip install shrimpy-python

Example

Now that we’ve installed the library, we can write our first script.

import shrimpy

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

client = shrimpy.ShrimpyApiClient(public_key, secret_key)

orderbooks = client.get_orderbooks(
    'bittrex',  # exchange
    'XLM',      # base_symbol
    'BTC',      # quote_symbol
    100         # limit of how much depth to return on each side (bid & ask)
)

Returns

The Shrimpy APIs return a well-organized list of order book data which includes the base symbol, quote symbol, exchange, and each of the available orders on the exchange precisely labeled.

[{
  "baseSymbol": "XLM",
  "quoteSymbol": "BTC",
  "orderBooks": [{
    "exchange": "Bittrex",
    "orderBook": {
      "asks": [
        {
          "price": "0.00002585",
          "quantity": "1891.1316431"
        },
        {
          "price": "0.00002594",
          "quantity": "35200"
        },
        ...
      ],
      "bids": [
        {
          "price": "0.00002577",
          "quantity": "774.92250177"
        },
        {
          "price": "0.00002576",
          "quantity": "3509.07031022"
        },
        ...
      ]
    }
  }]
}]

Shrimpy Example - Aggregated Pairs

Now, Shrimpy really shines when it comes to aggregating data across asset pairs or even exchanges. The following examples will highlight some of these use cases.

Retrieve the order book for every pair on Bittrex.

import shrimpy

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

client = shrimpy.ShrimpyApiClient(public_key, secret_key)

orderbooks = client.get_orderbooks(
    'bittrex'  # exchange
)

You can quickly see how the difference between this last example and the previous Shrimpy example, which only requested data for one trading pair, is essentially the same. All we did was remove the inputs for quote currency, base currency, and limit. Everything else remained exactly the same and retrieved all the data we needed in a single request.

This prevents us from spending 5 minutes collecting data for each trading pair individually.

Continuing from our last example, there are still more ways we can configure which data we collect from the Shrimpy APIs.

Most times, we only need a subset of the order books available on the exchange. As a result, we might want to know all of the base or quote currencies which connect to a specific asset. In these cases, we can request data for all pairs connected to a specific base or quote currency.

Access the order book for every pair on Bittrex which uses BTC as the quote currency.

import shrimpy

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

client = shrimpy.ShrimpyApiClient(public_key, secret_key)

orderbooks = client.get_orderbooks(
    'bittrex', # exchange
    None,      # base currency
    'BTC'      # quote currency
)

Notice how we specify “None” for the base currency. That means we will retrieve the order books for all trading pairs which have a quote currency of “BTC”.

Lastly, Shrimpy is not only contained to a single exchange. It’s even possible to request data across exchanges. This can be done by requesting trading pairs across multiple exchanges in the request.

An example request that pulls data across exchanges in a single request.

GET https://dev-api.shrimpy.io/v1/orderbooks?exchange=bittrex,binance,kucoin&baseSymbol=XLM&quoteSymbol=BTC&limit=10

Notice how we specify multiple exchanges. The result for this query will return the order book for the XLM/BTC trading pair on each of the 3 specified exchanges.

Returns

The format we get back is consistent whether we request order book data for a single trading pair or multiple pairs across exchanges.

[{
  "baseSymbol": "XLM",
  "quoteSymbol": "BTC",
  "orderBooks": [{
    "exchange": "Bittrex",
    "orderBook": {
      "asks": [
        {
          "price": "0.00002585",
          "quantity": "1891.1316431"
        },
        {
          "price": "0.00002594",
          "quantity": "35200"
        },
        ...
      ],
      "bids": [
        {
          "price": "0.00002577",
          "quantity": "774.92250177"
        },
        {
          "price": "0.00002576",
          "quantity": "3509.07031022"
        },
        ...
      ]
    }
  }]
},{
  "baseSymbol": "XRP",
  "quoteSymbol": "BTC",
  "orderBooks": [{
    "exchange": "Bittrex",
    "orderBook": {
      "asks": [
        {
          "price": "0.00001485",
          "quantity": "2891.1316431"
        },
        {
          "price": "0.00001494",
          "quantity": "45200"
        },
        ...
      ],
      "bids": [
        {
          "price": "0.00001477",
          "quantity": "874.92250177"
        },
        {
          "price": "0.00001476",
          "quantity": "4509.07031022"
        },
        ...
      ]
    }
  }]
},
...
]

Note: Shrimpy only requires a single request to instantly access order book data across every major exchange. This reduces the number of requests you need to send to exchanges and can drastically improve your data access.

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