def test_create_level_fail(): # Arrange # Act # Assert with pytest.raises(ValueError): OrderBook.create( instrument=AUDUSD_SIM, book_type=0, )
def test_betfair_orderbook(betfair_data_client, provider): provider.search_markets(market_filter={"market_id": "1.180737206"}) book = OrderBook(instrument_id=BetfairTestStubs.instrument_id(), level=OrderBookLevel.L2) for raw in BetfairTestStubs.raw_orderbook_updates(): update = orjson.loads(_fix_ids(raw.strip())) for operation in on_market_update(update, instrument_provider=provider): book.apply_operation(operation)
def update_midpoint(self, order_book: OrderBook): """ Check if midpoint has moved more than threshold, if so , update quotes. """ midpoint = Decimal(order_book.best_ask_price() + order_book.best_bid_price()) / Decimal(2.0) self.log.info(f"midpoint: {midpoint}, prev: {self.midpoint}") if (abs(midpoint - (self.midpoint or Decimal(-1e15))) > self.theo_change_threshold): self.log.info("Theo updating", LogColor.BLUE) self.midpoint = midpoint
def order_book( instrument=None, book_type=BookType.L2_MBP, bid_price=10, ask_price=15, bid_levels=3, ask_levels=3, bid_volume=10, ask_volume=10, ) -> OrderBook: instrument = instrument or TestInstrumentProvider.default_fx_ccy( "AUD/USD") order_book = OrderBook.create( instrument=instrument, book_type=book_type, ) snapshot = TestDataStubs.order_book_snapshot( instrument_id=instrument.id, bid_price=bid_price, ask_price=ask_price, bid_levels=bid_levels, ask_levels=ask_levels, bid_volume=bid_volume, ask_volume=ask_volume, ) order_book.apply_snapshot(snapshot) return order_book
def on_start(self): """Actions to be performed on strategy start.""" self.instrument = self.cache.instrument(self.instrument_id) if self.instrument is None: self.log.error(f"Could not find instrument for {self.instrument_id}") self.stop() return if self.config.book_type: self.book = OrderBook.create( instrument=self.instrument, book_type=self.config.book_type ) if self.config.snapshots: self.subscribe_order_book_snapshots( instrument_id=self.instrument_id, book_type=self.config.book_type ) else: self.subscribe_order_book_deltas( instrument_id=self.instrument_id, book_type=self.config.book_type ) if self.config.trade_ticks: self.subscribe_trade_ticks(instrument_id=self.instrument_id) if self.config.quote_ticks: self.subscribe_quote_ticks(instrument_id=self.instrument_id)
def test_add_l1_fails(): book = OrderBook.create( instrument=AUDUSD_SIM, level=OrderBookLevel.L1, ) with pytest.raises(NotImplementedError): book.add(TestStubs.order(price=10.0, side=OrderSide.BUY))
def on_data(self, data: Data): # self.log.debug(str(data)) if isinstance(data, InstrumentSearch): # Find and set instrument self.log.info( f"Received {len(data.instruments)} from instrument search") self.instrument_id = data.instruments[0].id # Subscribe to live data self.subscribe_order_book_deltas( instrument_id=self.instrument_id, level=BookLevel.L2, ) self.book = OrderBook( instrument_id=self.instrument_id, level=BookLevel.L2, )
def test_delete_l1(): book = OrderBook.create( instrument=AUDUSD_SIM, book_type=BookType.L1_TBBO, ) order = TestStubs.order(price=10.0, side=OrderSide.BUY) book.update(order) book.delete(order)
def test_delete_l1(): book = OrderBook.create( instrument=AUDUSD_SIM, level=OrderBookLevel.L1, ) order = TestStubs.order(price=10.0, side=OrderSide.BUY) book.update(order) book.delete(order)
def on_data(self, data: GenericData): # self.log.debug(str(data)) if data.data_type.type == InstrumentSearch: # Find and set instrument instrument_search = data.data self.log.info( f"Received {len(instrument_search.instruments)} from instrument search" ) self.instrument_id = instrument_search.instruments[0].id # Subscribe to live data self.subscribe_order_book_deltas( instrument_id=self.instrument_id, level=OrderBookLevel.L2, ) self.book = OrderBook( instrument_id=self.instrument_id, level=OrderBookLevel.L2, )
def on_order_book(self, order_book: OrderBook): """ Actions to be performed when the strategy is running and receives an order book. Parameters ---------- order_book : OrderBook The order book received. """ bid_qty = order_book.best_bid_qty() ask_qty = order_book.best_ask_qty() if bid_qty and ask_qty: imbalance = bid_qty / (bid_qty + ask_qty) if imbalance > 0.90: self.buy() elif imbalance < 0.10: self.sell()
def test_spread_with_no_orders_returns_none(): # Arrange book = OrderBook.create( instrument=AUDUSD_SIM, book_type=BookType.L2_MBP, ) # Act # Assert assert book.spread() is None
def test_spread_with_no_orders_returns_none(): # Arrange book = OrderBook.create( instrument=AUDUSD_SIM, level=OrderBookLevel.L2, ) # Act # Assert assert book.spread() is None
def test_instantiate_base_class_directly_raises_value_error(): # Arrange # Act # Assert with pytest.raises(RuntimeError): OrderBook( instrument_id=AUDUSD_SIM.id, book_type=BookType.L2_MBP, price_precision=5, size_precision=0, )
def test_best_bid_or_ask_qty_with_no_orders_returns_none(): # Arrange book = OrderBook.create( instrument=AUDUSD_SIM, level=OrderBookLevel.L2, ) # Act # Assert assert book.best_bid_qty() is None assert book.best_ask_qty() is None
def test_instantiate_base_class_directly_raises_value_error(self): # Arrange # Act # Assert with pytest.raises(ValueError): OrderBook( instrument_id=AUDUSD, level=OrderBookLevel.L2, price_precision=5, size_precision=0, )
def test_best_bid_or_ask_qty_with_no_orders_returns_none(): # Arrange book = OrderBook.create( instrument=AUDUSD_SIM, book_type=BookType.L2_MBP, ) # Act # Assert assert book.best_bid_qty() is None assert book.best_ask_qty() is None
def test_spread_with_no_orders_returns_none(self): # Arrange book = OrderBook.create( instrument_id=AUDUSD, level=OrderBookLevel.L2, price_precision=2, size_precision=2, ) # Act # Assert assert book.spread() is None
def test_repr(): book = OrderBook.create( instrument=AUDUSD_SIM, level=OrderBookLevel.L2, ) # Act book.add(Order(price=10.0, volume=5.0, side=OrderSide.BUY)) book.add(Order(price=11.0, volume=6.0, side=OrderSide.SELL)) # Assert assert isinstance(repr(book), str) # <-- calls pprint internally
def test_repr(): book = OrderBook.create( instrument=AUDUSD_SIM, book_type=BookType.L2_MBP, ) # Act book.add(Order(price=10.0, size=5.0, side=OrderSide.BUY)) book.add(Order(price=11.0, size=6.0, side=OrderSide.SELL)) # Assert assert isinstance(repr(book), str) # <-- calls pprint internally
def on_start(self): self.instrument = self.cache.instrument(self.instrument_id) if self.instrument is None: self.log.error(f"Could not find instrument for {self.instrument_id}") self.stop() return # Create orderbook self._book = OrderBook.create(instrument=self.instrument, book_type=BookType.L2_MBP) # Subscribe to live data self.subscribe_order_book_deltas(self.instrument_id)
def on_start(self): """Actions to be performed on strategy start.""" # self.request_data( # "BETFAIR", DataType(InstrumentSearch, metadata=self.instrument_filter) # ) self.subscribe_order_book_deltas( instrument_id=self.instrument.id, level=BookLevel.L2, ) self._book = OrderBook.create( instrument=self.instrument, level=BookLevel.L2, )
def test_create_level_3_order_book(self): # Arrange # Act book = OrderBook.create( instrument_id=AUDUSD, level=OrderBookLevel.L3, price_precision=2, size_precision=2, ) # Assert assert isinstance(book, L3OrderBook) assert book.level == OrderBookLevel.L3
def test_repr(self): book = OrderBook.create( instrument_id=AUDUSD, level=OrderBookLevel.L2, price_precision=2, size_precision=2, ) # Act book.add(Order(price=10, volume=5, side=OrderSide.BUY)) book.add(Order(price=11, volume=6, side=OrderSide.SELL)) # Assert assert isinstance(repr(book), str) # <-- calls pprint internally
def test_create_level_3_order_book(): # Arrange # Act book = OrderBook.create( instrument=AUDUSD_SIM, book_type=BookType.L3_MBO, ) # Assert assert isinstance(book, L3OrderBook) assert book.type == BookType.L3_MBO assert isinstance(book.bids, Ladder) and isinstance(book.asks, Ladder) assert book.bids.reverse assert not book.asks.reverse
def test_create_level_3_order_book(): # Arrange # Act book = OrderBook.create( instrument=AUDUSD_SIM, level=OrderBookLevel.L3, ) # Assert assert isinstance(book, L3OrderBook) assert book.level == OrderBookLevel.L3 assert isinstance(book.bids, Ladder) and isinstance(book.asks, Ladder) assert book.bids.reverse assert not book.asks.reverse
def test_add_orders_to_book(): # Arrange book = OrderBook.create( instrument=AUDUSD_SIM, book_type=BookType.L2_MBP, ) # Act book.add(Order(price=10.0, size=5.0, side=OrderSide.BUY)) book.add(Order(price=11.0, size=6.0, side=OrderSide.SELL)) # Assert assert book.best_bid_price() == 10.0 assert book.best_ask_price() == 11.0 assert book.best_bid_qty() == 5.0 assert book.best_ask_qty() == 6.0 assert book.spread() == 1
def on_order_book(self, order_book: OrderBook): """ Actions to be performed when the strategy is running and receives an order book. Parameters ---------- order_book : OrderBook The order book received. """ # self.log.debug( # f"Received {repr(order_book)}" # ) # For debugging (must add a subscription) if order_book.spread(): self.update_midpoint(order_book=order_book) self.log.debug(f"on_order_book {self._in_flight}") self.trigger()
def on_start(self): """Actions to be performed on strategy start.""" self.instrument = self.cache.instrument(self.instrument_id) if self.instrument is None: self.log.error( f"Could not find instrument for {self.instrument_id}") self.stop() return self.subscribe_order_book_deltas( instrument_id=self.instrument.id, book_type=BookType.L2_MBP, ) self._book = OrderBook.create( instrument=self.instrument, book_type=BookType.L2_MBP, )
def test_add_orders_to_book(): # Arrange book = OrderBook.create( instrument=AUDUSD_SIM, level=OrderBookLevel.L2, ) # Act book.add(Order(price=10.0, volume=5.0, side=OrderSide.BUY)) book.add(Order(price=11.0, volume=6.0, side=OrderSide.SELL)) # Assert assert book.best_bid_price() == 10.0 assert book.best_ask_price() == 11.0 assert book.best_bid_qty() == 5.0 assert book.best_ask_qty() == 6.0 assert book.spread() == 1