Order Book and Matching Engine
Project description
PyOrderBook
PyOrderBook is a pure Python implementation of a limit order book and matching engine.
Features
- Order Matching Engine
- Order Cancellation
- Detailed Trade Blotter
Usage
from pyorderbook import Book, bid, ask
# Create a new order book
book = Book()
# Process some orders
book.match(bid("IBM", 3.5, 20))
book.match(ask("IBM", 3.6, 10))
trade_blotter = book.match(ask("IBM", 3.5, 10))
# Print trade blotter
print(trade_blotter)
Installation
To install the package, use:
# pip
pip install pyorderbook
# uv
uv pip install pyorderbook
# or
uv add pyorderbook
System Requirements
- Python 3.11+
Design
- Price Levels: Stored in a heap of dataclasses, each with a price attribute and orders attribute. Orders are stored in a dictionary within each price level. New price levels are created when an unseen price is received for a symbol/side, and standing price levels are deleted when there are no more orders in the queue at that price level.
- Order Queueing: Unfilled orders are enqueued to the tail of the corresponding symbol/side/price queue, maintaining insertion order.
- Matching Logic: Iterates through the price level heap (descending order for bids, ascending for asks) and dequeues from the head of each matching price level until the level or incoming order quantity is exhausted.
- Order Cancellation: Uses a reference map from order ID to its encompassing price level. The order is popped from the price level and the reference map.
- Precision: Uses
decimal.Decimalobjects to store prices to avoid floating point arithmetic problems.
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
pyorderbook-0.4.9.tar.gz
(18.2 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyorderbook-0.4.9.tar.gz.
File metadata
- Download URL: pyorderbook-0.4.9.tar.gz
- Upload date:
- Size: 18.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.26
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63b0ac502f63178001619c2cbc62d5463b2d64c2cf1576b117aa14db1a78538e
|
|
| MD5 |
ad321560c01460563e4af4e7c1736d6e
|
|
| BLAKE2b-256 |
5c960dc8b8d790a3bec36bc6e70fd82c936113182ee8990ebe0181e521a3bafb
|
File details
Details for the file pyorderbook-0.4.9-py3-none-any.whl.
File metadata
- Download URL: pyorderbook-0.4.9-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.26
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d470c5b8e17fc653a01665bf8b6011a0a1cdabadb72895ed10631d6174f97f32
|
|
| MD5 |
525141b5cbd8fb5e35a7c58120e667e2
|
|
| BLAKE2b-256 |
ce5c42a25c3e19ce2eb6c917c24352360e8401e1cd543b60741f3adc9440311f
|