Skip to content

Commit

Permalink
Refine Bybit quote tick parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Mar 30, 2024
1 parent 3220803 commit ad8902b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
3 changes: 3 additions & 0 deletions nautilus_trader/adapters/bybit/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,9 @@ def _handle_orderbook(self, instrument_type: BybitInstrumentType, raw: bytes) ->
instrument_id: InstrumentId = self._get_cached_instrument_id(symbol)

instrument = self._cache.instrument(instrument_id)
if instrument is None:
self._log.error(f"Cannot parse order book data: no instrument for {instrument_id}")
return

if instrument_id in self._tob_quotes:
quote = msg.data.parse_to_quote_tick(
Expand Down
45 changes: 24 additions & 21 deletions nautilus_trader/adapters/bybit/schemas/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ def parse_to_quote_tick(
self,
instrument_id: InstrumentId,
last_quote: QuoteTick,
price_precision: int | None,
size_precision: int | None,
price_precision: int,
size_precision: int,
ts_event: int,
ts_init: int,
) -> QuoteTick:
Expand All @@ -268,29 +268,32 @@ def parse_to_quote_tick(
top_ask_size = top_ask[1] if top_ask else None

if top_bid_size == "0":
top_bid = None
top_bid_size = None
if top_ask_size == "0":
top_ask = None

# Ensure correct precision:
# (Spot price and size strings are not accurate to precision digits)
if price_precision:
if top_bid_price is not None:
top_bid_price = f"{float(top_bid_price):.{price_precision}f}"
if top_ask_price is not None:
top_ask_price = f"{float(top_ask_price):.{price_precision}f}"
if size_precision:
if top_bid_size is not None:
top_bid_size = f"{float(top_bid_size):.{size_precision}f}"
if top_ask_size is not None:
top_ask_size = f"{float(top_ask_size):.{size_precision}f}"
top_ask_size = None

return QuoteTick(
instrument_id=instrument_id,
bid_price=Price.from_str(top_bid_price) if top_bid else last_quote.bid_price,
ask_price=Price.from_str(top_ask_price) if top_ask else last_quote.ask_price,
bid_size=Quantity.from_str(top_bid_size) if top_bid else last_quote.bid_size,
ask_size=Quantity.from_str(top_ask_size) if top_ask else last_quote.ask_size,
bid_price=(
Price(float(top_bid_price), price_precision)
if top_bid_price
else last_quote.bid_price
),
ask_price=(
Price(float(top_ask_price), price_precision)
if top_ask_price
else last_quote.ask_price
),
bid_size=(
Quantity(float(top_bid_size), size_precision)
if top_bid_size
else last_quote.bid_size
),
ask_size=(
Quantity(float(top_ask_size), size_precision)
if top_ask_size
else last_quote.ask_size
),
ts_event=ts_event,
ts_init=ts_init,
)
Expand Down

0 comments on commit ad8902b

Please sign in to comment.