Joel Grunbaum
2022-01-11 d14f48a147190989d9b58f7b56429ac8711be077
commit | author | age
5d61a1 1 """.#protocol.py handling protocol."""
JG 2 import book
4497ee 3 from enum import Enum
JG 4 import httpx
5 import json
6
7 test = True
8
9 HOST: str = "sytev070"
10 if (test):
11     PORT = "9005"
12 else:
13     PORT = "9000"
14
15 USER: str = "jgrunbau"
16 PASS: str = "b7d630945a0854581d9f86ba147f34a5"
5d61a1 17
JG 18
19 def handleMessage(bs: {str, book.Book}, message: {str, str}):
20     """Handle message from exchange."""
21     if (message["type"] in {"FUTURE", "SPREAD", "CALL", "PUT"}):
22         announce(bs, message)
23     elif (message["type"] == "SETTLEMENT"):
24         settle(bs, message)
25     elif (message["type"] == "ADDED"):
26         addOrder(bs, message)
27     elif (message["type"] == "DELETED"):
4497ee 28         deleteOrder(bs, message)
JG 29     elif (message["type"] == "TRADE"):
30         tradeOrder(bs, message)
31     elif (message["type"] in {"BROKER_REQUEST", "BROKER_ACK",
32                               "BROKER_CONFIRM"}):
33         broker(bs, message)
5d61a1 34
JG 35
36 def announce(bs: {str, book.Book}, message: {str, str}):
37     """Announce message."""
38     # print(message)
39     bs[message["product"]] = book.Book(message["type"], message["product"],
40                                        message["stationId"], message["unit"],
41                                        message["expiry"],
42                                        message["aggressiveFee"],
43                                        message["passiveFee"],
44                                        message["brokerFee"])
45
46
47 def settle(bs: {str, book.Book}, message: {str, str}):
48     """Settle order."""
4497ee 49     b = bs.pop(message["product"])
JG 50     if (False):
51         b.printBook()
52         print()
53         if (len(b.askSide.levels) > 0):
54             print(b.askSide.levels[0])
55         if (len(b.bidSide.levels)):
56             print(b.bidSide.levels[0])
57         print()
58
5d61a1 59
JG 60 def addOrder(bs: {str, book.Book}, message: {str, str}):
61     """Order added to exchange."""
62     if (message["side"] == "BUY"):
4497ee 63         bs[message["product"]].bid(
JG 64             book.Order(message["price"], book.OrderSide.Buy,
65                        message["resting"],
66                        message["timestamp"], message["id"]))
5d61a1 67     else:
4497ee 68         bs[message["product"]].ask(
JG 69             book.Order(message["price"], book.OrderSide.Sell,
70                        message["resting"],
71                        message["timestamp"], message["id"]))
72
73
74 def deleteOrder(bs: {str, book.Book}, message: {str, str}):
75     """Delete order from market."""
76     if (message["side"] == "BUY"):
77         bs[message["product"]].bidSide.delete(message["id"])
78     else:
79         bs[message["product"]].askSide.delete(message["id"])
80
81
82 def tradeOrder(bs: {str, book.Book}, message: {str, str}):
83     """Order traded."""
84     if (message["tradeType"] == "BUY_AGGRESSOR"):
85         bs[message["product"]].bidSide.delete(message["passiveOrder"])
86     elif (message["tradeType"] == "SELL_AGGRESSOR"):
87         bs[message["product"]].askSide.delete(message["passiveOrder"])
88
89
90 def broker(bs: {str, book.Book}, message: {str, str}):
91     """Broker trades."""
92     pass
93
94
95 def send(message):
96     """Send message with exchange."""
97     return httpx.post(f"http://{HOST}:{PORT}/execution",
98                       data={"message": json.dumps(message),
99                             "username": USER,
100                             "password": PASS}).json()
101
102
103 class OrderEnum(Enum):
104     """Enum for ordering from the exchange."""
105
106     ADD = 0,
107     DELETE = 1,
108     BROKER_REQUEST = 2,
109     BROKER_CONFIRM = 3
110
111
112 def order(orderType: OrderEnum, product: str, price: float = 0,
113           side: book.OrderSide = book.OrderSide.Buy,
114           volume: int = 0, orderId: str = "", counterparty: str = ""):
115     """Order from exchange."""
116     if (orderType == OrderEnum.ADD):
117         resp = send({"type": "ADD",
118                      "product": product,
119                      "price": price,
120                      "side": side,
121                      "volume": volume})
122     elif (orderType == OrderEnum.DELETE):
123         resp = send({"type": "DELETE",
124                      "product": product,
125                      "id": id})
126     elif (orderType == OrderEnum.BROKER_REQUEST):
127         pass
128     elif (orderType == OrderEnum.BROKER_CONFIRM):
129         pass
130     return resp