Skip to content

Commit

Permalink
Fix Binance full depth order book subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Aug 31, 2023
1 parent 3a39263 commit 970d551
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ None
- Fixed `OrderBookDelta.clear` method (where the `sequence` field was swapped with `flags` causing an overflow)
- Fixed `OrderManager` OTO contingency handling on fills
- Fixed `Cache` loading of initialized emulated orders (were not being correctly indexed as emulated)
- Fixed Binance order book subscriptions for deltas at full depth (was not requesting initial snapshot), thanks for reporting @doublier1

---

Expand Down
19 changes: 10 additions & 9 deletions nautilus_trader/adapters/binance/common/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,9 @@ async def _subscribe_order_book( # noqa (too complex)
)
return

if depth is None:
depth = 0
if not depth:
# Reasonable default for full book, which works for Spot and Futures
depth = 1000

# Add delta stream buffer
self._book_buffer[instrument_id] = []
Expand All @@ -305,19 +306,19 @@ async def _subscribe_order_book( # noqa (too complex)
depth=depth,
speed=update_speed,
)

snapshot = await self._http_market.request_order_book_snapshot(
instrument_id=instrument_id,
limit=depth,
ts_init=self._clock.timestamp_ns(),
)
self._handle_data(snapshot)
else:
await self._ws_client.subscribe_diff_book_depth(
symbol=instrument_id.symbol.value,
speed=update_speed,
)

snapshot = await self._http_market.request_order_book_snapshot(
instrument_id=instrument_id,
limit=depth,
ts_init=self._clock.timestamp_ns(),
)
self._handle_data(snapshot)

book_buffer = self._book_buffer.pop(instrument_id, [])
for deltas in book_buffer:
if snapshot and deltas.sequence <= snapshot.sequence:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def on_start(self) -> None:
self.subscribe_quote_ticks(self.instrument_id)
# self.subscribe_trade_ticks(self.instrument_id)
# self.subscribe_ticker(self.instrument_id) # For debugging
# self.subscribe_order_book_deltas(self.instrument_id, depth=50) # For debugging
# self.subscribe_order_book_deltas(self.instrument_id) # For debugging
# self.subscribe_order_book_snapshots(
# self.instrument_id,
# depth=20,
Expand Down
6 changes: 4 additions & 2 deletions nautilus_trader/model/data/book.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ cdef class OrderBookDeltas(Data):
"""The order book deltas.\n\n:returns: `list[OrderBookDelta]`"""
cdef readonly bint is_snapshot
"""If the deltas represent a snapshot (an initial CLEAR then deltas).\n\n:returns: `bool`"""
cdef readonly uint64_t sequence
"""If the sequence number for the last delta.\n\n:returns: `bool`"""
cdef readonly uint64_t ts_event
"""The UNIX timestamp (nanoseconds) when the data event occurred.\n\n:returns: `uint64_t`"""
"""The UNIX timestamp (nanoseconds) when the last delta event occurred.\n\n:returns: `uint64_t`"""
cdef readonly uint64_t ts_init
"""The UNIX timestamp (nanoseconds) when the object was initialized.\n\n:returns: `uint64_t`"""
"""The UNIX timestamp (nanoseconds) when the last delta event was initialized.\n\n:returns: `uint64_t`"""


@staticmethod
Expand Down
2 changes: 2 additions & 0 deletions nautilus_trader/model/data/book.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ cdef class OrderBookDeltas(Data):
self.instrument_id = instrument_id
self.deltas = deltas
self.is_snapshot = deltas[0].is_clear
self.sequence = deltas[-1].sequence
self.ts_event = deltas[-1].ts_event
self.ts_init = deltas[-1].ts_init

Expand All @@ -665,6 +666,7 @@ cdef class OrderBookDeltas(Data):
f"instrument_id={self.instrument_id}, "
f"{self.deltas}, "
f"is_snapshot={self.is_snapshot}, "
f"sequence={self.sequence}, "
f"ts_event={self.ts_event}, "
f"ts_init={self.ts_init})"
)
Expand Down

0 comments on commit 970d551

Please sign in to comment.