Skip to main content

A fast orderbook implementation, in C, for Python

Project description

Orderbook

License Python PyPi coverage-lines coverage-functions

A fast L2/L3 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())

Main Features

  • Sides maintained in correct order
  • Can perform orderbook checksums
  • Supports max depth and depth truncation

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 as well as the other performance tests in perf/


Changelog

0.1.1

  • Feature: Checksum support for orderbooks
  • Feature: FTX checksum support
  • Feature: Kraken checksum support
  • Perf: Use CRC32 table to improve performance of checksum code

0.1.0 (2021-01-18)

  • Minor: Use enums to make code more readable
  • Bugfix: Add manifest file to ensure headers and changes file are included in sdist builds
  • Feature: 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.1.tar.gz (12.2 kB view details)

Uploaded Source

Built Distributions

order_book-0.1.1-cp39-cp39-manylinux2010_x86_64.whl (58.4 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

order_book-0.1.1-cp39-cp39-manylinux1_x86_64.whl (58.4 kB view details)

Uploaded CPython 3.9

order_book-0.1.1-cp39-cp39-macosx_10_9_x86_64.whl (24.3 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

order_book-0.1.1-cp38-cp38-manylinux2010_x86_64.whl (59.5 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

order_book-0.1.1-cp38-cp38-manylinux1_x86_64.whl (59.5 kB view details)

Uploaded CPython 3.8

order_book-0.1.1-cp38-cp38-macosx_10_9_x86_64.whl (25.0 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

order_book-0.1.1-cp37-cp37m-manylinux2010_x86_64.whl (57.4 kB view details)

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

order_book-0.1.1-cp37-cp37m-manylinux1_x86_64.whl (57.4 kB view details)

Uploaded CPython 3.7m

order_book-0.1.1-cp37-cp37m-macosx_10_9_x86_64.whl (24.7 kB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

File details

Details for the file order_book-0.1.1.tar.gz.

File metadata

  • Download URL: order_book-0.1.1.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0.post20201207 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for order_book-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ae302cbc8cb257e33ca929b2eb05bf5a8e7b7961e1e164cb84d0dff4fb4d06ad
MD5 3b370d4c6c601be1dda68358311c3540
BLAKE2b-256 4c9812cff0b0f948a3cdea9d337219d2e1be40fc24b81f6be209327db7c5929e

See more details on using hashes here.

Provenance

File details

Details for the file order_book-0.1.1-cp39-cp39-manylinux2010_x86_64.whl.

File metadata

  • Download URL: order_book-0.1.1-cp39-cp39-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 58.4 kB
  • Tags: CPython 3.9, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0.post20201207 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for order_book-0.1.1-cp39-cp39-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 554d60bc920c36bcbd20765444d7bfc5392d0eca01d6fcab5c19d9ca34a563fd
MD5 b37fe12d48ebeff3b6af44395e4c7f79
BLAKE2b-256 9342f34f3d88d224e18de0d897b8499f2a1c13cccab3ed0c1b1c6bb056c1a6d7

See more details on using hashes here.

Provenance

File details

Details for the file order_book-0.1.1-cp39-cp39-manylinux1_x86_64.whl.

File metadata

  • Download URL: order_book-0.1.1-cp39-cp39-manylinux1_x86_64.whl
  • Upload date:
  • Size: 58.4 kB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0.post20201207 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for order_book-0.1.1-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 316036004cf248d47b343841dd5cf8e48edada7296df433e03fead13ce1a0fa5
MD5 f1314baa2e04c894a70fed5c76251006
BLAKE2b-256 511e9f4811851201b80d90c221cc42593a894097c2990ab72cc07665922f0ba3

See more details on using hashes here.

Provenance

File details

Details for the file order_book-0.1.1-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: order_book-0.1.1-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 24.3 kB
  • Tags: CPython 3.9, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0.post20201207 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for order_book-0.1.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 5faaa243191cae23f646df6be4983664f358d6c9a74bc557e98459e744297bf5
MD5 fa1a80c2d9aa410d6dd26f846233ec3b
BLAKE2b-256 598c43f5f89969a4a41c9a394c27675ea49c2874a3b3fb369ef17ccccfed61a4

See more details on using hashes here.

Provenance

File details

Details for the file order_book-0.1.1-cp38-cp38-manylinux2010_x86_64.whl.

File metadata

  • Download URL: order_book-0.1.1-cp38-cp38-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 59.5 kB
  • Tags: CPython 3.8, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0.post20201207 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for order_book-0.1.1-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 5a80bff736121f7547b99564630cbeb693fca7adc53333215a362ec8c613da56
MD5 0cde5b37550be4aaab867ba65f120043
BLAKE2b-256 6344fdb5c25085e9bbcad0a6491bfd0b89b788235cc0b967f6de2a68b9b920c1

See more details on using hashes here.

Provenance

File details

Details for the file order_book-0.1.1-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: order_book-0.1.1-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 59.5 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0.post20201207 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for order_book-0.1.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e3c39c72ceb505d3f9fa0653540b5342966d3234c3214b308d404b7315942db7
MD5 28d88f40cd647a5ef4420706dbb98ffa
BLAKE2b-256 e1a0484405f3973dd3bdad048466a6bda935cc9914bbcd1146c90033bef42302

See more details on using hashes here.

Provenance

File details

Details for the file order_book-0.1.1-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: order_book-0.1.1-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 25.0 kB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0.post20201207 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for order_book-0.1.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 88c7699a71188aa96941fc0b1b2c433b1c78d3cafa4d8fc36abcc7caf61986ae
MD5 7536586e8ba588102d43716102160517
BLAKE2b-256 f887e65719b627c05cea0a5cd709314ec7bc77a79c74b8b16a7eb546b1614678

See more details on using hashes here.

Provenance

File details

Details for the file order_book-0.1.1-cp37-cp37m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: order_book-0.1.1-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 57.4 kB
  • Tags: CPython 3.7m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0.post20201207 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for order_book-0.1.1-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 aaee4a53661146341a38c81850ce98f56f56bf112a4cf8d862eefcdd2ae3f9b9
MD5 87f9331448b3d6847e55d8136451ab80
BLAKE2b-256 7a026991dd8f9157a7fe2f9727d4b2f92e8f60fed98f91c27cb21225ce9ee22d

See more details on using hashes here.

Provenance

File details

Details for the file order_book-0.1.1-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: order_book-0.1.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 57.4 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0.post20201207 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for order_book-0.1.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2fa10aa194ac42574c0e0dbd71b290e97252638ac66b732557a501875667970c
MD5 8513bfd5e70006063846cd25b850ebd8
BLAKE2b-256 964440c0b0acc2e5c26de0f0eadfaa6e838b4fb057a46d722df0ad8ca245a012

See more details on using hashes here.

Provenance

File details

Details for the file order_book-0.1.1-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: order_book-0.1.1-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 24.7 kB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0.post20201207 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for order_book-0.1.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 11f46bb9d8d90801215c019a22d672b731c38bfe55ebba191ab8fda8f6fb344f
MD5 158448b59861cb59512a7d086c9a2dfe
BLAKE2b-256 9db94427da2bb52d62dad37cb921204f6fa0530688e8c75524e00dcf9a28075b

See more details on using hashes here.

Provenance

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