Joel Grunbaum
2022-01-06 5d61a1e5c5942901cc02f87b664ad6304cf79213
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
""".#protocol.py handling protocol."""
import book
 
 
def handleMessage(bs: {str, book.Book}, message: {str, str}):
    """Handle message from exchange."""
    if (message["type"] in {"FUTURE", "SPREAD", "CALL", "PUT"}):
        announce(bs, message)
    elif (message["type"] == "SETTLEMENT"):
        settle(bs, message)
    elif (message["type"] == "ADDED"):
        addOrder(bs, message)
    elif (message["type"] == "DELETED"):
        pass
 
 
def announce(bs: {str, book.Book}, message: {str, str}):
    """Announce message."""
    # print(message)
    bs[message["product"]] = book.Book(message["type"], message["product"],
                                       message["stationId"], message["unit"],
                                       message["expiry"],
                                       message["aggressiveFee"],
                                       message["passiveFee"],
                                       message["brokerFee"])
 
 
def settle(bs: {str, book.Book}, message: {str, str}):
    """Settle order."""
    bs.pop(message["product"])#.printBook()
 
def addOrder(bs: {str, book.Book}, message: {str, str}):
    """Order added to exchange."""
    price = message["price"]
    volume = message["resting"] + message["filled"]
    if (message["side"] == "BUY"):
        side = book.OrderSide.Buy
    else:
        side = book.OrderSide.Sell
    bs[message["product"]].addOrder(book.Order(price, side, volume))