Skip to content

Keep V5 orderbook sorted and fix crash on delta delete of absent level - #372

Open
massimiliano1991 wants to merge 1 commit into
bybit-exchange:masterfrom
massimiliano1991:fix/orderbook-sorting-and-delete
Open

Keep V5 orderbook sorted and fix crash on delta delete of absent level#372
massimiliano1991 wants to merge 1 commit into
bybit-exchange:masterfrom
massimiliano1991:fix/orderbook-sorting-and-delete

Conversation

@massimiliano1991

Copy link
Copy Markdown

Addresses the two defects in _process_delta_orderbook reported in #369, scoped to the parts that are uncontroversial correctness fixes. It intentionally does not touch the delta→snapshot re-labelling, which the maintainer noted in #369 is the documented/intended behaviour — this PR leaves that exactly as-is.

What it fixes

  1. Unsorted book. New price levels were appended and the side was never re-sorted. Bybit deltas can add levels anywhere in the range, so after a few insert deltas data[topic]["b"][0] / ["a"][0] stop being the best bid/ask. This PR re-sorts each side after applying a delta (bids high→low, asks low→high), so [0] is always the best level.

  2. Crash on delete of an absent level. A qty-0 delta for a level not currently held made _helpers.find_index raise StopIteration (it wraps a bare next()), which propagated out of _process_normal_message and dropped the message. The delete branch now checks existence first (same pattern already used by the insert branch) and skips a missing level.

What it does not change

  • The type="snapshot" re-labelling and the merged-book payload delivered to callbacks are untouched.
  • find_index's signature/behaviour is untouched (the guard is at the call site).

Tests

Two regression tests added in tests/test_pybit.py:

  • inserting levels inside the range keeps both sides sorted and [0] the best bid/ask;
  • a delete of an absent level is a no-op instead of raising.

Full suite passes locally (pytest tests/test_pybit.py → 46 passed).

Refs #369

_process_delta_orderbook appended new price levels without re-sorting, so
after a few insert deltas b[0]/a[0] stopped being the best bid/ask; and a
qty=0 delta for a level not currently held made find_index raise
StopIteration, dropping the message.

- Re-sort each side after applying a delta (bids high->low, asks low->high)
  so [0] is always the best level.
- Skip a delete whose level is absent instead of raising StopIteration.

Does not change the snapshot/delta re-labelling (intended behaviour).
Adds regression tests; full suite passes.

Refs bybit-exchange#369

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant