Beispiel #1
0
    def test_run_quote_ticks_through_aggregator_results_in_expected_bars(self):
        # Arrange
        bar_store = ObjectStorer()
        handler = bar_store.store
        instrument_id = TestStubs.audusd_id()
        bar_spec = BarSpecification(1000, BarAggregation.VALUE, PriceType.MID)
        bar_type = BarType(instrument_id, bar_spec)
        aggregator = ValueBarAggregator(bar_type, handler,
                                        TestLogger(TestClock()))

        wrangler = QuoteTickDataWrangler(
            instrument=AUDUSD_SIM,
            data_quotes=TestDataProvider.audusd_ticks(),
        )

        wrangler.pre_process(instrument_indexer=0)
        ticks = wrangler.build_ticks()

        # Act
        for tick in ticks:
            aggregator.handle_quote_tick(tick)

        # Assert
        last_bar = bar_store.get_store()[-1].bar
        self.assertEqual(67, len(bar_store.get_store()))
        self.assertEqual(Price("0.66921"), last_bar.open)
        self.assertEqual(Price("0.669485"), last_bar.high)
        self.assertEqual(Price("0.669205"), last_bar.low)
        self.assertEqual(Price("0.669475"), last_bar.close)
        self.assertEqual(Quantity(1494), last_bar.volume)
Beispiel #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(bar_type, handler,
                                        TestLogger(TestClock()))

        tick1 = QuoteTick(
            instrument_id=AUDUSD_SIM.id,
            bid=Price("1.00001"),
            ask=Price("1.00004"),
            bid_size=Quantity(3000),
            ask_size=Quantity(2000),
            timestamp=UNIX_EPOCH,
        )

        # Act
        aggregator.handle_quote_tick(tick1)

        # Assert
        self.assertEqual(0, len(bar_store.get_store()))
        self.assertEqual(Decimal("3000.03000"), aggregator.cum_value)
Beispiel #3
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())
    def test_run_quote_ticks_through_aggregator_results_in_expected_bars(self):
        # Arrange
        bar_store = ObjectStorer()
        handler = bar_store.store
        instrument_id = TestStubs.audusd_id()
        bar_spec = BarSpecification(1000, BarAggregation.VALUE, PriceType.MID)
        bar_type = BarType(instrument_id, bar_spec)
        aggregator = ValueBarAggregator(
            AUDUSD_SIM,
            bar_type,
            handler,
            Logger(TestClock()),
        )

        # Setup data
        wrangler = QuoteTickDataWrangler(AUDUSD_SIM)
        provider = TestDataProvider()
        ticks = wrangler.process(
            data=provider.read_csv_ticks("truefx-audusd-ticks.csv")[:10000],
            default_volume=1,
        )

        # Act
        for tick in ticks:
            aggregator.handle_quote_tick(tick)

        # Assert
        last_bar = bar_store.get_store()[-1]
        assert len(bar_store.get_store()) == 6
        assert last_bar.open == Price.from_str("0.671230")
        assert last_bar.high == Price.from_str("0.671330")
        assert last_bar.low == Price.from_str("0.670370")
        assert last_bar.close == Price.from_str("0.670630")
        assert last_bar.volume == Quantity.from_int(1490)
    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())
    def test_handle_quote_tick_when_value_beyond_threshold_sends_bar_to_handler(
            self):
        # Arrange
        bar_store = ObjectStorer()
        handler = bar_store.store
        symbol = TestStubs.symbol_audusd()
        bar_spec = BarSpecification(100000, BarAggregation.VALUE,
                                    PriceType.BID)
        bar_type = BarType(symbol, bar_spec)
        aggregator = ValueBarAggregator(bar_type, handler,
                                        TestLogger(TestClock()))

        tick1 = QuoteTick(
            symbol=AUDUSD_SIM.symbol,
            bid=Price("1.00001"),
            ask=Price("1.00004"),
            bid_size=Quantity(20000),
            ask_size=Quantity(20000),
            timestamp=UNIX_EPOCH,
        )

        tick2 = QuoteTick(
            symbol=AUDUSD_SIM.symbol,
            bid=Price("1.00002"),
            ask=Price("1.00005"),
            bid_size=Quantity(60000),
            ask_size=Quantity(20000),
            timestamp=UNIX_EPOCH,
        )

        tick3 = QuoteTick(
            symbol=AUDUSD_SIM.symbol,
            bid=Price("1.00000"),
            ask=Price("1.00003"),
            bid_size=Quantity(30500),
            ask_size=Quantity(20000),
            timestamp=UNIX_EPOCH,
        )

        # 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("1.00001"), bar_store.get_store()[0].bar.open)
        self.assertEqual(Price("1.00002"), bar_store.get_store()[0].bar.high)
        self.assertEqual(Price("1.00000"), bar_store.get_store()[0].bar.low)
        self.assertEqual(Price('1.00000'), bar_store.get_store()[0].bar.close)
        self.assertEqual(Quantity("99999"),
                         bar_store.get_store()[0].bar.volume)
        self.assertEqual(Decimal("10501.00000"), aggregator.cum_value)