Skip to main content

Retrieve and analyze crypto order book data using a cryptocurrency API

In this tutorial, we will delve into the process of fetching and analyzing cryptocurrency order books using the Python programming language and CoinAPI.

Agenda๐Ÿ”ฅ:โ€‹

  1. Crypto order book basics: Understanding cryptocurrency trading order books.
  2. Fetching data: Using CoinAPI and Python to retrieve real-time crypto order book data.
  3. Analysis: Interpreting cryptocurrency API data for insights.
  4. Visualization: Displaying order book data with Pythonโ€™s Matplotlib.
  5. Metrics calculation: Computing key metrics from cryptocurrency API data.

CoinAPI: The ultimate cryptocurrency API for all your crypto data needs ๐Ÿš€โ€‹

Before diving into our guide, letโ€™s take a moment to introduce CoinAPI. CoinAPI offers easy access to a wide range of financial information, encompassing both live and historical data from various exchanges.

Whether youโ€™re looking for detailed trading data, OHLCV (Open, High, Low, Close, Volume), or specific event information, CoinAPI delivers. CoinAPI provides crypto order book data, making it one of the best crypto APIs available.

Additionally, our support for multiple data delivery methods, including REST and WebSocket, makes it highly versatile for developers creating trading algorithms or crypto market data visualizations.

Let's Begin ๐Ÿš€โ€‹

Harnessing the capabilities of CoinAPI, a robust cryptocurrency API, alongside Python, known for its versatility in data analysis and statistical computing, we can delve deeply into the nuances of the cryptocurrency market.

Weโ€™ll walk you through the steps to fetch and analyze crypto order book data in real-time, leveraging the comprehensive data provided by the cryptocurrency API.

Setting Up the API Requestโ€‹

To fetch real-time order book data, you'll first need an API key from CoinAPI website. Once you have it, you can proceed to retrieve the data using Python.

Fetching the data

import requests
import json

API_KEY = "YOUR_API_KEY_HERE"
url = f"https://rest.coinapi.io/v1/orderbooks/current?symbol=KRAKEN_SPOT_BTC_USD&apikey={API_KEY}"

response = requests.get(url)

if response.status_code == 200:
# The request was successful
data = response.json()

# Save the data to a file
with open("order-books-kraken-spot-btc-usd.json", "w") as file:
json.dump(data, file, indent=4)
print("Data saved to order-books-kraken-spot-btc-usd.json")
else:
# Handle error cases
print(f"Error: Status code {response.status_code}")
print(response.text)

Response

{
"symbol_id": "KRAKEN_SPOT_BTC_USD",
"time_exchange": "2023-11-24T08:26:46.6928657Z",
"time_coinapi": "2023-11-24T08:26:46.6928657Z",
"asks": [
{
"price": 37549.1,
"size": 19.23618731
},
{
"price": 37549.9,
"size": 0.08242079
},
/// other entries omitted for brevity
],
"bids": [
{
"price": 37549.0,
"size": 0.136004
},
{
"price": 37545.1,
}
/// other entries omitted for brevity
]
}

Data Analysis and Metricsโ€‹

Upon data retrieval, the next step involves data analysis using Python.

The obtained data typically represents a snapshot of buy and sell orders in the market at a specific moment. This data consists of two key components: bids and asks. Bids signify the price levels at which buyers are willing to acquire an asset, while asks represent the price levels at which sellers are willing to sell it. The difference between the highest bid and the lowest ask is referred to as the spread.

Here is a comprehensive list of common metrics and analytical procedures:

  • Order Book Depth - visualize the liquidity available at various price levels.

  • Order Imbalance - analyze the disparity between bids and asks to gain insights into market sentiment.

  • Price Levels - identify significant price levels characterized by substantial volumes of bids or asks.

  • Market Spread - compute the current bid-ask spread as an indicator of market liquidity.

Order Book Depth Visualizationโ€‹

In this Python code, we demonstrate how to visualize the order book depth of a cryptocurrency trading pair using Matplotlib.

import json
import matplotlib.pyplot as plt

# Function to read order book data from a JSON
def read_order_book_data(file_path):
with open(file_path, 'r') as file:
order_book_data = json.load(file)
return order_book_data

file_path = 'order-books-kraken-spot-btc-usd.json'

# Read order book data from the file we've obtained in the previous step
order_book_data = read_order_book_data(file_path)

# Function to visualize order book depth chart
def visualize_order_book_depth(order_book_data):
bids = order_book_data["bids"]
asks = order_book_data["asks"]

bid_prices = [entry["price"] for entry in bids]
bid_sizes = [entry["size"] for entry in bids]

ask_prices = [entry["price"] for entry in asks]
ask_sizes = [entry["size"] for entry in asks]

plt.figure(figsize=(10, 6))
plt.plot(bid_prices, bid_sizes, label="Bids (buyers)", color="green")
plt.plot(ask_prices, ask_sizes, label="Asks (sellers)", color="red")
plt.xlabel("Price")
plt.ylabel("Size")
plt.title("Order Book Depth")
plt.legend()
plt.show()

# Visualize the order book depth
visualize_order_book_depth(order_book_data)

The following visualization is the result of the Python script demonstrated above.

order book depth

Order Book Depth Visualization:

  • is a measure of the quantity of buy and sell orders available for a particular financial asset
  • it provides insight into market liquidity and the willingness of traders to buy or sell at different prices
  • may be used for trading decisions, as it reveals potential price trends

Order Imbalance, Price Levels, Market Spreadโ€‹

In this Python code, we explore order imbalance, significant bids & asks, market spread metrics for a BTC/USD trading pair.

import json

# Function to read order book data from a JSON
def read_order_book_data(file_path):
with open(file_path, 'r') as file:
order_book_data = json.load(file)
return order_book_data

file_path = 'order-books-kraken-spot-btc-usd.json'

# Read order book data from the file we've obtained in the previous step
order_book_data = read_order_book_data(file_path)

# Function to calculate order imbalance
def calculate_order_imbalance(order_book_data):
bids = sum(entry["size"] for entry in order_book_data["bids"])
asks = sum(entry["size"] for entry in order_book_data["asks"])

order_imbalance = (bids - asks) / (bids + asks)
return order_imbalance

# Function to identify significant price levels
def identify_significant_price_levels(order_book_data, threshold=50):
bids = order_book_data["bids"]
asks = order_book_data["asks"]

significant_bids = [entry for entry in bids if entry["size"] > threshold]
significant_asks = [entry for entry in asks if entry["size"] > threshold]
return significant_bids, significant_asks

# Function to calculate market spread
def calculate_market_spread(order_book_data):
best_bid = order_book_data["bids"][0]["price"]
best_ask = order_book_data["asks"][0]["price"]

spread = best_ask - best_bid
return spread

# Common metrics calculation
order_imbalance = calculate_order_imbalance(order_book_data)
significant_price_threshold = 20
significant_bids, significant_asks = identify_significant_price_levels(order_book_data, significant_price_threshold)
spread = calculate_market_spread(order_book_data)

print("******************************")
print("Order Imbalance:", order_imbalance)
print("***************************")
print("Significant Bids:", significant_bids)
print("***************************")
print("Significant Asks:", significant_asks)
print("***************************")
print("Market Spread:", spread)
print("***************************")

Result

> ******************************
> Order Imbalance: -0.012400502029283922
> ***************************
> Significant Bids: [{'price': 37440.7, 'size': 24.73909889}, {'price': 36451.0, 'size': 25.0}]
> ***************************
> Significant Asks: [{'price': 37749.0, 'size': 25.0}, {'price': 37840.0, 'size': 24.08119208}, {'price': 38000.0, 'size': 29.60579175}, {'price': 39000.0, 'size': 21.75395535}, {'price': 40000.0, 'size': 58.28620718}]
> ***************************
> Market Spread: 0.09999999999854481
> ***************************

Calculated metrics:

  • order_imbalance - it measures the difference between the total volume of buy orders and sell orders. It provides insights into the overall market sentiment by indicating whether there is an excess of buying or selling interest.
  • significant_price_threshold is a predetermined value (set to 20 in this case) used to identify price levels in the order book that are considered significant. It helps traders focus on specific price points that may have a more substantial impact on the market.
  • significant_bids and significant_asks are lists of price levels derived from the order book data that meet or exceed the significant_price_threshold. These levels are typically associated with higher trading activity and may be seen as key support and resistance levels.
  • spread is a metric that calculates the difference between the best bid and best ask prices. It represents the cost of executing a market order and is a fundamental factor for traders to consider when entering or exiting positions.

Summaryโ€‹

This tutorial has equipped you with the knowledge and tools necessary to fetch and analyze cryptocurrency order book data using CoinAPI and Python. You've learned how to access real-time order book information, visualize order book depth and calculate important metrics.

Armed with this understanding, you are now better prepared to explore and navigate the cryptocurrency markets, enabling you to make more informed trading decisions and develop data-driven trading strategies.

Remember that the cryptocurrency market is highly dynamic, so continuous monitoring and analysis are essential for staying ahead in this exciting and rapidly evolving space.

Happy trading! ๐Ÿš€๐Ÿ“ˆ๐Ÿ’ฐ