Skip to main content

A fast orderbook implementation, in C, for Python

Project description

Orderbook

License Python PyPi coverage-lines coverage-functions

A fast orderbook implementation, in C, for Python

Basic Usage

from decimal import Decimal

import requests
from order_book import OrderBook

ob = OrderBook()

# get some orderbook data
data = requests.get("https://api.pro.coinbase.com/products/BTC-USD/book?level=2").json()

ob.bids = {Decimal(price): size for price, size, _ in data['bids']}
ob.asks = {Decimal(price): size for price, size, _ in data['asks']}

# OR

for side in data:
    # there is additional data we need to ignore
    if side in {'bids', 'asks'}:
        ob[side] = {Decimal(price): size for price, size, _ in data[side]}


# Data is accessible by .index(), which returns a tuple of (price, size) at that level in the book
price, size = ob.bids.index(0)
print(f"Best bid price: {price} size: {size}")

price, size = ob.asks.index(0)
print(f"Best ask price: {price} size: {size}")

print(f"The spread is {ob.asks.index(0)[0] - ob.bids.index(0)[0]}\n\n")

# Data is accessible via iteration

print("Bids")
for price in ob.bids:
    print(f"Price: {price} Size: {ob.bids[price]}")


print("\n\nAsks")
for price in ob.asks:
    print(f"Price: {price} Size: {ob.asks[price]}")


# Data can be exported to a sorted dictionary
# in Python3.7+ dictionaries remain in insertion ordering, the
# dict returned by .to_dict() has had its keys inserted in sorted order
print("\n\nRaw asks dictionary")
print(ob.asks.to_dict())

Installation

The preferable way to install is via pip - pip install order-book. Installing from source will require a compiler and can be done with setuptools: python setup.py install.

Running code coverage

The script coverage.sh will compile the source using the -coverage CFLAG, run the unit tests, and build a coverage report in HTML. The script uses tools that may need to be installed (coverage, lcov, genhtml).

Running the performance tests

You can run the performance tests like so: python perf/performance_test.py. The program will profile the time to run for random data samples of various sizes as well as the construction of a sorted orderbook using live L2 orderbook data from Coinbase.

The performance of constructing a sorted orderbook (using live data from Coinbase) using this C library, versus a pure Python sorted dictionary library:

Library Time, in seconds
C Library 0.00021767616271
Python Library 0.00043988227844

The performance of constructing sorted dictionaries using the same libraries, as well as the cost of building unsorted, python dictionaies for dictionaries of random floating point data:

Library Number of Keys Time, in seconds
C Library 100 0.00021600723266
Python Library 100 0.00044703483581
Python Dict 100 0.00022006034851
C Library 500 0.00103306770324
Python Library 500 0.00222206115722
Python Dict 500 0.00097918510437
C Library 1000 0.00202703475952
Python Library 1000 0.00423812866210
Python Dict 1000 0.00176715850830

This represents a roughly 2x speedup compared to a pure python implementation, and in many cases is close to the performance of an unsorted python dictionary.

For other performance metrics, run performance_test.py


Changelog

0.1.0 (2021-01-18)

  • Use enums to make code more readable
  • Add manifest file to ensure headers and changes file are included in sdist builds
  • Add support for max depth and depth truncation

0.0.2 (2020-12-27)

  • Bugfix: Fix sorted dictionary arg parsing
  • Feature: Coverage report generation for C library
  • Bugfix: Fix reference counting in index method in SortedDict
  • Feature: New unit tests to improve SortedDict coverage
  • Feature: Modularize files
  • Feature: Add ability to set bids/asks to dictionaries via attributes or [ ]
  • Docs: Update README with simple usage example

0.0.1 (2020-12-26)

  • Initial Release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

order_book-0.1.0.tar.gz (8.7 kB view hashes)

Uploaded Source

Built Distributions

order_book-0.1.0-cp39-cp39-manylinux2010_x86_64.whl (49.9 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

order_book-0.1.0-cp39-cp39-manylinux1_x86_64.whl (49.9 kB view hashes)

Uploaded CPython 3.9

order_book-0.1.0-cp39-cp39-macosx_10_9_x86_64.whl (21.1 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

order_book-0.1.0-cp38-cp38-manylinux2010_x86_64.whl (49.5 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

order_book-0.1.0-cp38-cp38-manylinux1_x86_64.whl (49.5 kB view hashes)

Uploaded CPython 3.8

order_book-0.1.0-cp38-cp38-macosx_10_9_x86_64.whl (21.7 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

order_book-0.1.0-cp37-cp37m-manylinux2010_x86_64.whl (46.9 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

order_book-0.1.0-cp37-cp37m-manylinux1_x86_64.whl (46.9 kB view hashes)

Uploaded CPython 3.7m

order_book-0.1.0-cp37-cp37m-macosx_10_9_x86_64.whl (21.6 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page