Skip to main content

Order Book Matching Engine

Project description

Order Book Matching Engine

pytest Documentation Status !pypi !python-versions pre-commit Code style: black 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 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 = pd.Timestamp("2023-01-01")
>>> transaction_timestamp = timestamp + pd.Timedelta(1, unit="D")
>>> 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=Timestamp('2023-01-02 00:00:00'))]

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

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.2.tar.gz (19.6 kB view hashes)

Uploaded Source

Built Distribution

order_matching-0.3.2-py3-none-any.whl (12.8 kB view hashes)

Uploaded Python 3

Supported by

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