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 details)

Uploaded Source

Built Distributions

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

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 details)

Uploaded CPython 3.9

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

Uploaded CPython 3.9 macOS 10.9+ x86-64

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

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 details)

Uploaded CPython 3.8

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

Uploaded CPython 3.8 macOS 10.9+ x86-64

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

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 details)

Uploaded CPython 3.7m

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

Uploaded CPython 3.7m macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: order_book-0.1.0.tar.gz
  • Upload date:
  • Size: 8.7 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.0.tar.gz
Algorithm Hash digest
SHA256 6939af4525b549f3a6643bcca8f5bbdec2528ff3b1fa4272f70e5d7635ac2b94
MD5 d1039ea8bb8fb0358b5328a1aa6bd9d1
BLAKE2b-256 337bb8c7dbd5008f35ddef9898b3332379384d16b66304b52ef4e2eaf4589f3f

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.1.0-cp39-cp39-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 49.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.1.0-cp39-cp39-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 8b40ca440459c972334e09f6367ce05b5442c2d94b1e37eb341721ad2313bebb
MD5 a3267cb341621103bae41a6e955f1673
BLAKE2b-256 cd1467e88cf9304e573f0cac4048cd01f749ff137bd905541c9d12ba4d5ed697

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.1.0-cp39-cp39-manylinux1_x86_64.whl
  • Upload date:
  • Size: 49.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.1.0-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 bbdef529f8e5e132833285cbadb860bcae3cbe14c668c5079bf436a16e483724
MD5 0defa4b563b4ab9824fe3b58bd27ff28
BLAKE2b-256 3c9352ee0ecae50636aa532b673ccce85680a6fc13e5f12497123c15e214d4f0

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.1.0-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 21.1 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.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6b07e83d9f8e1db4e9ead5a4033fa0d4801f2731406c9e6e8b0547727e3684b9
MD5 a57c57fe8badbd29cdac794c955da529
BLAKE2b-256 c13828afc0a8174ae572abeeb40e1247497fef1a5e44b1509189c786e5e016ee

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.1.0-cp38-cp38-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 49.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.0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 761714160bf124ea3c996a236fa1646c3a1e645fc3126ce4db3d1eccae0c6473
MD5 4d59c9f782b770dad19875dec7859577
BLAKE2b-256 c573f7f75d14ff7cfbc108834c626d8d8a86531d492d0379acfc090dc2a1c876

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.1.0-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 49.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.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d8d6071236976524f778b2822bc1351c27bf0aba71230deb539c40ef7e5c2ea4
MD5 b17e548dd0351291bab98784cf1794b5
BLAKE2b-256 13fdbedd142d8fecf24219ff8f1d0fccbcc076e8b5bb18450da7cdbeb050ef7b

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.1.0-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 21.7 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.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 acbacd291cb640ebb5a080565f3d7606f734c426be1d20d5b5318b37e3a6e5f0
MD5 a8fb3d5abc2e6190edb34c34aaaa23f2
BLAKE2b-256 d279023d43272363ea3c003e3b258105ff21e5c2cdf191b1679dbaf979893809

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.1.0-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 46.9 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.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 f19ac62212e17ed064441a9d9157db6bef1765142755dbce69cd9fb88c83ef25
MD5 4f8fcdd660479b5046223bbf93e3ea20
BLAKE2b-256 62cc671fba3ffe441392267243ab188c246804ced45b0985b6e807075dc957b9

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.1.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 46.9 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.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 68607f25e5d60878384d68262d6b7d3f8ddc1ae306d91593ded3fc5f96996f4d
MD5 935f71358375e6b604aca96d20336d8f
BLAKE2b-256 205182eb94a7c7ea32a616ed11bc275f457d6d6970fd923c9ba4ab1ba6232e8d

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.1.0-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 21.6 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.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 cd4ca805a8415f2faceb733357605b0adae943f2a7e35fbd872c369734e29406
MD5 7a90345e5f82ad1e1f984470191fee81
BLAKE2b-256 71fef5e6c90b5e0537b67cfa2b0691fe56c04cd0d04ee09e59de26b0728837dc

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