-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAKUNA-order-marking
97 lines (84 loc) · 4.45 KB
/
AKUNA-order-marking
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Implement the class below, keeping the constructor's signature unchanged; it should take no arguments.
import json
class Company:
def __init__(self, name):
self.name = name
self.own = 0
self.sell = 0
self.buy = 0
class MarkingPositionMonitor:
def __init__(self):
self.company_dict = {}
self.order_dict = {}
def on_event(self, message):
m_dict = json.loads(message)
if m_dict['type'] == 'NEW':
company_name = m_dict['symbol']
order_id = m_dict['order_id']
self.order_dict[order_id] = m_dict
if company_name not in self.company_dict:
self.company_dict[company_name] = Company(company_name)
if m_dict['side'] == 'SELL':
self.company_dict[company_name].sell += int(m_dict['quantity'])
if m_dict['side'] == 'BUY':
self.company_dict[company_name].buy += int(m_dict['quantity'])
return self.company_dict[company_name].own - self.company_dict[company_name].sell
if m_dict['type'] == 'ORDER_REJECT':
order_id = m_dict['order_id']
order_detail = self.order_dict[order_id]
company_name = order_detail['symbol']
if order_detail['type'] == 'NEW':
if order_detail['side'] == 'SELL':
self.company_dict[company_name].sell -= int(order_detail['quantity'])
if order_detail['side'] == 'BUY':
self.company_dict[company_name].buy -= int(order_detail['quantity'])
return self.company_dict[company_name].own - self.company_dict[company_name].sell
if m_dict['type'] == 'ORDER_ACK':
order_id = m_dict['order_id']
order_detail = self.order_dict[order_id]
company_name = order_detail['symbol']
if order_detail['side'] == 'SELL' or order_detail['side'] == 'BUY':
pass
return self.company_dict[company_name].own - self.company_dict[company_name].sell
if m_dict['type'] == 'CANCEL':
order_id = m_dict['order_id']
order_detail = self.order_dict[order_id]
company_name = order_detail['symbol']
if order_detail['side'] == 'SELL' or order_detail['side'] == 'BUY':
pass
return self.company_dict[company_name].own - self.company_dict[company_name].sell
if m_dict['type'] == 'CANCEL_ACK':
order_id = m_dict['order_id']
order_detail = self.order_dict[order_id]
company_name = order_detail['symbol']
if order_detail['type'] == 'NEW':
if order_detail['side'] == 'SELL':
self.company_dict[company_name].sell -= int(order_detail['quantity'])
if order_detail['side'] == 'BUY':
self.company_dict[company_name].buy -= int(order_detail['quantity'])
return self.company_dict[company_name].own - self.company_dict[company_name].sell
if m_dict["type"] == "CANCEL_REJECT":
# reject cancellation; no effect.
order_id = m_dict["order_id"]
order_detail = self.order_dict[order_id]
company_name = order_detail["symbol"]
if order_detail["type"] == "NEW":
if order_detail["side"] == "SELL":
pass
if order_detail["side"] == "BUY":
pass
return self.company_dict[company_name].own - self.company_dict[company_name].sell
if m_dict['type'] == 'FILL':
order_id = m_dict['order_id']
order_detail = self.order_dict[order_id]
company_name = order_detail['symbol']
if order_detail['type'] == 'NEW':
if 'filled_quantity' not in order_detail:
order_detail['filled_quantity'] = 0
if order_detail['side'] == 'SELL':
order_detail['filled_quantity'] = m_dict['filled_quantity']
if order_detail['side'] == 'BUY':
order_detail['filled_quantity'] = m_dict['filled_quantity']
self.company_dict[company_name].own += order_detail['filled_quantity']
return self.company_dict[company_name].own - self.company_dict[company_name].sell
return 0