def test_process_order_book_when_multiple_subscribers_then_sends_to_registered_handlers(self): # Arrange self.data_engine.register_client(self.binance_client) self.binance_client.connect() order_book = OrderBook( instrument_id=ETHUSDT_BINANCE.id, level=2, depth=25, price_precision=2, size_precision=5, bids=[], asks=[], update_id=0, timestamp=0, ) handler1 = [] subscribe1 = Subscribe( provider=BINANCE.value, data_type=DataType(OrderBook, { "InstrumentId": ETHUSDT_BINANCE.id, "Level": 2, "Depth": 25, "Interval": 0, # Streaming }), handler=handler1.append, command_id=self.uuid_factory.generate(), command_timestamp=self.clock.utc_now(), ) handler2 = [] subscribe2 = Subscribe( provider=BINANCE.value, data_type=DataType(OrderBook, { "InstrumentId": ETHUSDT_BINANCE.id, "Level": 2, "Depth": 25, "Interval": 0, # Streaming }), handler=handler2.append, command_id=self.uuid_factory.generate(), command_timestamp=self.clock.utc_now(), ) self.data_engine.execute(subscribe1) self.data_engine.execute(subscribe2) # Act self.data_engine.process(order_book) # Assert self.assertEqual([ETHUSDT_BINANCE.id], self.data_engine.subscribed_order_books) self.assertEqual(order_book, handler1[0]) self.assertEqual(order_book, handler2[0])
def test_bids_and_asks(self): # Arrange bids = np.asarray([[1550.15, 0.51], [1580.00, 1.20]]) asks = np.asarray([[1552.15, 1.51], [1582.00, 2.20]]) # Act order_book = OrderBook( symbol=ETHUSDT_BINANCE.symbol, level=2, price_precision=2, size_precision=2, bids=bids, asks=asks, timestamp=0, ) # Assert self.assertEqual(ETHUSDT_BINANCE.symbol, order_book.symbol) self.assertEqual(0, order_book.timestamp) self.assertEqual([[1550.15, 0.51], [1580.00, 1.20]], order_book.bids()) self.assertEqual([[1552.15, 1.51], [1582.00, 2.20]], order_book.asks())
def test_update(self): # Arrange bids1 = np.asarray([[1550.15, 0.51], [1580.00, 1.20]]) asks1 = np.asarray([[1552.15, 1.51], [1582.00, 2.20]]) order_book = OrderBook( symbol=ETHUSDT_BINANCE.symbol, level=2, price_precision=2, size_precision=2, bids=bids1, asks=asks1, timestamp=0, ) bids2 = np.asarray([[1551.00, 1.00], [1581.00, 2.00]]) asks2 = np.asarray([[1553.00, 1.00], [1583.00, 2.00]]) # Act order_book.update(bids2, asks2, 1) # Assert self.assertEqual(ETHUSDT_BINANCE.symbol, order_book.symbol) self.assertEqual(1, order_book.timestamp) self.assertEqual([[1551.00, 1.00], [1581.00, 2.00]], order_book.bids()) self.assertEqual([[1553.00, 1.00], [1583.00, 2.00]], order_book.asks())
def test_handle_order_book_sends_to_data_engine(self): # Arrange order_book = OrderBook( instrument_id=ETHUSDT_BINANCE.id, level=2, depth=25, price_precision=2, size_precision=5, bids=[], asks=[], update_id=0, timestamp=0, ) # Act self.client._handle_order_book_py(order_book) # Assert self.assertEqual(1, self.data_engine.data_count)
def test_order_book_when_order_book_exists_returns_expected(self): # Arrange order_book = OrderBook( symbol=ETHUSDT_BINANCE.symbol, level=2, price_precision=2, size_precision=2, bids=np.asarray([[1550.15, 0.51], [1580.00, 1.20]]), asks=np.asarray([[1552.15, 1.51], [1582.00, 2.20]]), timestamp=0, ) self.cache.add_order_book(order_book) # Act result = self.cache.order_book(ETHUSDT_BINANCE.symbol) # Assert self.assertEqual(order_book, result)
def test_instantiation(self): # Arrange # Act order_book = OrderBook( instrument_id=ETHUSDT_BINANCE.id, level=2, depth=25, price_precision=2, size_precision=5, bids=[], asks=[], update_id=0, timestamp=0, ) # Act # Assert self.assertEqual(2, order_book.level) self.assertEqual(25, order_book.depth) self.assertEqual(0, order_book.timestamp) self.assertEqual(0, order_book.update_id)
def test_order_book_when_order_book_exists_returns_expected(self): # Arrange order_book = OrderBook( instrument_id=ETHUSDT_BINANCE.id, level=2, depth=25, price_precision=2, size_precision=2, bids=[[1550.15, 0.51], [1580.00, 1.20]], asks=[[1552.15, 1.51], [1582.00, 2.20]], update_id=1, timestamp=0, ) self.cache.add_order_book(order_book) # Act result = self.cache.order_book(ETHUSDT_BINANCE.id) # Assert self.assertEqual(order_book, result)
def test_str_and_repr(self): # Arrange bids = np.asarray([[1550.15, 0.51], [1580.00, 1.20]]) asks = np.asarray([[1552.15, 1.51], [1582.00, 2.20]]) # Act order_book = OrderBook( symbol=ETHUSDT_BINANCE.symbol, level=2, price_precision=2, size_precision=2, bids=bids, asks=asks, timestamp=0, ) # Assert self.assertEqual( "ETH/USDT.BINANCE, bids_len=2, asks_len=2, timestamp=0", str(order_book)) self.assertEqual( "OrderBook(ETH/USDT.BINANCE, bids_len=2, asks_len=2, timestamp=0)", repr(order_book))
def test_apply_snapshot(self): # Arrange order_book = OrderBook( instrument_id=ETHUSDT_BINANCE.id, level=2, depth=25, price_precision=2, size_precision=5, bids=[], asks=[], update_id=0, timestamp=0, ) bids = [ [1002.0, 10.0], [1001.0, 20.0], [1000.8, 30.0], [1000.7, 40.0], [1000.6, 50.0], [1000.5, 60.0], [1000.4, 70.0], [1000.3, 80.0], [1000.2, 90.0], [1000.1, 100.0], ] asks = [ [1003.0, 10.0], [1004.0, 20.0], [1004.1, 30.0], [1004.2, 40.0], [1004.3, 50.0], [1004.4, 60.0], [1004.5, 70.0], [1004.6, 80.0], [1004.7, 90.0], [1004.8, 100.0], ] # Act order_book.apply_snapshot(bids, asks, 1, 1) # Assert self.assertEqual(bids, order_book.bids()) self.assertEqual(asks, order_book.asks()) self.assertEqual(1.0, order_book.spread()) self.assertEqual(1002.0, order_book.best_bid_price()) self.assertEqual(1003.0, order_book.best_ask_price()) self.assertEqual(10.0, order_book.best_bid_qty()) self.assertEqual(10.0, order_book.best_ask_qty())