Skip to main content

Order Book Matching Engine

Project description

Order Book Matching Engine

pytest Documentation Status !pypi !python-versions pre-commit Ruff

Overview

This package is a simple order book matching engine implementation in Python. Its main features are:

  • price-time priority
  • limit and market orders
  • order cancellation and expiration
  • conversion into pandas DataFrame of orders, executed trades, order book summary

Install

pip install order-matching

Documentation

order-book-matching-engine.readthedocs.io

Usage

>>> from datetime import datetime, timedelta
>>> from pprint import pp
>>> import pandas as pd

>>> from order_matching.matching_engine import MatchingEngine
>>> from order_matching.order import LimitOrder
>>> from order_matching.side import Side
>>> from order_matching.orders import Orders

>>> matching_engine = MatchingEngine(seed=123)
>>> timestamp = datetime(2023, 1, 1)
>>> transaction_timestamp = timestamp + timedelta(days=1)
>>> buy_order = LimitOrder(side=Side.BUY, price=1.2, size=2.3, timestamp=timestamp, order_id="a", trader_id="x")
>>> sell_order = LimitOrder(side=Side.SELL, price=0.8, size=1.6, timestamp=timestamp, order_id="b", trader_id="y")
>>> executed_trades = matching_engine.match(orders=Orders([buy_order, sell_order]), timestamp=transaction_timestamp)

>>> pp(executed_trades.trades)
[Trade(side=SELL,
       price=1.2,
       size=1.6,
       incoming_order_id='b',
       book_order_id='a',
       execution=LIMIT,
       trade_id='c4da537c-1651-4dae-8486-7db30d67b366',
       timestamp=datetime.datetime(2023, 1, 2, 0, 0))]

Related Projects

Contribute

Create a virtual environment and activate it:

python -m venv venv
source venv/bin/activate

Install development dependencies:

pip install -e .[dev]

and use pre-commit to make sure that your code is formatted using black and isort automatically:

pre-commit install

Run tests:

pip install -e .[test]
pytest

Run benchmark and see the result either in the terminal or as a plot in benchmark_history.svg:

./benchmark.sh

Build and serve documentation website:

pip install -e .[doc]
mkdocs serve

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

order_matching-0.3.12.tar.gz (80.7 kB view details)

Uploaded Source

Built Distribution

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

order_matching-0.3.12-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file order_matching-0.3.12.tar.gz.

File metadata

  • Download URL: order_matching-0.3.12.tar.gz
  • Upload date:
  • Size: 80.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for order_matching-0.3.12.tar.gz
Algorithm Hash digest
SHA256 1db3509891e6dea1298374f806572c47530699df099340d132912ee7e9d06dc8
MD5 06f8c31479f66018e9028a5e9aac41bb
BLAKE2b-256 e1f8543f53ac95861ae74c7afdf647904cf5507c6c4c555bc19c152bec7f9297

See more details on using hashes here.

Provenance

The following attestation bundles were made for order_matching-0.3.12.tar.gz:

Publisher: workflow.yaml on khrapovs/OrderBookMatchingEngine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file order_matching-0.3.12-py3-none-any.whl.

File metadata

File hashes

Hashes for order_matching-0.3.12-py3-none-any.whl
Algorithm Hash digest
SHA256 5579de844c1e89b85d0c0922b883014e1688b849202d662b4d63ff01d8cdb04b
MD5 abd56c0ec8056748b92ddeea7cc043b0
BLAKE2b-256 6c5d8487d03d4033998aa8bce1311f0442d898f27b0bdec3b3c2ab0b79e3ec96

See more details on using hashes here.

Provenance

The following attestation bundles were made for order_matching-0.3.12-py3-none-any.whl:

Publisher: workflow.yaml on khrapovs/OrderBookMatchingEngine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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