Ejemplo n.º 1
0
    def test_handle_trade_tick_when_value_below_threshold_updates(self):
        # Arrange
        bar_store = ObjectStorer()
        handler = bar_store.store
        instrument_id = TestStubs.audusd_id()
        bar_spec = BarSpecification(100000, BarAggregation.VALUE,
                                    PriceType.LAST)
        bar_type = BarType(instrument_id, bar_spec)
        aggregator = ValueBarAggregator(
            AUDUSD_SIM,
            bar_type,
            handler,
            Logger(TestClock()),
        )

        tick1 = TradeTick(
            instrument_id=AUDUSD_SIM.id,
            price=Price.from_str("15000.00"),
            size=Quantity.from_str("3.5"),
            aggressor_side=AggressorSide.BUY,
            match_id="123456",
            ts_event_ns=0,
            ts_recv_ns=0,
        )

        # Act
        aggregator.handle_trade_tick(tick1)

        # Assert
        self.assertEqual(0, len(bar_store.get_store()))
        self.assertEqual(Decimal("52500.000"),
                         aggregator.get_cumulative_value())
Ejemplo n.º 2
0
    def test_handle_quote_tick_when_value_below_threshold_updates(self):
        # Arrange
        bar_store = ObjectStorer()
        handler = bar_store.store
        instrument_id = TestStubs.audusd_id()
        bar_spec = BarSpecification(100000, BarAggregation.VALUE,
                                    PriceType.BID)
        bar_type = BarType(instrument_id, bar_spec)
        aggregator = ValueBarAggregator(
            AUDUSD_SIM,
            bar_type,
            handler,
            Logger(TestClock()),
        )

        tick1 = QuoteTick(
            instrument_id=AUDUSD_SIM.id,
            bid=Price.from_str("1.00001"),
            ask=Price.from_str("1.00004"),
            bid_size=Quantity.from_int(3000),
            ask_size=Quantity.from_int(2000),
            ts_event_ns=0,
            ts_recv_ns=0,
        )

        # Act
        aggregator.handle_quote_tick(tick1)

        # Assert
        self.assertEqual(0, len(bar_store.get_store()))
        self.assertEqual(Decimal("3000.03000"),
                         aggregator.get_cumulative_value())
Ejemplo n.º 3
0
    def test_handle_quote_tick_when_value_beyond_threshold_sends_bar_to_handler(
            self):
        # Arrange
        bar_store = ObjectStorer()
        handler = bar_store.store
        instrument_id = TestStubs.audusd_id()
        bar_spec = BarSpecification(100000, BarAggregation.VALUE,
                                    PriceType.BID)
        bar_type = BarType(instrument_id, bar_spec)
        aggregator = ValueBarAggregator(bar_type, handler, Logger(TestClock()))

        tick1 = QuoteTick(
            instrument_id=AUDUSD_SIM.id,
            bid=Price.from_str("1.00001"),
            ask=Price.from_str("1.00004"),
            bid_size=Quantity.from_int(20000),
            ask_size=Quantity.from_int(20000),
            timestamp_origin_ns=0,
            timestamp_ns=0,
        )

        tick2 = QuoteTick(
            instrument_id=AUDUSD_SIM.id,
            bid=Price.from_str("1.00002"),
            ask=Price.from_str("1.00005"),
            bid_size=Quantity.from_int(60000),
            ask_size=Quantity.from_int(20000),
            timestamp_origin_ns=0,
            timestamp_ns=0,
        )

        tick3 = QuoteTick(
            instrument_id=AUDUSD_SIM.id,
            bid=Price.from_str("1.00000"),
            ask=Price.from_str("1.00003"),
            bid_size=Quantity.from_int(30500),
            ask_size=Quantity.from_int(20000),
            timestamp_origin_ns=0,
            timestamp_ns=0,
        )

        # Act
        aggregator.handle_quote_tick(tick1)
        aggregator.handle_quote_tick(tick2)
        aggregator.handle_quote_tick(tick3)

        # Assert
        self.assertEqual(1, len(bar_store.get_store()))
        self.assertEqual(Price.from_str("1.00001"),
                         bar_store.get_store()[0].open)
        self.assertEqual(Price.from_str("1.00002"),
                         bar_store.get_store()[0].high)
        self.assertEqual(Price.from_str("1.00000"),
                         bar_store.get_store()[0].low)
        self.assertEqual(Price.from_str("1.00000"),
                         bar_store.get_store()[0].close)
        self.assertEqual(Quantity.from_str("99999"),
                         bar_store.get_store()[0].volume)
        self.assertEqual(Decimal("10501.400"),
                         aggregator.get_cumulative_value())
Ejemplo n.º 4
0
    def test_handle_trade_tick_when_volume_beyond_threshold_sends_bars_to_handler(
            self):
        # Arrange
        bar_store = ObjectStorer()
        handler = bar_store.store
        instrument_id = TestStubs.audusd_id()
        bar_spec = BarSpecification(100000, BarAggregation.VALUE,
                                    PriceType.LAST)
        bar_type = BarType(instrument_id, bar_spec)
        aggregator = ValueBarAggregator(bar_type, handler, Logger(TestClock()))

        tick1 = TradeTick(
            instrument_id=AUDUSD_SIM.id,
            price=Price("20.00001"),
            size=Quantity("3000.00"),
            aggressor_side=AggressorSide.BUY,
            match_id=TradeMatchId("123456"),
            timestamp_ns=0,
        )

        tick2 = TradeTick(
            instrument_id=AUDUSD_SIM.id,
            price=Price("20.00002"),
            size=Quantity("4000.00"),
            aggressor_side=AggressorSide.BUY,
            match_id=TradeMatchId("123457"),
            timestamp_ns=0,
        )

        tick3 = TradeTick(
            instrument_id=AUDUSD_SIM.id,
            price=Price("20.00000"),
            size=Quantity("5000.00"),
            aggressor_side=AggressorSide.BUY,
            match_id=TradeMatchId("123458"),
            timestamp_ns=0,
        )

        # Act
        aggregator.handle_trade_tick(tick1)
        aggregator.handle_trade_tick(tick2)
        aggregator.handle_trade_tick(tick3)

        # Assert
        self.assertEqual(2, len(bar_store.get_store()))
        self.assertEqual(Price("20.00001"), bar_store.get_store()[0].open)
        self.assertEqual(Price("20.00002"), bar_store.get_store()[0].high)
        self.assertEqual(Price("20.00001"), bar_store.get_store()[0].low)
        self.assertEqual(Price("20.00002"), bar_store.get_store()[0].close)
        # self.assertEqual(Quantity("5000.00"), bar_store.get_store()[0].volume)  # TODO: WIP - intermittent?
        self.assertEqual(Price("20.00002"), bar_store.get_store()[1].open)
        self.assertEqual(Price("20.00002"), bar_store.get_store()[1].high)
        self.assertEqual(Price("20.00000"), bar_store.get_store()[1].low)
        self.assertEqual(Price("20.00000"), bar_store.get_store()[1].close)
        # self.assertEqual(Quantity("5000.00"), bar_store.get_store()[1].volume)  # TODO: WIP - intermittent?
        self.assertEqual(
            Decimal("40000.11000"),
            aggregator.get_cumulative_value())  # TODO: WIP - Should be 40000
    def test_handle_trade_tick_when_volume_beyond_threshold_sends_bars_to_handler(
            self):
        # Arrange
        bar_store = ObjectStorer()
        handler = bar_store.store
        instrument_id = TestStubs.audusd_id()
        bar_spec = BarSpecification(100000, BarAggregation.VALUE,
                                    PriceType.LAST)
        bar_type = BarType(instrument_id, bar_spec)
        aggregator = ValueBarAggregator(
            AUDUSD_SIM,
            bar_type,
            handler,
            Logger(TestClock()),
        )

        tick1 = TradeTick(
            instrument_id=AUDUSD_SIM.id,
            price=Price.from_str("20.00001"),
            size=Quantity.from_str("3000.00"),
            aggressor_side=AggressorSide.BUY,
            trade_id="123456",
            ts_event=0,
            ts_init=0,
        )

        tick2 = TradeTick(
            instrument_id=AUDUSD_SIM.id,
            price=Price.from_str("20.00002"),
            size=Quantity.from_str("4000.00"),
            aggressor_side=AggressorSide.BUY,
            trade_id="123457",
            ts_event=0,
            ts_init=0,
        )

        tick3 = TradeTick(
            instrument_id=AUDUSD_SIM.id,
            price=Price.from_str("20.00000"),
            size=Quantity.from_str("5000.00"),
            aggressor_side=AggressorSide.BUY,
            trade_id="123458",
            ts_event=0,
            ts_init=0,
        )

        # Act
        aggregator.handle_trade_tick(tick1)
        aggregator.handle_trade_tick(tick2)
        aggregator.handle_trade_tick(tick3)

        # Assert
        assert len(bar_store.get_store()) == 2
        assert bar_store.get_store()[0].open == Price.from_str("20.00001")
        assert bar_store.get_store()[0].high == Price.from_str("20.00002")
        assert bar_store.get_store()[0].low == Price.from_str("20.00001")
        assert bar_store.get_store()[0].close == Price.from_str("20.00002")
        assert bar_store.get_store()[0].volume == Quantity.from_str("5000.00")
        assert bar_store.get_store()[1].open == Price.from_str("20.00002")
        assert bar_store.get_store()[1].high == Price.from_str("20.00002")
        assert bar_store.get_store()[1].low == Price.from_str("20.00000")
        assert bar_store.get_store()[1].close == Price.from_str("20.00000")
        assert bar_store.get_store()[1].volume == Quantity.from_str("5000.00")
        assert aggregator.get_cumulative_value() == Decimal("40000.11000")