matching_engine
MatchingEngine
Order Book Matching Engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int | None
|
Random seed |
None
|
Examples:
>>> from datetime import datetime, timedelta
>>> from pprint import pp
>>> from order_matching.matching_engine import MatchingEngine
>>> from order_matching.order import LimitOrder
>>> from order_matching.side import Side
>>> 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))]
match(timestamp, orders=None)
Match incoming orders in price-time priority.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestamp
|
datetime
|
Timestamp of order matching |
required |
orders
|
Orders | None
|
Incoming orders. Will be matched with existing ones on the order book in |
None
|
Returns:
| Type | Description |
|---|---|
ExecutedTrades
|
Executed trades storage object |