Keep V5 orderbook sorted and fix crash on delta delete of absent level - #372
Open
massimiliano1991 wants to merge 1 commit into
Open
Conversation
_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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses the two defects in
_process_delta_orderbookreported 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
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 deltasdata[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.Crash on delete of an absent level. A qty-
0delta for a level not currently held made_helpers.find_indexraiseStopIteration(it wraps a barenext()), which propagated out of_process_normal_messageand 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
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:[0]the best bid/ask;Full suite passes locally (
pytest tests/test_pybit.py→ 46 passed).Refs #369