{"id":13683055,"url":"https://github.com/bmoscon/orderbook","last_synced_at":"2025-05-15T09:06:51.041Z","repository":{"id":37021893,"uuid":"315783540","full_name":"bmoscon/orderbook","owner":"bmoscon","description":"A fast L2/L3 orderbook data structure, in C, for Python","archived":false,"fork":false,"pushed_at":"2024-11-09T00:14:46.000Z","size":233,"stargazers_count":270,"open_issues_count":1,"forks_count":54,"subscribers_count":13,"default_branch":"main","last_synced_at":"2025-04-14T15:01:15.051Z","etag":null,"topics":["c","finance","orderbook","python","python-c-extension","python-extension","trading"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bmoscon.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-25T00:09:54.000Z","updated_at":"2025-04-13T20:30:59.000Z","dependencies_parsed_at":"2024-04-04T00:38:54.154Z","dependency_job_id":"c7462355-a2b1-4562-b263-f5125b145af9","html_url":"https://github.com/bmoscon/orderbook","commit_stats":{"total_commits":187,"total_committers":7,"mean_commits":"26.714285714285715","dds":0.03743315508021394,"last_synced_commit":"0c32932dd9f0f79b30897ecdcd8ea445efb4ab1d"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bmoscon%2Forderbook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bmoscon%2Forderbook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bmoscon%2Forderbook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bmoscon%2Forderbook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bmoscon","download_url":"https://codeload.github.com/bmoscon/orderbook/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254310515,"owners_count":22049469,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","finance","orderbook","python","python-c-extension","python-extension","trading"],"created_at":"2024-08-02T13:01:58.771Z","updated_at":"2025-05-15T09:06:51.023Z","avatar_url":"https://github.com/bmoscon.png","language":"Python","readme":"# Orderbook\n\n[![License](https://img.shields.io/badge/license-GLPv3-blue.svg)](LICENSE)\n![Python](https://img.shields.io/badge/Python-3.8+-green.svg)\n[![PyPi](https://img.shields.io/badge/PyPi-order--book-brightgreen)](https://pypi.python.org/pypi/order-book)\n![coverage-lines](https://img.shields.io/badge/coverage%3A%20lines-84.6%25-blue)\n![coverage-functions](https://img.shields.io/badge/coverage%3A%20functions-100%25-blue)\n\n\nA ***fast*** L2/L3 orderbook data structure, in C, for Python\n\n\n### Basic Usage\n\n```python\nfrom decimal import Decimal\n\nimport requests\nfrom order_book import OrderBook\n\nob = OrderBook()\n\n# get some orderbook data\ndata = requests.get(\"https://api.pro.coinbase.com/products/BTC-USD/book?level=2\").json()\n\nob.bids = {Decimal(price): size for price, size, _ in data['bids']}\nob.asks = {Decimal(price): size for price, size, _ in data['asks']}\n\n# OR\n\nfor side in data:\n    # there is additional data we need to ignore\n    if side in {'bids', 'asks'}:\n        ob[side] = {Decimal(price): size for price, size, _ in data[side]}\n\n\n# Data is accessible by .index(), which returns a tuple of (price, size) at that level in the book\nprice, size = ob.bids.index(0)\nprint(f\"Best bid price: {price} size: {size}\")\n\nprice, size = ob.asks.index(0)\nprint(f\"Best ask price: {price} size: {size}\")\n\nprint(f\"The spread is {ob.asks.index(0)[0] - ob.bids.index(0)[0]}\\n\\n\")\n\n# Data is accessible via iteration\n# Note: bids/asks are iterators\n\nprint(\"Bids\")\nfor price in ob.bids:\n    print(f\"Price: {price} Size: {ob.bids[price]}\")\n\n\nprint(\"\\n\\nAsks\")\nfor price in ob.asks:\n    print(f\"Price: {price} Size: {ob.asks[price]}\")\n\n\n# Data can be exported to a sorted dictionary\n# In Python3.7+ dictionaries remain in insertion ordering. The\n# dict returned by .to_dict() has had its keys inserted in sorted order\nprint(\"\\n\\nRaw asks dictionary\")\nprint(ob.asks.to_dict())\n\n\n# Data can also be exported as an ordered list\n# .to_list() returns a list of (price, size) tuples\nprint(\"Top 5 Asks\")\nprint(ob.asks.to_list()[:5])\nprint(\"\\nTop 5 Bids\")\nprint(ob.bids.to_list()[:5])\n\n```\n\n### Main Features\n\n* Sides maintained in correct order\n* Can perform orderbook checksums\n* Supports max depth and depth truncation\n\n\n### Installation\n\nThe 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`. \n\n\n### Running code coverage\n\nThe 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).\n\n\n### Running the performance tests\n\nYou 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.\n\nThe performance of constructing a sorted orderbook (using live data from Coinbase) using this C library, versus a pure Python sorted dictionary library:\n\n\n| Library        | Time, in seconds |\n| ---------------| ---------------- |\n| C Library      | 0.00021767616271 |\n| Python Library | 0.00043988227844 |\n\nThe 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:\n\n\n| Library        | Number of Keys | Time, in seconds |\n| -------------- | -------------- | ---------------- |\n| C Library      |     100        | 0.00021600723266 |\n| Python Library |     100        | 0.00044703483581 |\n| Python Dict    |     100        | 0.00022006034851 |\n| C Library      |     500        | 0.00103306770324 |\n| Python Library |     500        | 0.00222206115722 |\n| Python Dict    |     500        | 0.00097918510437 |\n| C Library      |     1000       | 0.00202703475952 |\n| Python Library |     1000       | 0.00423812866210 |\n| Python Dict    |     1000       | 0.00176715850830 |\n\n\nThis 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.\n\n\nFor other performance metrics, run `performance_test.py` as well as the other performance tests in [`perf/`](perf/)\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbmoscon%2Forderbook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbmoscon%2Forderbook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbmoscon%2Forderbook/lists"}