-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatchingEngine.py
64 lines (48 loc) · 2.37 KB
/
MatchingEngine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from Book import *
class MatchingEngine:
def __init__(self):
self._book = Book() # internal book
self._subscribed_books = []
self._on_reject_cbs = []
# Handles a new order submitted to the matching engine.
# `refnum` is unpopulated on `order`. If the incoming `order` matches a resting order,
# both orders are executed. If `order` still needs to be added to the book, a
# unique, positive refnum is generated and set on the order before the order is
# added to the book and downstream callbacks are invoked.
# Returns the order's refnum if the order was added to the book. Returns -1 if
# no order was added.
def on_new(self, order : Order) -> int:
return -1
# Handles an order update submitted to the matching engine.
# Rejects any orders that increase the size.
def on_update(self, order : Order) -> None:
pass
# Handles an order cancellation submitted to the matching engine.
# Partial cancels are acceptable.
# A size of 0 indicates cancelling all shares of an order.
# Price and Side are not expected to be populated.
def on_cancel(self, order : Order) -> None:
pass
# Invoke downstream callbacks on a new order.
def _call_on_add(self, order : Order) -> None:
[book.on_add(order) for book in self._subscribed_books]
# Invoke downstream callbacks on an update.
def _call_on_update(self, order : Order) -> None:
[book.on_update(order) for book in self._subscribed_books]
# Invoke downstream callbacks on a cancel.
def _call_on_cancel(self, order :Order) -> None:
[book.on_cancel(order) for book in self._subscribed_books]
# Invoke downstream callbacks on an exec.
def _call_on_exec(self, order :Order) ->None:
[book.on_exec(order) for book in self._subscribed_books]
# Invoke downstream callbacks on a reject.
def _on_reject(self, order :Order) ->None:
[cb[order] for cb in self._on_reject_cbs]
# Subscribe an `external_book` that subscribes to add, update, cancel, exec
# messages from matching engine.
def subscribe(self, external_book : Book):
self._subscribed_books.append(external_book)
# Subscribe a callback `cb` to be invoked when an order is rejected by
# the matching engine.
def sub_on_reject(self, cb : Callable[[Order],None]):
self._on_reject_cbs.append(cb)