A fast orderbook implementation, in C, for Python
Project description
Orderbook
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.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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for order_book-0.3.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9096e7a806ea65563fbf7d0cf88cbe3f87eba6f26583c01a497479b67275f33e |
|
MD5 | 4b15c8033ff3d698d1e9d72b2fb8aff1 |
|
BLAKE2b-256 | 1070cbdcbe1b4a697ba7dbde4efea3dbdfee2ba5ad6850a70fb9c1b4c3a82b74 |
Hashes for order_book-0.3.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa76802302d79753ea88b2da8fd54de1a1009457b6de9f2cf2ddb5012befa25b |
|
MD5 | feeadc04181cec096625ab6b20fb3beb |
|
BLAKE2b-256 | 36d9d1e5a510d23c75bf9676c4e7b8a49c469d71925557f09e190289890fd5b1 |
Hashes for order_book-0.3.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dbd057848d6d8a9f7b7b7b3dc5c11bc259ae34a59bbd8fb828c51aba241c2ae5 |
|
MD5 | f1629206eff0fae074bf4a2e047f7afc |
|
BLAKE2b-256 | 73bfc62dbdb79a6dc7ab3e4149434c8805a7ddac9fb69cb027b4181160f1c54c |
Hashes for order_book-0.3.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a14d491390aa86fb658508106bbce13836d6f4d1852b6518cb00773d054df73 |
|
MD5 | d68439303ea66a0a07afd7f9ef874324 |
|
BLAKE2b-256 | 5397c4ce5efecc10dde6649e45a3e76d11ece422e2a9592e4bb1e4232c91ca09 |
Hashes for order_book-0.3.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ac5dac834ed40c2d783a7029200ae14430b957d0e26445f89de91d2b1032ff3 |
|
MD5 | fe34a2986a0bb912b3a94a3a186b4fc8 |
|
BLAKE2b-256 | b4b175eedfb5c056aeecde48b03d3e430fbac60f178be4dc0b96c06821e597d0 |
Hashes for order_book-0.3.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d6eb59c710815673ebdf25f1fdff8d778cdf5024fc5a7fef2d7aac1d118450a |
|
MD5 | 91291a206ecd65c0c25cf3ba96417753 |
|
BLAKE2b-256 | 0156cd6c0a50792d269b4eccc5cead2ed9b8188920bcaa1e2be696fcd82a77f6 |