Python Scripts for Crypto Trade Data Websockets

Collecting trade data from crypto exchanges is a nightmare. It takes months to set up the infrastructure and then countless man-hours to maintain the infrastructure over time.

To combat this prevalent problem, Shrimpy has launched a set of Universal Crypto Exchange APIs, where developers can access live trade data from every major exchange.

Connecting to the websocket APIs for the first time takes under 5 minutes. By the end of this article, you will have a fully functional connection to crypto exchanges for accessing live trade data.

So, let’s not waste any more time!

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

After subscribing to the trade websocket, the latest trades will immediately start streaming through the websocket. These represent the individual trades that are being executed on the trading pair that was specified in the subscription request.

The format of the data that is returned through the websocket looks like the following.

{
    "exchange": "coinbasepro",
    "pair": "ltc-btc",
    "channel": "trade",
    "snapshot": false,
    "sequence": 0,
    "content": [
        {
            "id": 138368370,
            "price": "0.0130650000000000",
            "quantity": "2.0300000000000000",
            "time": "2019-05-31T20:25:14.000Z",
            "btcValue": 0.02652195,
            "usdValue": 224.16218805254,
            "takerSide": "buyer"
        },
        {
            "id": 138368371,
            "price": "0.0130650000000000",
            "quantity": "1.0000000000000000",
            "time": "2019-05-31T20:25:14.000Z",
            "btcValue": 0.013065,
            "usdValue": 110.42472317859,
            "takerSide": "seller"
        },
        ...
    ]
}

Snapshot

The trade websockets do not support any snapshot functionality at this time. Each individual trade data point that is sent through the websocket represents a single trade that was executed. This field can be ignored for the trade websocket.

ID

The unique identifier for the trade. Every trade is unique, so there will not be duplicates of this ID.

Price

The exact price of the trade that was executed. The price of the trade is in terms of the quote currency. In this example, the quote currency is BTC, so the price would be in terms of Bitcoin.

Quantity

The size of the trade. Quantities are in terms of the base currency. In this example, the base currency is LTC, so a quantity of 1 would mean the trade was executed with 1 Litecoin.

Time

The exact timestamp for when the trade was executed.

BTCValue

The value of the trade in terms of Bitcoin. This value is calculated by Shrimpy, not by the exchange.

USDValue

The value of the trade in terms of USD. This value is calculated by Shrimpy, not by the exchange.

TakerSide

Taker side is either “buyer” or “seller”. What this tells us is which side of the market executed the trade. In this example, if “takerSide” is “seller”, that means the person who is selling Litecoin (the base currency) for Bitcoin (the quote currency) was the taker in the transaction. The taker is the person who crossed the bid-ask spread to execute the order.

Simple Script Example

In the following script, we will connect to the live trade websocket for the LTC/BTC trading pair on Coinbase Pro. Each time we receive a message through the websocket, we will print it out.

Notice that we are not manipulating the trade data in any way 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": "coinbasepro",
    "pair": "ltc-btc",
    "channel": "trade"
}

# 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 trade updates from every major exchange.

Notice that we didn’t share how to use the trade data with this Python script example. Since there are boundless opportunities for ways you can use trade data, we would recommend exploring the possibilities and coming up with what information matters most to you.

Conclusions

Connecting to cryptocurrency exchanges and streaming live trade 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.

historical trade data article segment.png

Historical Crypto Trade Data

Historical crypto trade data contains the individual trades that were executed by an exchange over time. Each historical trade contains information about the trading pair, the trade size, the exact time the trade was executed, and the price of the assets being bought or sold.


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