-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathorderbooktest.py
68 lines (51 loc) · 2.39 KB
/
orderbooktest.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
63
64
65
66
67
68
import time
try:
import ujson as json
except ImportError:
import json
from orderbook.book import Book
def dict_compare(variable_order_book, control_order_book, price_map=False, order_map=False):
d1_keys = set(variable_order_book.keys())
d2_keys = set(control_order_book.keys())
intersect_keys = d1_keys.intersection(d2_keys)
assert intersect_keys
added = d1_keys - d2_keys
assert not added
removed = d2_keys - d1_keys
assert not removed
for key in intersect_keys:
if price_map:
assert len(variable_order_book[key]) == len(control_order_book[key])
zipped = zip(variable_order_book[key], control_order_book[key])
for order in zipped:
assert order[0]['order_id'] == order[1]['order_id']
assert order[0]['price'] == order[1]['price']
assert order[0]['size'] == order[1]['size']
if order_map:
assert variable_order_book[key]['order_id'] == control_order_book[key]['order_id']
assert variable_order_book[key]['price'] == control_order_book[key]['price']
assert variable_order_book[key]['size'] == control_order_book[key]['size']
def test_orderbook():
variable_order_book = Book()
control_order_book = Book()
with open('testdata/messages.json') as messages_json_file:
messages = json.load(messages_json_file)
with open('testdata/beginning_level_3.json') as begin_json_file:
beginning_level_3 = json.load(begin_json_file)
with open('testdata/ending_level_3.json') as end_json_file:
ending_level_3 = json.load(end_json_file)
try:
assert beginning_level_3['sequence'] + 1 == messages[0]['sequence']
assert ending_level_3['sequence'] == messages[-1]['sequence']
except AssertionError:
print("Problem with sample data sequences")
variable_order_book.get_level3(beginning_level_3)
start = time.time()
[variable_order_book.process_message(message) for message in messages]
end = time.time()
print('messages per sec: {0}'.format(int(len(messages)/(end-start))))
control_order_book.get_level3(ending_level_3)
dict_compare(variable_order_book.asks.price_map, control_order_book.asks.price_map, price_map=True)
dict_compare(variable_order_book.asks.order_map, control_order_book.asks.order_map, order_map=True)
if __name__ == '__main__':
test_orderbook()