Python Scripts for Historical Crypto Exchange Order Book Snapshots

market data engineer.png

There is a major issue in the cryptocurrency market. Countless exchanges are operating thousands of trading markets and pumping out billions of data points every day. Although the data is publicly available, nobody has collected, organized, and stored this data in a way that is easily accessible to developers.

Conveniently, Shrimpy is working to solve this problem with data sourced from Kaiko. Throughout this article, we will describe how developers can begin accessing historical market data to run backtests, analyze the market, and study trends.

The Shrimpy Developer APIs provide access to historical exchange order books. Snapshots of each order book are taken on a 1-minute interval. Using the Shrimpy Python Library, we will provide examples of how developers can access historical data with only a few lines of code.

 
order book btc.usdt.PNG

What is an exchange order book?

Simply put, the order book is the list of all open orders that are currently available on an exchange for a specific trading pair. An open order is essentially another investor saying they are willing to buy or sell an asset at a specific price.

 

Data Format

Each order book snapshot will have the following information:

  • Timestamp - The date and time the order book snapshot was taken.

  • Ask Prices - The 20 best asking prices on the order book at the time.

  • Bid Prices - The 20 best bid prices on the order book at the time.

  • Price - The exact price of the available order in terms of the quote currency.

  • Size - The size of the order available in terms of the base currency.

[
    {
        "time":"2020-03-02T09:00:58Z",
        "asks":[
            {
                "price":"8672.77",
                "size":"0.002738"
            },
            {
                "price":"8672.78",
                "size":"0.022015"
            },
            {
                "price":"8673.47",
                "size":"0.4"
            },
            ...
        ],
        "bids":[
            {
                "price":"8672.49",
                "size":"0.088572"
            },
            {
                "price":"8672.47",
                "size":"0.181144"
            },
            {
                "price":"8672.44",
                "size":"0.088572"
            },
            ...
        ]
    }
]

Each order book snapshot is specific to a single trading pair on a single exchange. Order book snapshots are not aggregated across exchanges or market pairs.

Single Snapshot - Single Trading Pair

A single snapshot can be retrieved by specifying “1” for the limit field when making the request for historical order books.

import shrimpy

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

client = shrimpy.ShrimpyApiClient(public_key, secret_key)

orderbooks = client.get_historical_orderbooks(
    'binance',                  # exchange
    'BTC',                      # base currency
    'USDT',                     # quote currency
    '2020-03-02T09:00:00.000Z', # start time
    '2020-03-02T09:05:00.000Z', # end time
    1                           # get only one snapshot (the first in time period)
)

with open('depth.txt', 'w') as file:
    file.write(json.dumps(orderbooks)) # use `json.loads` to do the reverse

Single Snapshot - Multiple Trading Pairs

Accessing data across exchanges is simple. Imagine you want to compare the historical snapshots of a specific trading pair across multiple different exchanges. Substituting different exchanges into the API request is as easy as a few small updates to our script.

import shrimpy

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

client = shrimpy.ShrimpyApiClient(public_key, secret_key)

exchanges = ["binance", "kucoin", "bittrex", "okex"]

orderbooks = {}

for exchange in exchanges:
    orderbooks[exchange] = client.get_historical_orderbooks( 
            exchange,                   # exchange
            'BTC',                      # base currency
            'USDT',                     # quote currency
            '2020-03-02T09:00:00.000Z', # start time
            '2020-03-02T09:05:00.000Z', # end time
            1                           # get only one snapshot (the first in time period)
        )

with open('depth.txt', 'w') as file:
    file.write(json.dumps(orderbooks)) # use `json.loads` to do the reverse

Multiple Snapshots - Multiple Trading Pairs

Developers don’t need to only access a single snapshot at a time. Increasing the “limit” for each request allows us to retrieve up to 1,000 consecutive order book snapshots for a single trading pair.

import shrimpy

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

client = shrimpy.ShrimpyApiClient(public_key, secret_key)

exchanges = ["binance", "kucoin", "bittrex", "okex"]

orderbooks = {}

for exchange in exchanges:
    orderbooks[exchange] = client.get_historical_orderbooks( 
            exchange,                   # exchange
            'BTC',                      # base currency
            'USDT',                     # quote currency
            '2020-03-02T06:00:00.000Z', # start time
            '2020-03-02T09:00:00.000Z', # end time
            100                         # get only one snapshot (the first in time period)
        )

with open('depth.txt', 'w') as file:
    file.write(json.dumps(orderbooks)) # use `json.loads` to do the reverse

Note: These are not production-grade scripts. They were put together as simple examples. Running these scripts will collect real data from the Shrimpy APIs and will charge data credits as a result. Please use caution when running these scripts.

Data Credit Cost

Order book snapshots cost 20 data credits for every 1,000 order book snapshots retrieved from Shrimpy. That means if you input a limit size of 100 and Shrimpy returns 100 order book snapshots for a single trading pair, that will cost 2,000 data credits. Every 100 credits cost $1.

Additional Good Reads

How to Make a Crypto Trading Bot Using Python

How to Download Cryptocurrency Candlestick Data from Exchanges

Arbitrage Scripts for Crypto Trading Bots

Script for Bitcoin Price Live Ticker (Using Websockets)


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