Package: order-book
OrderbookAfastL2/L3 orderbook data structure, in C, for PythonBasic UsagefromdecimalimportDecimalimportrequestsfromorder_bookimportOrderBookob=OrderBook()# get some orderbook datadata=requests.get("https://api.pro.coinbase.com/products/BTC-USD/book?level=2").json()ob.bids={Decimal(price):sizeforprice,size,_indata['bids']}ob.asks={Decimal(price):sizeforprice,size,_indata['asks']}# ORforsideindata:# there is additional data we need to ignoreifsidein{'bids','asks'}:ob[side]={Decimal(price):sizeforprice,size,_indata[side]}# Data is accessible by .index(), which returns a tuple of (price, size) at that level in the bookprice,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 iteratorsprint("Bids")forpriceinob.bids:print(f"Price:{price}Size:{ob.bids[price]}")print("\n\nAsks")forpriceinob.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 orderprint("\n\nRaw asks dictionary")print(ob.asks.to_dict())Main FeaturesSides maintained in correct orderCan perform orderbook checksumsSupports max depth and depth truncationInstallationThe preferable way to install is viapip-pip install order-book. Installing from source will require a compiler and can be done with setuptools:python setup.py install.Running code coverageThe scriptcoverage.shwill compile the source using the-coverageCFLAG, 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 testsYou 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:LibraryTime, in secondsC Library0.00021767616271Python Library0.00043988227844The 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:LibraryNumber of KeysTime, in secondsC Library1000.00021600723266Python Library1000.00044703483581Python Dict1000.00022006034851C Library5000.00103306770324Python Library5000.00222206115722Python Dict5000.00097918510437C Library10000.00202703475952Python Library10000.00423812866210Python Dict10000.00176715850830This 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, runperformance_test.pyas well as the other performance tests inperf/Changelog0.6.0 (2022-10-19)Update: Drop support for python 3.7Feature: to_list methodBugfix: Initialize iterator correctly0.5.0 (2022-08-23)Bugfix: fix segmentation fault when calculating checksum on empty orderbookBugfix: fix missing reference decrementPerformance: Improvement to marking dirty keys0.4.3 (2022-05-29)Bugfix: handle scientific notation of small values in Kraken checksumUpdate: calculate Kraken checksum on order books less than 10 levels deepBugfix: fix occasional incorrect checksums for OKX, FTX and Bitget0.4.2 (2022-04-17)Update: OKEx renamed OKX (for checksum validation)Feature: Add support for orderbook checksums with Bitget0.4.1 (2021-10-12)Bugfix: unnecessary reference counting prevented sorted dictionaries from being deallocatedBugfix: setting ordering on a sorted dict before checking that it was created successfully0.4.0 (2021-09-16)Feature: changes to code and setup.py to enable compiling on windowsFeature: add from_type/to_type kwargs to the to_dict methods, allowing for type conversion when creating the dictionary0.3.2 (2021-09-04)Bugfix: depth was incorrectly ignored when converting sorteddict to python dict0.3.1 (2021-09-01)Bugfix: truncate and max_depth not being passed from orderbook to sorteddict object correctlyFeature: let checksum_format kwarg be set to None0.3.0 (2021-07-16)Update classifiers to indicate this projects only supports MacOS/LinuxBugfix: Using less than the minimum number of levels for a checksum with Kraken not raising error correctlyUpdate: add del examples to test code0.2.1 (2021-03-29)Bugfix: Invalid deallocation of python object0.2.0 (2021-03-12)Feature: Add branch prediction hints around error handling codeBugfix: Fix regression from adding branch predictorsBugfix: Fix error corner case when iterating twice on an empty datasetFeature: Add contains function for membership testBugfix: Fix issues around storing L3 dataFeature: Enhance testing, add in L3 book test cases0.1.1 (2021-02-12)Feature: Checksum support for orderbooksFeature: FTX checksum supportFeature: Kraken checksum supportFeature: OkEX/OKCoin checksum supportPerf: Use CRC32 table to improve performance of checksum code0.1.0 (2021-01-18)Minor: Use enums to make code more readableBugfix: Add manifest file to ensure headers and changes file are included in sdist buildsFeature: Add support for max depth and depth truncation0.0.2 (2020-12-27)Bugfix: Fix sorted dictionary arg parsingFeature: Coverage report generation for C libraryBugfix: Fix reference counting in index method in SortedDictFeature: New unit tests to improve SortedDict coverageFeature: Modularize filesFeature: Add ability to set bids/asks to dictionaries via attributes or [ ]Docs: Update README with simple usage example0.0.1 (2020-12-26)Initial Release