Exemplo n.º 1
0
    def test_process_instrument_when_subscribers_then_sends_to_registered_handlers(self):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        handler1 = []
        subscribe1 = Subscribe(
            provider=BINANCE.value,
            data_type=DataType(Instrument, metadata={"InstrumentId": ETHUSDT_BINANCE.id}),
            handler=handler1.append,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        handler2 = []
        subscribe2 = Subscribe(
            provider=BINANCE.value,
            data_type=DataType(Instrument, metadata={"InstrumentId": ETHUSDT_BINANCE.id}),
            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(ETHUSDT_BINANCE)

        # Assert
        self.assertEqual([ETHUSDT_BINANCE.id], self.data_engine.subscribed_instruments)
        self.assertEqual([ETHUSDT_BINANCE], handler1)
        self.assertEqual([ETHUSDT_BINANCE], handler2)
Exemplo n.º 2
0
    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])
Exemplo n.º 3
0
    def test_process_bar_when_subscribers_then_sends_to_registered_handlers(
            self):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        bar_spec = BarSpecification(1000, BarAggregation.TICK, PriceType.MID)
        bar_type = BarType(ETHUSDT_BINANCE.symbol,
                           bar_spec,
                           internal_aggregation=True)

        handler1 = ObjectStorer()
        subscribe1 = Subscribe(
            venue=BINANCE,
            data_type=Bar,
            metadata={"BarType": bar_type},
            handler=handler1.store_2,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        handler2 = ObjectStorer()
        subscribe2 = Subscribe(
            venue=BINANCE,
            data_type=Bar,
            metadata={"BarType": bar_type},
            handler=handler2.store_2,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        self.data_engine.execute(subscribe1)
        self.data_engine.execute(subscribe2)

        bar = Bar(
            Price("1051.00000"),
            Price("1055.00000"),
            Price("1050.00000"),
            Price("1052.00000"),
            Quantity(100),
            UNIX_EPOCH,
        )

        data = BarData(bar_type, bar)

        # Act
        self.data_engine.process(data)

        # Assert
        self.assertEqual([(bar_type, bar)], handler1.get_store())
        self.assertEqual([(bar_type, bar)], handler2.get_store())
Exemplo n.º 4
0
    def test_process_quote_tick_when_subscribers_then_sends_to_registered_handlers(
        self,
    ):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        handler1 = []
        subscribe1 = Subscribe(
            client_id=ClientId(BINANCE.value),
            data_type=DataType(
                QuoteTick, metadata={"InstrumentId": ETHUSDT_BINANCE.id}
            ),
            handler=handler1.append,
            command_id=self.uuid_factory.generate(),
            timestamp_ns=self.clock.timestamp_ns(),
        )

        handler2 = []
        subscribe2 = Subscribe(
            client_id=ClientId(BINANCE.value),
            data_type=DataType(
                QuoteTick, metadata={"InstrumentId": ETHUSDT_BINANCE.id}
            ),
            handler=handler2.append,
            command_id=self.uuid_factory.generate(),
            timestamp_ns=self.clock.timestamp_ns(),
        )

        self.data_engine.execute(subscribe1)
        self.data_engine.execute(subscribe2)

        tick = QuoteTick(
            ETHUSDT_BINANCE.id,
            Price.from_str("100.003"),
            Price.from_str("100.003"),
            Quantity.from_int(1),
            Quantity.from_int(1),
            0,
            0,
        )

        # Act
        self.data_engine.process(tick)

        # Assert
        self.assertEqual([ETHUSDT_BINANCE.id], self.data_engine.subscribed_quote_ticks)
        self.assertEqual([tick], handler1)
        self.assertEqual([tick], handler2)
    def test_process_bar_when_subscribers_then_sends_to_registered_handlers(
            self):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        bar_spec = BarSpecification(1000, BarAggregation.TICK, PriceType.MID)
        bar_type = BarType(ETHUSDT_BINANCE.id,
                           bar_spec,
                           internal_aggregation=True)

        handler1 = ObjectStorer()
        subscribe1 = Subscribe(
            client_id=ClientId(BINANCE.value),
            data_type=DataType(Bar, metadata={"bar_type": bar_type}),
            handler=handler1.store,
            command_id=self.uuid_factory.generate(),
            timestamp_ns=self.clock.timestamp_ns(),
        )

        handler2 = ObjectStorer()
        subscribe2 = Subscribe(
            client_id=ClientId(BINANCE.value),
            data_type=DataType(Bar, metadata={"bar_type": bar_type}),
            handler=handler2.store,
            command_id=self.uuid_factory.generate(),
            timestamp_ns=self.clock.timestamp_ns(),
        )

        self.data_engine.execute(subscribe1)
        self.data_engine.execute(subscribe2)

        bar = Bar(
            bar_type,
            Price.from_str("1051.00000"),
            Price.from_str("1055.00000"),
            Price.from_str("1050.00000"),
            Price.from_str("1052.00000"),
            Quantity.from_int(100),
            0,
            0,
        )

        # Act
        self.data_engine.process(bar)

        # Assert
        self.assertEqual([bar], handler1.get_store())
        self.assertEqual([bar], handler2.get_store())
Exemplo n.º 6
0
    def test_execute_subscribe_order_book_intervals_then_adds_handler(self):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        subscribe = Subscribe(
            client_id=ClientId(BINANCE.value),
            data_type=DataType(
                OrderBook,
                metadata={
                    "InstrumentId": ETHUSDT_BINANCE.id,
                    "Level": 2,
                    "Depth": 25,
                    "Interval": 10,
                },
            ),
            handler=[].append,
            command_id=self.uuid_factory.generate(),
            timestamp_ns=self.clock.timestamp_ns(),
        )

        # Act
        self.data_engine.execute(subscribe)

        # Assert
        self.assertEqual([ETHUSDT_BINANCE.id], self.data_engine.subscribed_order_books)
Exemplo n.º 7
0
    def test_execute_unsubscribe_custom_data(self):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.data_engine.register_client(self.quandl)
        self.binance_client.connect()

        handler = []
        subscribe = Subscribe(
            provider="QUANDL",
            data_type=DataType(str, metadata={"Type": "news"}),
            handler=handler.append,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        self.data_engine.execute(subscribe)

        unsubscribe = Unsubscribe(
            provider="QUANDL",
            data_type=DataType(str, metadata={"Type": "news"}),
            handler=handler.append,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        # Act
        self.data_engine.execute(unsubscribe)

        # Assert
        self.assertEqual(2, self.data_engine.command_count)
        self.assertEqual(["subscribe", "unsubscribe"], self.quandl.calls)
Exemplo n.º 8
0
    def test_process_trade_tick_when_subscriber_then_sends_to_registered_handler(self):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        handler = []
        subscribe = Subscribe(
            client_id=ClientId(BINANCE.value),
            data_type=DataType(
                TradeTick, metadata={"InstrumentId": ETHUSDT_BINANCE.id}
            ),
            handler=handler.append,
            command_id=self.uuid_factory.generate(),
            timestamp_ns=self.clock.timestamp_ns(),
        )

        self.data_engine.execute(subscribe)

        tick = TradeTick(
            ETHUSDT_BINANCE.id,
            Price.from_str("1050.00000"),
            Quantity.from_int(100),
            AggressorSide.BUY,
            TradeMatchId("123456789"),
            0,
            0,
        )

        # Act
        self.data_engine.process(tick)

        # Assert
        self.assertEqual([tick], handler)
Exemplo n.º 9
0
    def test_process_trade_tick_when_subscriber_then_sends_to_registered_handler(self):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        handler = []
        subscribe = Subscribe(
            venue=BINANCE,
            data_type=TradeTick,
            metadata={"Symbol": ETHUSDT_BINANCE.symbol},
            handler=handler.append,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        self.data_engine.execute(subscribe)

        tick = TradeTick(
            ETHUSDT_BINANCE.symbol,
            Price("1050.00000"),
            Quantity(100),
            OrderSide.BUY,
            TradeMatchId("123456789"),
            UNIX_EPOCH,
        )

        # Act
        self.data_engine.process(tick)

        # Assert
        self.assertEqual([tick], handler)
Exemplo n.º 10
0
    def test_unsubscribe_trade_tick_then_unsubscribes(self):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        handler = []
        subscribe = Subscribe(
            venue=BINANCE,
            data_type=TradeTick,
            metadata={"Symbol": ETHUSDT_BINANCE.symbol},
            handler=handler.append,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        self.data_engine.execute(subscribe)

        unsubscribe = Unsubscribe(
            venue=BINANCE,
            data_type=TradeTick,
            metadata={"Symbol": ETHUSDT_BINANCE.symbol},
            handler=handler.append,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        # Act
        self.data_engine.execute(unsubscribe)

        # Assert
        self.assertEqual([], self.data_engine.subscribed_trade_ticks)
Exemplo n.º 11
0
    async def test_message_qsize_at_max_blocks_on_put_data_command(self):
        # Arrange
        self.msgbus.deregister(endpoint="DataEngine.execute",
                               handler=self.engine.execute)
        self.msgbus.deregister(endpoint="DataEngine.process",
                               handler=self.engine.process)
        self.msgbus.deregister(endpoint="DataEngine.request",
                               handler=self.engine.request)
        self.msgbus.deregister(endpoint="DataEngine.response",
                               handler=self.engine.response)

        self.engine = LiveDataEngine(
            loop=self.loop,
            msgbus=self.msgbus,
            cache=self.cache,
            clock=self.clock,
            logger=self.logger,
            config=LiveDataEngineConfig(qsize=1),
        )

        subscribe = Subscribe(
            client_id=ClientId(BINANCE.value),
            data_type=DataType(QuoteTick),
            command_id=self.uuid_factory.generate(),
            ts_init=self.clock.timestamp_ns(),
        )

        # Act
        self.engine.execute(subscribe)
        self.engine.execute(subscribe)
        await asyncio.sleep(0.1)

        # Assert
        assert self.engine.message_qsize() == 1
        assert self.engine.command_count == 0
Exemplo n.º 12
0
    def test_execute_subscribe_order_book_intervals_then_adds_handler(self):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        subscribe = Subscribe(
            venue=BINANCE,
            data_type=OrderBook,
            metadata={
                "Symbol": ETHUSDT_BINANCE.symbol,
                "Level": 2,
                "Depth": 25,
                "Interval": 10,
            },
            handler=[].append,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        # Act
        self.data_engine.execute(subscribe)

        # Assert
        self.assertEqual([ETHUSDT_BINANCE.symbol],
                         self.data_engine.subscribed_order_books)
Exemplo n.º 13
0
    def test_process_quote_tick_when_subscriber_then_sends_to_registered_handler(self):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        handler = []
        subscribe = Subscribe(
            provider=BINANCE.value,
            data_type=DataType(QuoteTick, metadata={"InstrumentId": ETHUSDT_BINANCE.id}),
            handler=handler.append,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        self.data_engine.execute(subscribe)

        tick = QuoteTick(
            ETHUSDT_BINANCE.id,
            Price("100.003"),
            Price("100.003"),
            Quantity(1),
            Quantity(1),
            UNIX_EPOCH,
        )

        # Act
        self.data_engine.process(tick)

        # Assert
        self.assertEqual([ETHUSDT_BINANCE.id], self.data_engine.subscribed_quote_ticks)
        self.assertEqual([tick], handler)
Exemplo n.º 14
0
    def test_execute_unsubscribe_instrument_then_removes_handler(self):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        handler = []
        subscribe = Subscribe(
            client_id=ClientId(BINANCE.value),
            data_type=DataType(
                Instrument, metadata={"InstrumentId": ETHUSDT_BINANCE.id}
            ),
            handler=handler.append,
            command_id=self.uuid_factory.generate(),
            timestamp_ns=self.clock.timestamp_ns(),
        )

        self.data_engine.execute(subscribe)

        unsubscribe = Unsubscribe(
            client_id=ClientId(BINANCE.value),
            data_type=DataType(
                Instrument, metadata={"InstrumentId": ETHUSDT_BINANCE.id}
            ),
            handler=handler.append,
            command_id=self.uuid_factory.generate(),
            timestamp_ns=self.clock.timestamp_ns(),
        )

        # Act
        self.data_engine.execute(unsubscribe)

        # Assert
        self.assertEqual([], self.data_engine.subscribed_instruments)
Exemplo n.º 15
0
    def test_message_qsize_at_max_blocks_on_put_data_command(self):
        # Arrange
        self.engine = LiveDataEngine(
            loop=self.loop,
            portfolio=self.portfolio,
            clock=self.clock,
            logger=self.logger,
            config={"qsize": 1},
        )

        subscribe = Subscribe(
            client_id=ClientId(BINANCE.value),
            data_type=DataType(QuoteTick),
            handler=[].append,
            command_id=self.uuid_factory.generate(),
            timestamp_ns=self.clock.timestamp_ns(),
        )

        # Act
        self.engine.execute(subscribe)
        self.engine.execute(subscribe)

        # Assert
        self.assertEqual(1, self.engine.message_qsize())
        self.assertEqual(0, self.engine.command_count)
Exemplo n.º 16
0
    def test_message_qsize_at_max_blocks_on_put_data_command(self):
        # Arrange
        self.data_engine = LiveDataEngine(
            loop=self.loop,
            portfolio=self.portfolio,
            clock=self.clock,
            logger=self.logger,
            config={"qsize": 1}
        )

        subscribe = Subscribe(
            venue=BINANCE,
            data_type=QuoteTick,
            metadata={},
            handler=[].append,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        # Act
        self.data_engine.execute(subscribe)
        self.data_engine.execute(subscribe)

        # Assert
        self.assertEqual(1, self.data_engine.message_qsize())
        self.assertEqual(0, self.data_engine.command_count)
Exemplo n.º 17
0
    def test_unsubscribe_bar_type_then_unsubscribes(self):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        bar_spec = BarSpecification(1000, BarAggregation.TICK, PriceType.MID)
        bar_type = BarType(ETHUSDT_BINANCE.id, bar_spec, internal_aggregation=True)

        handler = ObjectStorer()
        subscribe = Subscribe(
            provider=BINANCE.value,
            data_type=DataType(Bar, metadata={"BarType": bar_type}),
            handler=handler.store_2,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        self.data_engine.execute(subscribe)

        unsubscribe = Unsubscribe(
            provider=BINANCE.value,
            data_type=DataType(Bar, metadata={"BarType": bar_type}),
            handler=handler.store_2,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        # Act
        self.data_engine.execute(unsubscribe)

        # Assert
        self.assertEqual([], self.data_engine.subscribed_bars)
Exemplo n.º 18
0
    def test_data_command_str_and_repr(self):
        # Arrange
        # Act
        handler = [].append
        command_id = self.uuid_factory.generate()

        command = Subscribe(
            provider=BINANCE.value,
            data_type=DataType(str, {"type": "newswire"}),  # str data type is invalid
            handler=handler,
            command_id=command_id,
            command_timestamp=self.clock.utc_now(),
        )

        # Assert
        self.assertEqual("Subscribe(<str> {'type': 'newswire'})", str(command))
        self.assertEqual(
            f"Subscribe("
            f"provider=BINANCE, "
            f"data_type=<str> {{'type': 'newswire'}}, "
            f"handler={repr(handler)}, "
            f"id={command_id}, "
            f"timestamp=1970-01-01 00:00:00+00:00)",
            repr(command),
        )
Exemplo n.º 19
0
    def test_execute_unsubscribe_for_quote_ticks(self):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        handler = []
        subscribe = Subscribe(
            provider=BINANCE.value,
            data_type=DataType(QuoteTick, metadata={"InstrumentId": ETHUSDT_BINANCE.id}),
            handler=handler.append,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        self.data_engine.execute(subscribe)

        unsubscribe = Unsubscribe(
            provider=BINANCE.value,
            data_type=DataType(QuoteTick, metadata={"InstrumentId": ETHUSDT_BINANCE.id}),
            handler=handler.append,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        # Act
        self.data_engine.execute(unsubscribe)

        # Assert
        self.assertEqual([], self.data_engine.subscribed_quote_ticks)
    def test_data_command_str_and_repr(self):
        # Arrange
        # Act
        handler = [].append
        command_id = self.uuid_factory.generate()

        command = Subscribe(
            client_id=ClientId(BINANCE.value),
            data_type=DataType(
                str, {"type": "newswire"}),  # str data type is invalid
            handler=handler,
            command_id=command_id,
            timestamp_ns=self.clock.timestamp_ns(),
        )

        # Assert
        self.assertEqual("Subscribe(<str> {'type': 'newswire'})", str(command))
        self.assertEqual(
            f"Subscribe("
            f"client_id=BINANCE, "
            f"data_type=<str> {{'type': 'newswire'}}, "
            f"handler={repr(handler)}, "
            f"id={command_id})",
            repr(command),
        )
Exemplo n.º 21
0
    def test_process_trade_tick_when_subscribers_then_sends_to_registered_handlers(
        self,
    ):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        handler1 = []
        subscribe1 = Subscribe(
            client_name=BINANCE.value,
            data_type=DataType(
                TradeTick, metadata={"InstrumentId": ETHUSDT_BINANCE.id}
            ),
            handler=handler1.append,
            command_id=self.uuid_factory.generate(),
            timestamp_ns=self.clock.timestamp_ns(),
        )

        handler2 = []
        subscribe2 = Subscribe(
            client_name=BINANCE.value,
            data_type=DataType(
                TradeTick, metadata={"InstrumentId": ETHUSDT_BINANCE.id}
            ),
            handler=handler2.append,
            command_id=self.uuid_factory.generate(),
            timestamp_ns=self.clock.timestamp_ns(),
        )

        self.data_engine.execute(subscribe1)
        self.data_engine.execute(subscribe2)

        tick = TradeTick(
            ETHUSDT_BINANCE.id,
            Price("1050.00000"),
            Quantity(100),
            OrderSide.BUY,
            TradeMatchId("123456789"),
            0,
        )

        # Act
        self.data_engine.process(tick)

        # Assert
        self.assertEqual([tick], handler1)
        self.assertEqual([tick], handler2)
Exemplo n.º 22
0
    def test_process_quote_tick_when_subscribers_then_sends_to_registered_handlers(
            self):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        handler1 = []
        subscribe1 = Subscribe(
            venue=BINANCE,
            data_type=QuoteTick,
            metadata={"Symbol": ETHUSDT_BINANCE.symbol},
            handler=handler1.append,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        handler2 = []
        subscribe2 = Subscribe(
            venue=BINANCE,
            data_type=QuoteTick,
            metadata={"Symbol": ETHUSDT_BINANCE.symbol},
            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)

        tick = QuoteTick(
            ETHUSDT_BINANCE.symbol,
            Price("100.003"),
            Price("100.003"),
            Quantity(1),
            Quantity(1),
            UNIX_EPOCH,
        )

        # Act
        self.data_engine.process(tick)

        # Assert
        self.assertEqual([ETHUSDT_BINANCE.symbol],
                         self.data_engine.subscribed_quote_ticks)
        self.assertEqual([tick], handler1)
        self.assertEqual([tick], handler2)
    def test_data_messages_when_client_id_and_venue_none_raise_value_error(
            self):
        # Arrange, Act , Assert
        with pytest.raises(ValueError) as ex:
            Subscribe(
                client_id=None,
                venue=None,
                data_type=DataType(str, {"type": "newswire"}),
                command_id=self.uuid_factory.generate(),
                ts_init=self.clock.timestamp_ns(),
            )
        assert ex.type == ValueError
        assert ex.match("Both `client_id` and `venue` were None")

        with pytest.raises(ValueError) as ex:
            Unsubscribe(
                client_id=None,
                venue=None,
                data_type=DataType(str, {"type": "newswire"}),
                command_id=self.uuid_factory.generate(),
                ts_init=self.clock.timestamp_ns(),
            )
        assert ex.type == ValueError
        assert ex.match("Both `client_id` and `venue` were None")

        with pytest.raises(ValueError) as ex:
            handler = []
            DataRequest(
                client_id=None,
                venue=None,
                data_type=DataType(QuoteTick),
                callback=handler.append,
                request_id=self.uuid_factory.generate(),
                ts_init=self.clock.timestamp_ns(),
            )
        assert ex.type == ValueError
        assert ex.match("Both `client_id` and `venue` were None")

        with pytest.raises(ValueError) as ex:
            DataResponse(
                client_id=None,
                venue=None,
                data_type=DataType(QuoteTick),
                data=[],
                correlation_id=self.uuid_factory.generate(),
                response_id=self.uuid_factory.generate(),
                ts_init=self.clock.timestamp_ns(),
            )
        assert ex.type == ValueError
        assert ex.match("Both `client_id` and `venue` were None")
    def test_command_serializer_methods_raise_not_implemented_error(self):
        # Arrange
        command = Subscribe(
            provider="SIM",
            data_type=DataType(QuoteTick),
            handler=[].append,
            command_id=uuid4(),
            command_timestamp=UNIX_EPOCH,
        )

        serializer = CommandSerializer()

        # Act
        # Assert
        self.assertRaises(NotImplementedError, serializer.serialize, command)
        self.assertRaises(NotImplementedError, serializer.deserialize, bytes())
Exemplo n.º 25
0
    def test_process_order_book_snapshot_when_one_subscriber_then_sends_to_registered_handler(
        self,
    ):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        self.data_engine.process(
            ETHUSDT_BINANCE
        )  # <-- add necessary instrument for test

        handler = []
        subscribe = Subscribe(
            client_id=ClientId(BINANCE.value),
            data_type=DataType(
                OrderBook,
                {
                    "InstrumentId": ETHUSDT_BINANCE.id,
                    "Level": OrderBookLevel.L2,
                    "Depth": 25,
                    "Interval": 0,  # Streaming
                },
            ),
            handler=handler.append,
            command_id=self.uuid_factory.generate(),
            timestamp_ns=self.clock.timestamp_ns(),
        )

        self.data_engine.execute(subscribe)

        snapshot = OrderBookSnapshot(
            instrument_id=ETHUSDT_BINANCE.id,
            level=OrderBookLevel.L2,
            bids=[[1000, 1]],
            asks=[[1001, 1]],
            timestamp_origin_ns=0,
            timestamp_ns=0,
        )

        # Act
        self.data_engine.process(snapshot)

        # Assert
        assert self.data_engine.subscribed_order_books == [ETHUSDT_BINANCE.id]
        assert handler[0].instrument_id == ETHUSDT_BINANCE.id
        assert type(handler[0]) == L2OrderBook
Exemplo n.º 26
0
    def test_execute_subscribe_when_data_type_unrecognized_logs_and_does_nothing(self):
        # Arrange
        self.data_engine.register_client(self.binance_client)

        subscribe = Subscribe(
            provider=BINANCE.value,
            data_type=DataType(str),  # str data type is invalid
            handler=[].append,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        # Act
        self.data_engine.execute(subscribe)

        # Assert
        self.assertEqual(1, self.data_engine.command_count)
Exemplo n.º 27
0
    def test_execute_subscribe_instrument_then_adds_handler(self):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        subscribe = Subscribe(
            provider=BINANCE.value,
            data_type=DataType(Instrument, metadata={"InstrumentId": ETHUSDT_BINANCE.id}),
            handler=[].append,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        # Act
        self.data_engine.execute(subscribe)

        # Assert
        self.assertEqual([ETHUSDT_BINANCE.id], self.data_engine.subscribed_instruments)
    def test_data_command_str_and_repr(self):
        # Arrange, Act
        command_id = self.uuid_factory.generate()

        command = Subscribe(
            client_id=None,
            venue=BINANCE,
            data_type=DataType(str, {"type": "newswire"}),
            command_id=command_id,
            ts_init=self.clock.timestamp_ns(),
        )

        # Assert
        assert str(command) == "Subscribe(str{'type': 'newswire'})"
        assert repr(command) == (f"Subscribe("
                                 f"client_id=None, "
                                 f"venue=BINANCE, "
                                 f"data_type=str{{'type': 'newswire'}}, "
                                 f"id={command_id})")
Exemplo n.º 29
0
    def test_command_serializer_methods_raise_not_implemented_error(self):
        # Arrange
        command = Subscribe(
            client_id=ClientId("SIM"),
            data_type=DataType(QuoteTick),
            handler=[].append,
            command_id=uuid4(),
            timestamp_ns=0,
        )

        serializer = CommandSerializer()

        # Act
        # Assert
        with pytest.raises(NotImplementedError):
            serializer.serialize(command)

        with pytest.raises(NotImplementedError):
            serializer.deserialize(bytes())
Exemplo n.º 30
0
    def test_execute_subscribe_when_already_subscribed_does_not_add_and_logs(self):
        # Arrange
        self.data_engine.register_client(self.binance_client)
        self.binance_client.connect()

        subscribe = Subscribe(
            provider=BINANCE.value,
            data_type=DataType(QuoteTick, metadata={"InstrumentId": ETHUSDT_BINANCE.id}),
            handler=[].append,
            command_id=self.uuid_factory.generate(),
            command_timestamp=self.clock.utc_now(),
        )

        # Act
        self.data_engine.execute(subscribe)
        self.data_engine.execute(subscribe)

        # Assert
        self.assertEqual(2, self.data_engine.command_count)