commit | author | age
|
5d61a1
|
1 |
""".#protocol.py handling protocol.""" |
JG |
2 |
import book |
|
3 |
|
|
4 |
|
|
5 |
def handleMessage(bs: {str, book.Book}, message: {str, str}): |
|
6 |
"""Handle message from exchange.""" |
|
7 |
if (message["type"] in {"FUTURE", "SPREAD", "CALL", "PUT"}): |
|
8 |
announce(bs, message) |
|
9 |
elif (message["type"] == "SETTLEMENT"): |
|
10 |
settle(bs, message) |
|
11 |
elif (message["type"] == "ADDED"): |
|
12 |
addOrder(bs, message) |
|
13 |
elif (message["type"] == "DELETED"): |
|
14 |
pass |
|
15 |
|
|
16 |
|
|
17 |
def announce(bs: {str, book.Book}, message: {str, str}): |
|
18 |
"""Announce message.""" |
|
19 |
# print(message) |
|
20 |
bs[message["product"]] = book.Book(message["type"], message["product"], |
|
21 |
message["stationId"], message["unit"], |
|
22 |
message["expiry"], |
|
23 |
message["aggressiveFee"], |
|
24 |
message["passiveFee"], |
|
25 |
message["brokerFee"]) |
|
26 |
|
|
27 |
|
|
28 |
def settle(bs: {str, book.Book}, message: {str, str}): |
|
29 |
"""Settle order.""" |
|
30 |
bs.pop(message["product"])#.printBook() |
|
31 |
|
|
32 |
def addOrder(bs: {str, book.Book}, message: {str, str}): |
|
33 |
"""Order added to exchange.""" |
|
34 |
price = message["price"] |
|
35 |
volume = message["resting"] + message["filled"] |
|
36 |
if (message["side"] == "BUY"): |
|
37 |
side = book.OrderSide.Buy |
|
38 |
else: |
|
39 |
side = book.OrderSide.Sell |
|
40 |
bs[message["product"]].addOrder(book.Order(price, side, volume)) |