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
# Note: bids/asks are iterators

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.4.0 (2021-09-16)

  • Feature: changes to code and setup.py to enable compiling on windows
  • Feature: add from_type/to_type kwargs to the to_dict methods, allowing for type conversion when creating the dictionary

0.3.2 (2021-09-04)

  • Bugfix: depth was incorrectly ignored when converting sorteddict to python dict

0.3.1 (2021-09-01)

  • Bugfix: truncate and max_depth not being passed from orderbook to sorteddict object correctly
  • Feature: let checksum_format kwarg be set to None

0.3.0 (2021-07-16)

  • Update classifiers to indicate this projects only supports MacOS/Linux
  • Bugfix: Using less than the minimum number of levels for a checksum with Kraken not raising error correctly
  • Update: add del examples to test code

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.4.0.tar.gz (13.5 kB view details)

Uploaded Source

Built Distributions

order_book-0.4.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (61.5 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64 manylinux: glibc 2.5+ x86-64

order_book-0.4.0-cp39-cp39-macosx_10_9_x86_64.whl (25.3 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

order_book-0.4.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (62.8 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64 manylinux: glibc 2.5+ x86-64

order_book-0.4.0-cp38-cp38-macosx_10_9_x86_64.whl (26.1 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

order_book-0.4.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (60.1 kB view details)

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

order_book-0.4.0-cp37-cp37m-macosx_10_9_x86_64.whl (25.8 kB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: order_book-0.4.0.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.26.0 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.4.0.tar.gz
Algorithm Hash digest
SHA256 ac3341372941e57019ce100e6b51deb5625fe09026e51f1dbac0f0715936cb6a
MD5 29098da8d9fbf21009498bd7ba99b0cc
BLAKE2b-256 1ac7fa5965f67e9bccf0adff11debd04b27ba01ecc6ba90a085c101bdc73c77f

See more details on using hashes here.

Provenance

File details

Details for the file order_book-0.4.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for order_book-0.4.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 ae19215e3b7a6b754422698323bf643a70f1e2c7d69a416b463032859d4f93c1
MD5 b22ea0c231fc2e1bbff96eb854ccb1de
BLAKE2b-256 5fd78cfb4582e9acfa9c3606f584390b3d1506cea7a9a7d5ac3e679be2f9e232

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.4.0-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 25.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.26.0 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.4.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ef27f0b63eafba203275dfc24ef2aa35911d09a6064ce2f7f30192c699e7e329
MD5 2da92a9f7362ab3542d277b4bc77200a
BLAKE2b-256 2dfa451bb01888db90e74b02ddefc597046ac8d03f6bfba178a6756171bc5569

See more details on using hashes here.

Provenance

File details

Details for the file order_book-0.4.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for order_book-0.4.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 c1f8d93c200f334fdde9b2f6ee45c48664b123ea71706668b18435ffcf3d1c08
MD5 5465106157707c4fb5ec5b976fd3a20a
BLAKE2b-256 a109df5b101d07e15f87dbe72560f52534fccc118567b9f8d4ad6c974bd4a7d0

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.4.0-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 26.1 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.26.0 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.4.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 37990ae0c677af4706798784471836e44a1be435be8977e2232d37990bd5f45a
MD5 f92a2fa36249769761fd06515552cba9
BLAKE2b-256 846c0fe85f9339ea65a114e915d473e3e3165615ad9df95223230cd272ecda5a

See more details on using hashes here.

Provenance

File details

Details for the file order_book-0.4.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for order_book-0.4.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 6dcda3f7bd792df9d2d60fbb2203a31d8dfdcccf694af1f159fe4662fc9c85b2
MD5 dab9725a931a9095859f37d7ea9db390
BLAKE2b-256 2bf73ec36bfee61330f3f65893d99c2d8b557a729a4a59a991900cdc8782faa1

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.4.0-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 25.8 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.26.0 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.4.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1103c6f43eb0cd1d26a53eb414bd3a9c90055d35457ae88a9858da12e391891d
MD5 0bedcc2cf035f5897096484182f5999b
BLAKE2b-256 6ff07e430aca62481f12eb8c87f50085f110729e410c377ae264539b3db2daf5

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