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 data structure, 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.2.1 (2021-03-29)

  • Bugfix: Invalid deallocation of python object

0.2.0 (2021-03-12)

  • Feature: Add branch prediction hints around error handling code
  • Bugfix: Fix regression from adding branch predictors
  • Bugfix: Fix error corner case when iterating twice on an empty dataset
  • Feature: Add contains function for membership test
  • Bugfix: Fix issues around storing L3 data
  • Feature: Enhance testing, add in L3 book test cases

0.1.1 (2021-02-12)

  • Feature: Checksum support for orderbooks
  • Feature: FTX checksum support
  • Feature: Kraken checksum support
  • Feature: OkEX/OKCoin 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.2.1.tar.gz (12.6 kB view details)

Uploaded Source

Built Distributions

order_book-0.2.1-cp39-cp39-manylinux2010_x86_64.whl (58.9 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

order_book-0.2.1-cp39-cp39-manylinux1_x86_64.whl (58.9 kB view details)

Uploaded CPython 3.9

order_book-0.2.1-cp39-cp39-macosx_10_9_x86_64.whl (24.5 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

order_book-0.2.1-cp38-cp38-manylinux2010_x86_64.whl (60.0 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

order_book-0.2.1-cp38-cp38-manylinux1_x86_64.whl (60.0 kB view details)

Uploaded CPython 3.8

order_book-0.2.1-cp38-cp38-macosx_10_9_x86_64.whl (25.3 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

order_book-0.2.1-cp37-cp37m-manylinux2010_x86_64.whl (57.8 kB view details)

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

order_book-0.2.1-cp37-cp37m-manylinux1_x86_64.whl (57.8 kB view details)

Uploaded CPython 3.7m

order_book-0.2.1-cp37-cp37m-macosx_10_9_x86_64.whl (25.0 kB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: order_book-0.2.1.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for order_book-0.2.1.tar.gz
Algorithm Hash digest
SHA256 194c5f1c2e8741f889c87a2ef87a5dfd763945a56ca10ee09e7948a2a040c2b1
MD5 54eeb8c7d08b4b5dd5c656e1276f408f
BLAKE2b-256 9d470e48e65473d124c4e63936e0db9d18a2ad675399ff56b6fc86895bc2b9c8

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.2.1-cp39-cp39-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 58.9 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.2.1-cp39-cp39-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 8ff55c1366b0db3034f425eafff4bf24e1d1a7c3d424a7ebfa931e63d6c21fe7
MD5 a294c4429797ecd8c2949d8c611ca722
BLAKE2b-256 082378cba093dfd090563d1353efee7a5ba58c65bbac8e78de308d8f6c2133b3

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.2.1-cp39-cp39-manylinux1_x86_64.whl
  • Upload date:
  • Size: 58.9 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.2.1-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f1b5886e9f144b348c948099e3720acf8530351cb4c7d205890534985571aaed
MD5 d279a2bd2b5c4678babff63bbbfe010a
BLAKE2b-256 2c0a0012f2fc3a79c0fbb7c2464c5edb0aee1999b651a047a5494e8cfe6499b6

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.2.1-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 24.5 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.2.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 be64a5806afca5c191246685c2eb1cb482c31ea9ac810bcb003084a17339a077
MD5 cbb7f8ed03a00ba145341d32ed03296f
BLAKE2b-256 d3db684efcda0abe9c919c7028629f9b8bb97739affe4d338df0743907b0c86b

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.2.1-cp38-cp38-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 60.0 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.2.1-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 1fbc53e3e70343fa0f05a36f59124d5c852c50d56c95f320691adb6b003c4e11
MD5 d2964d8e29b2caafd64ec5ea7f53b9e1
BLAKE2b-256 cbe01db8c26d3cd94eb25f3ef40636e355443874300b02df7a5cea737f939ccd

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.2.1-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 60.0 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.2.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5e2a37761a505479dceb61764d176388dbf8c07b8a25380c76043a2b81d1ae35
MD5 8506cf778bbfb38cf0ab766bd043007f
BLAKE2b-256 bc76a4081b2f1933d5ce1b2774323bb00d7d891a3a3e21d8cb590004a59e3b3a

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.2.1-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 25.3 kB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for order_book-0.2.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f539b20d794bb70ec6df72193e74e0da328b8872735d182bc7247a6f04649c6a
MD5 8ef82a980135b3d7f3677b768e24bce5
BLAKE2b-256 4dad99816a450440645454f7874ca8f91b9f87b15825230f1d5b21c376698713

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.2.1-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 57.8 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.2.1-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 1f803b56da69e02b3743abaab52f519aba4d5fd230391bf24d2a59d822dcbd83
MD5 df021a2db2dbdc96b5d51987c2108be5
BLAKE2b-256 2b2ab4e029491301a086517906b41ec954841660616ae7eb2c9aed805be7a7ad

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.2.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 57.8 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.2.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b8747e7ee44eeb526d32953a78e1f2f7b709b9b5c0de015fc4a7b035f63d5935
MD5 8a9b77fcf7813e539d7091895258641c
BLAKE2b-256 5ca95fde37773c5ef72a4161321936c9745615eca4ef1d65ab6672d62ff4724d

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.2.1-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 25.0 kB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for order_book-0.2.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 4ab069d1ce2bde6e1350f6e0ef5844bde117d7dddee06a4b5932a1a53dfc0ee2
MD5 21f57ea74c5d41bbaee0d8b0df443d6a
BLAKE2b-256 2898e4eba0c4ab93c5a66145e5e2df34fa237dd7277acbb285d5ac673ec9b7ae

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