Crypto Exchange Order Book Websockets for Developers [Python Scripts]

We’re coming to you live from every major cryptocurrency exchange.

If time is money, that explains why everyone is always looking to maximize their time. Engineers are no different. They want the fastest tools, most efficient data sources, and reliable infrastructure.

Access to cryptocurrency data has historically been slow. Many data sources like CoinMarketCap update on a 5 minute time interval. For most developers, this is unacceptable.

That’s where Shrimpy comes to the rescue.

Shrimpy is fast. So fast you can’t even blink your eyes without missing how fast data is processed from exchanges. After using Shrimpy for the first time, people say things like, “man those socket streams are FAST” (direct quote from a Shrimpy user).

In this tutorial, we will show you how to access our blazing-fast websocket streams in under 5 minutes. That’s right, our fast websockets deserve a fast tutorial.

So, before we get started, complete these 3 steps:

  1. Sign up for the Shrimpy Developer APIs.

  2. Install the Shrimpy Python Library.

  3. Create an API Key and subscribe.

Data Format

Before we get to example scripts, we must first understand the data we are collecting. In the next two sections, we will cover “order book snapshots” and “order book updates”. These two messages are similar in many ways, but they serve different purposes.

Order Book Snapshots

Order book snapshots are the first message you will receive through the websocket after you subscribe. This snapshot will contain the entire order book for a single trading pair.

Notice there is an element specifying “snapshot" is “true”. This is how we know this message is a snapshot of the entire orderbook.

{
    "exchange": "coinbasepro",
    "pair": "ltc-btc",
    "channel": "orderbook",
    "snapshot": true,
    "sequence": 6784322,
    "content": {
        "asks": [
            {
                "price": "59.782355",
                "quantity": "3.456722"
            },
            {
                "price": "60.782355",
                "quantity": "3.456722"
            },
            ...
        ],
        "bids": [
            {
                "price": "58.782355",
                "quantity": "3.456722"
            },
            {
                "price": "56.782355",
                "quantity": "3.456722"
            },
            ...
        ]
    }
}

Order Book Update

After the initial order book snapshot, updates will immediately start streaming through the websocket. Each of these updates should be applied to the original snapshot that was received. That way the local order book you maintain is always up to date with the latest values.

When we receive an update for a specific “price”, we should replace the old “quantity” with the new “quantity”. If an update specifies a “price” that we don’t currently have in our local copy of the order book, we should update our order book to contain that price and quantity.

{
    "exchange": "coinbasepro",
    "pair": "ltc-btc",
    "channel": "orderbook",
    "snapshot": false,
    "sequence": 6784322,
    "content": {
        "asks": [
            {
                "price": "59.782355",
                "quantity": "3.456722"
            },
            {
                "price": "61.782355",
                "quantity": "3.56722"
            },
            ...
        ],
        "bids": [
            {
                "price": "58.782355",
                "quantity": "3.61234"
            },
            {
                "price": "56.782355",
                "quantity": "1.34234"
            },
            ...
        ]
    }
}

Simple Script Example

In the following script, we will connect to the order book websocket for the ETH/BTC trading pair on Binance. Each time we receive a message through the websocket, we will print it out.

Notice that we are not maintaining the order book with this script. That means after printing each message, we don’t do anything with the message. This is for example purposes only.

import shrimpy

# sign up for https://developers.shrimpy.io/ to get your API key
public_key = '...'
secret_key = '...'

# This is a sample handler, it simply prints the incoming message to the console
def error_handler(err):
    print(err)

# This is a sample handler, it simply prints the incoming message to the console
def handler(msg):
    print(msg)

api_client = shrimpy.ShrimpyApiClient(public_key, secret_key)
raw_token = api_client.get_token()
client = shrimpy.ShrimpyWsClient(error_handler, raw_token['token'])

subscribe_data = {
    "type": "subscribe",
    "exchange": "binance",
    "pair": "eth-btc",
    "channel": "orderbook"
}

# Start processing the Shrimpy websocket stream!
client.connect()
client.subscribe(subscribe_data, handler)

# Once complete, stop the client
client.disconnect()

That’s it! You now have access to streaming live order book updates from every major exchange.

Notice that we didn’t share how to properly maintain an order book with this Python script example. Since the strategies for maintaining an order book will depend on your specific use case, we would recommend investigating different options and implementing the version that works best for you.

Conclusions

Connecting to cryptocurrency exchanges and streaming live order book data is simple with the Shrimpy developer APIs. Every exchange can be accessed in the same way since Shrimpy does the heavy lifting when it comes to collecting data, processing updates, and converting all data to a consistent format.

Reminder: Side effects of using the Shrimpy developer APIs include: (1) Completing projects ahead of schedule and under budget (2) Unintentionally mumbling the word “wow” under your breath while staring at data flashing on your screen (3) Forgetting what it was like to process exchange data before you started using Shrimpy.

market data engineer.png

Historical Order Book Snapshots

Access over 1 billion historical order book snapshots with the most powerful crypto data APIs.


About Shrimpy

Shrimpy’s Developer Trading API is a unified way to integrate trading functionality across every major exchange. Collect market data, access real-time websockets, execute advanced trading strategies, and manage an unlimited number of users.

Shrimpy Crypto Trading API: Shrimpy | Crypto Trading APIs for Developers

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

The Shrimpy Team