Пример #1
0
    def test_order_book_when_order_book_exists_returns_expected(self):
        # Arrange
        snapshot = OrderBookSnapshot(
            instrument_id=ETHUSDT_BINANCE.id,
            book_type=BookType.L2_MBP,
            bids=[[1550.15, 0.51], [1580.00, 1.20]],
            asks=[[1552.15, 1.51], [1582.00, 2.20]],
            ts_event=0,
            ts_init=0,
        )

        order_book = L2OrderBook(
            instrument_id=ETHUSDT_BINANCE.id,
            price_precision=2,
            size_precision=8,
        )
        order_book.apply_snapshot(snapshot)

        self.cache.add_order_book(order_book)

        # Act
        result = self.cache.order_book(ETHUSDT_BINANCE.id)

        # Assert
        assert result == order_book
def test_l2_feed():
    book = L2OrderBook(
        instrument_id=TestStubs.audusd_id(),
        price_precision=5,
        size_precision=0,
    )

    # Duplicate delete messages
    skip = [
        (12152, "378a3caf-0262-4d8b-95b6-8df65312b9f3"),
        (28646, "8101452c-8a80-4ca9-b0d9-c472691cec28"),
        (68431, "8913f4bf-cc49-4e23-b05d-5eeed948a454"),
    ]
    i = 0
    for i, m in enumerate(TestDataProvider.l2_feed()):
        if not m or m["op"] == "trade":
            pass
        elif (i, m["order"].id) in skip:
            continue
        elif m["op"] == "update":
            book.update(order=m["order"])
        elif m["op"] == "delete":
            book.delete(order=m["order"])
        book.check_integrity()
    assert i == 68462
Пример #3
0
def test_orderbook_updates(betfair_data_client):
    order_books = {}
    for raw in BetfairTestStubs.streaming_market_updates():
        for update in on_market_update(
                update=orjson.loads(raw),
                instrument_provider=betfair_data_client.instrument_provider(),
        ):
            if len(order_books) > 1 and update.instrument_id != list(
                    order_books)[1]:
                continue
            print(update)
            if isinstance(update, OrderBookSnapshot):
                order_books[update.instrument_id] = L2OrderBook(
                    instrument_id=update.instrument_id,
                    price_precision=4,
                    size_precision=4,
                )
                order_books[update.instrument_id].apply_snapshot(update)
            elif isinstance(update, OrderBookDeltas):
                order_books[update.instrument_id].apply_deltas(update)
            elif isinstance(update, TradeTick):
                pass
            else:
                raise KeyError

    book = order_books[list(order_books)[0]]
    assert (book.pprint() == """bids       price   asks
--------  -------  ---------
          0.8621   [932.64]
          0.8547   [1275.83]
          0.8475   [151.96]
[147.79]  0.8403
[156.74]  0.8333
[11.19]   0.8197""")
    def test_order_book_when_order_book_exists_returns_expected(self):
        # Arrange
        snapshot = OrderBookSnapshot(
            instrument_id=ETHUSDT_BINANCE.id,
            level=BookLevel.L2,
            bids=[[1550.15, 0.51], [1580.00, 1.20]],
            asks=[[1552.15, 1.51], [1582.00, 2.20]],
            ts_event_ns=0,
            ts_recv_ns=0,
        )

        order_book = L2OrderBook(
            instrument_id=ETHUSDT_BINANCE.id,
            price_precision=2,
            size_precision=8,
        )
        order_book.apply_snapshot(snapshot)

        self.cache.add_order_book(order_book)

        # Act
        result = self.cache.order_book(ETHUSDT_BINANCE.id)

        # Assert
        self.assertEqual(order_book, result)
Пример #5
0
 def test_betfair_orderbook(self):
     book = L2OrderBook(
         instrument_id=BetfairTestStubs.instrument_id(),
         price_precision=2,
         size_precision=2,
     )
     for update in BetfairDataProvider.raw_market_updates():
         for message in on_market_update(
                 instrument_provider=self.instrument_provider,
                 update=update):
             try:
                 if isinstance(message, OrderBookSnapshot):
                     book.apply_snapshot(message)
                 elif isinstance(message, OrderBookDeltas):
                     book.apply_deltas(message)
                 elif isinstance(message, OrderBookDelta):
                     book.apply_delta(message)
                 elif isinstance(message,
                                 (Ticker, TradeTick, InstrumentStatusUpdate,
                                  InstrumentClosePrice)):
                     pass
                 else:
                     raise NotImplementedError(str(type(message)))
                 book.check_integrity()
             except Exception as ex:
                 print(str(type(ex)) + " " + str(ex))
    def test_orderbook_updates(self):
        order_books = {}
        for raw_update in BetfairStreaming.market_updates():
            for update in on_market_update(
                update=raw_update,
                instrument_provider=self.client.instrument_provider(),
            ):
                if len(order_books) > 1 and update.instrument_id != list(order_books)[1]:
                    continue
                print(update)
                if isinstance(update, OrderBookSnapshot):
                    order_books[update.instrument_id] = L2OrderBook(
                        instrument_id=update.instrument_id,
                        price_precision=4,
                        size_precision=4,
                    )
                    order_books[update.instrument_id].apply_snapshot(update)
                elif isinstance(update, OrderBookDeltas):
                    order_books[update.instrument_id].apply_deltas(update)
                elif isinstance(update, TradeTick):
                    pass
                else:
                    raise KeyError

        book = order_books[list(order_books)[0]]
        expected = """bids       price   asks
--------  -------  ---------
          0.8621   [932.64]
          0.8547   [1275.83]
          0.8475   [151.96]
[147.79]  0.8403
[156.74]  0.8333
[11.19]   0.8197"""
        result = book.pprint()
        assert result == expected
Пример #7
0
def test_init():
    ob = L2OrderBook(
        instrument_id=TestStubs.audusd_id(),
        price_precision=5,
        size_precision=0,
    )
    assert isinstance(ob.bids, Ladder) and isinstance(ob.asks, Ladder)
    assert ob.bids.reverse and not ob.asks.reverse
Пример #8
0
 def test_orderbook_repr(self):
     self.client._on_market_update(BetfairStreaming.mcm_live_IMAGE())
     ob_snap = self.messages[14]
     ob = L2OrderBook(InstrumentId(Symbol("1"), BETFAIR_VENUE), 5, 5)
     ob.apply_snapshot(ob_snap)
     print(ob.pprint())
     assert ob.best_ask_price() == 0.5882353
     assert ob.best_bid_price() == 0.5847953
Пример #9
0
def test_pprint_when_no_orders():
    ob = L2OrderBook(
        instrument_id=TestStubs.audusd_id(),
        price_precision=5,
        size_precision=0,
    )
    result = ob.pprint()

    assert "" == result
Пример #10
0
def test_orderbook_repr(betfair_data_client, data_engine):
    betfair_data_client._on_market_update(
        BetfairTestStubs.streaming_mcm_live_IMAGE())
    ob_snap = data_engine.events[14]
    ob = L2OrderBook(InstrumentId(Symbol("1"), BETFAIR_VENUE), 5, 5)
    ob.apply_snapshot(ob_snap)
    print(ob.pprint())
    assert ob.best_ask_price() == 0.58824
    assert ob.best_bid_price() == 0.58480
Пример #11
0
def test_l2_feed():
    ob = L2OrderBook(TestStubs.audusd_id())

    # Duplicate delete messages
    skip = [
        (12152, "378a3caf-0262-4d8b-95b6-8df65312b9f3"),
        (28646, "8101452c-8a80-4ca9-b0d9-c472691cec28"),
        (68431, "8913f4bf-cc49-4e23-b05d-5eeed948a454"),
    ]
    i = 0
    for i, m in enumerate(TestDataProvider.l2_feed()):
        if not m or (i, m["order"].id) in skip:
            continue
        if m["op"] == "update":
            ob.update(order=m["order"])
        elif m["op"] == "delete":
            ob.delete(order=m["order"])
        ob.check_integrity()
    assert i == 68462
Пример #12
0
def test_betfair_orderbook(betfair_data_client, provider):
    provider.load_all()
    book = L2OrderBook(
        instrument_id=BetfairTestStubs.instrument_id(),
        price_precision=2,
        size_precision=2,
    )
    for update in BetfairTestStubs.raw_market_updates():
        for message in on_market_update(instrument_provider=provider,
                                        update=update):

            if isinstance(message, OrderBookSnapshot):
                book.apply_snapshot(message)
            elif isinstance(message, OrderBookDeltas):
                book.apply_deltas(message)
            elif isinstance(message, OrderBookDelta):
                book.apply_delta(message)
            elif isinstance(
                    message,
                (TradeTick, InstrumentStatusEvent, InstrumentClosePrice)):
                pass
            else:
                raise NotImplementedError(str(type(message)))
Пример #13
0
def empty_l2_book():
    return L2OrderBook(
        instrument_id=TestStubs.audusd_id(),
        price_precision=5,
        size_precision=0,
    )
Пример #14
0
def test_pprint_when_no_orders():
    ob = L2OrderBook(TestStubs.audusd_id())
    result = ob.pprint()

    assert "" == result
Пример #15
0
def test_init():
    ob = L2OrderBook(TestStubs.audusd_id())
    assert isinstance(ob.bids, Ladder) and isinstance(ob.asks, Ladder)
    assert ob.bids.reverse and not ob.asks.reverse
Пример #16
0
def empty_book():
    return L2OrderBook(TestStubs.audusd_id())