⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content

Simple Python implementation of order book matching engine

License

Notifications You must be signed in to change notification settings

khrapovs/OrderBookMatchingEngine

 
 

Repository files navigation

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

About

Simple Python implementation of order book matching engine

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 99.6%
  • Shell 0.4%