Skip to main content

Order Book and Matching Engine

Project description

PyOrderBook

Project CI PyPI version Python Versions License

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.Decimal objects 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)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyorderbook-0.4.9-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

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

Hashes for pyorderbook-0.4.9.tar.gz
Algorithm Hash digest
SHA256 63b0ac502f63178001619c2cbc62d5463b2d64c2cf1576b117aa14db1a78538e
MD5 ad321560c01460563e4af4e7c1736d6e
BLAKE2b-256 5c960dc8b8d790a3bec36bc6e70fd82c936113182ee8990ebe0181e521a3bafb

See more details on using hashes here.

File details

Details for the file pyorderbook-0.4.9-py3-none-any.whl.

File metadata

File hashes

Hashes for pyorderbook-0.4.9-py3-none-any.whl
Algorithm Hash digest
SHA256 d470c5b8e17fc653a01665bf8b6011a0a1cdabadb72895ed10631d6174f97f32
MD5 525141b5cbd8fb5e35a7c58120e667e2
BLAKE2b-256 ce5c42a25c3e19ce2eb6c917c24352360e8401e1cd543b60741f3adc9440311f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page