-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorderbook_test.py
74 lines (62 loc) · 3.23 KB
/
orderbook_test.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
69
70
71
72
73
74
import unittest
from orderway import Buy, Sell
from serverorder import ServerOrder
from orderbook import OrderBook
from instrument import Instrument
class TestOrderBook(unittest.TestCase):
def setUp(self):
self.instrument = Instrument(identifier=0, name='Carrefour', isin='FR0000120172', currency_identifier=0)
self.book = OrderBook(self.instrument.identifier)
def test_count_bids(self):
buy_order = ServerOrder(Buy(), self.instrument.identifier, 50, 42.0, 'Trader1')
self.assertEqual(self.book.count_bids(), 0)
self.book.on_new_order(buy_order)
self.assertEqual(self.book.count_bids(), 1)
def test_count_asks(self):
sell_order = ServerOrder(Sell(), self.instrument.identifier, 50, 42.0, 'Trader1')
self.assertEqual(self.book.count_asks(), 0)
self.book.on_new_order(sell_order)
self.assertEqual(self.book.count_asks(), 1)
def test_get_bids(self):
bids = self.book.get_bids()
self.assertEqual(len(bids), 0)
def test_get_asks(self):
asks = self.book.get_asks()
self.assertEqual(len(asks), 0)
def test_two_orders_no_match(self):
buy_order = ServerOrder(Buy(), self.instrument.identifier, 50, 40.0, 'Trader1')
sell_order = ServerOrder(Sell(), self.instrument.identifier, 50, 42.0, 'Trader2')
self.book.on_new_order(buy_order)
self.book.on_new_order(sell_order)
self.assertEqual(self.book.count_bids(), 1)
self.assertEqual(self.book.count_asks(), 1)
def test_four_stacked_orders_no_match(self):
orders = [ServerOrder(Buy(), self.instrument.identifier, 50, 40.0, 'Trader1'),
ServerOrder(Buy(), self.instrument.identifier, 50, 40.0, 'Trader1'),
ServerOrder(Sell(), self.instrument.identifier, 50, 42.0, 'Trader2'),
ServerOrder(Sell(), self.instrument.identifier, 50, 42.0, 'Trader2')]
for order in orders:
self.book.on_new_order(order)
self.assertEqual(self.book.count_bids(), 2)
self.assertEqual(self.book.count_asks(), 2)
def test_fifo_matching_orders(self):
""" Ensure FIFO matching is enforced """
orders = [ServerOrder(Buy(), self.instrument.identifier, 50, 40.0, 'Trader1', timestamp=1),
ServerOrder(Buy(), self.instrument.identifier, 50, 40.0, 'Trader2', timestamp=2),
ServerOrder(Sell(), self.instrument.identifier, 50, 40.0, 'Trader3', timestamp=3)]
for order in orders:
self.book.on_new_order(order)
self.assertEqual(self.book.count_bids(), 1)
self.assertEqual(self.book.count_asks(), 0)
self.assertEqual(self.book.get_bids()[0].timestamp, 2)
self.assertEqual(self.book.get_bids()[0].counterparty, 'Trader2')
def test_self_execution(self):
""" Ensure you cannot trade with yourself """
orders = [ServerOrder(Buy(), self.instrument.identifier, 50, 40.0, 'Trader1'),
ServerOrder(Sell(), self.instrument.identifier, 50, 40.0, 'Trader1')]
for order in orders:
self.book.on_new_order(order)
self.assertEqual(self.book.count_bids(), 1)
self.assertEqual(self.book.count_asks(), 1)
if __name__ == '__main__':
unittest.main()