Skip to main content

A fast orderbook implementation, in C, for Python

Project description

Orderbook

License Python PyPi

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-public.sandbox.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 contains some dependencies 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.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.0.2.tar.gz (6.0 kB view details)

Uploaded Source

Built Distributions

order_book-0.0.2-cp39-cp39-manylinux2010_x86_64.whl (44.0 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

order_book-0.0.2-cp39-cp39-manylinux1_x86_64.whl (44.0 kB view details)

Uploaded CPython 3.9

order_book-0.0.2-cp39-cp39-macosx_10_9_x86_64.whl (20.2 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

order_book-0.0.2-cp38-cp38-manylinux2010_x86_64.whl (44.5 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

order_book-0.0.2-cp38-cp38-manylinux1_x86_64.whl (44.5 kB view details)

Uploaded CPython 3.8

order_book-0.0.2-cp38-cp38-macosx_10_9_x86_64.whl (20.7 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

order_book-0.0.2-cp37-cp37m-manylinux2010_x86_64.whl (42.6 kB view details)

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

order_book-0.0.2-cp37-cp37m-manylinux1_x86_64.whl (42.6 kB view details)

Uploaded CPython 3.7m

order_book-0.0.2-cp37-cp37m-macosx_10_9_x86_64.whl (20.7 kB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: order_book-0.0.2.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0.post20200106 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.9

File hashes

Hashes for order_book-0.0.2.tar.gz
Algorithm Hash digest
SHA256 f506bd7944511d0905d12d4f2c6738691028e6b01e0edb06b1528ed23c21fd84
MD5 7b7eec43cca177f1d9448be6a34bca0c
BLAKE2b-256 3dc9289f0abf43ada394e2ffae1c2c4d0c2da295828ea6463148c123c7ac709b

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.0.2-cp39-cp39-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 44.0 kB
  • Tags: CPython 3.9, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0.post20200106 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.9

File hashes

Hashes for order_book-0.0.2-cp39-cp39-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 2ee7d1f0c33b1f5dc082fa382d86253779adeeee30061a05798177b659544f44
MD5 3899d4114633e3451e4939c1de1c0efa
BLAKE2b-256 5c9b866bd7a82332f0164b80b0eec3e8103e9778e21144cab55c5cb9a7fdaf3c

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.0.2-cp39-cp39-manylinux1_x86_64.whl
  • Upload date:
  • Size: 44.0 kB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0.post20200106 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.9

File hashes

Hashes for order_book-0.0.2-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2afb58e5826a55ff60a37a8b649be4c5d8886b02f48192671480ea7690db84a9
MD5 2de5ec5bdfe5d783ced2d5381376e5ac
BLAKE2b-256 b372defe596c111d66ee014e6aa947564cabd813446a2533ce18ba79b3f48fa7

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.0.2-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 20.2 kB
  • Tags: CPython 3.9, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0.post20200106 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.9

File hashes

Hashes for order_book-0.0.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7abd039fbc4c9a7b80dc2a56556eb6e143ee64e7ad3cf6bf74e42319f56fd878
MD5 c6b3f35463e07a659df2f5fb31feeb66
BLAKE2b-256 bf85f031b24dd5795eeaf665c2cf7dd084a2b58f43a610d5a9b2b045ac3500f1

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.0.2-cp38-cp38-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 44.5 kB
  • Tags: CPython 3.8, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0.post20200106 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.9

File hashes

Hashes for order_book-0.0.2-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 2c9cc4bca30cad9f6efa3dfb79eb5d7fe73e7a882e0dd3db8de2aac8ae48169e
MD5 e6806d31154b3ae84e2ae5b1ba4eebe5
BLAKE2b-256 aeb864979eb7917d65f81c0e9a7168765172f2c78e5158bc4dd50cbf295e315a

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.0.2-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 44.5 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0.post20200106 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.9

File hashes

Hashes for order_book-0.0.2-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 519873f915288750047f88ace0120eb6411c03352b23608b885cddef7d1b6bda
MD5 a580dbabd8225efa7edef2d9fb619af3
BLAKE2b-256 4c46bc6d88eda5569403d8517fdfa198fd7e26c0c72599c0771253124bcb1be7

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.0.2-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 20.7 kB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0.post20200106 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.9

File hashes

Hashes for order_book-0.0.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 abe6fa4c26fba78bfda149def44fee8a361fc1a2a0ed055fd8d5ae87b2608bc5
MD5 6b6a9c58ebdd0ff53dad5a2a22e45063
BLAKE2b-256 d854570332475c327d71be54155b7c765e9403295e78d42e71ebd48874f955c3

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.0.2-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 42.6 kB
  • Tags: CPython 3.7m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0.post20200106 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.9

File hashes

Hashes for order_book-0.0.2-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 58156f4b3b480a8f82713e5121926630823d6cad4d94096a84abb2aac809d908
MD5 a7ca9810fd8d7fe57147ebfcea92fd9b
BLAKE2b-256 13513aa9251a40154832971670b5b07b6ac8c064a7bdc3a43ea9470f92b0ae74

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.0.2-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 42.6 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0.post20200106 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.9

File hashes

Hashes for order_book-0.0.2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 33605f6be2a1453636f06af8488c3b218aef8b166cc41a1ab316d6f355d2d1c8
MD5 cc3bf706d6ac4a40fea5ab31bdc7565a
BLAKE2b-256 5cfd2c5d4db30d705f32a2460db055c316581d0cf0505de423bb3d05be49ee9d

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: order_book-0.0.2-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 20.7 kB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0.post20200106 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.9

File hashes

Hashes for order_book-0.0.2-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b3480282c5dc8b7a0aba3df5fb9c5b0dd7bd2d1e1d1334b991bba72b7f3b8391
MD5 7418ec767726732fb7086aab0e70dfe0
BLAKE2b-256 fe9f4c6374cdce8814db104d4a19bb642d22ce93d0413063624a1f39a4131544

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