Repository navigation
Conversation
…uity - asks/bids stay sorted (asks ascending, bids descending) when new price levels arrive; previously new levels were appended to the end of the lists - level updates use bisect over a parallel price index instead of scanning and copying the list per delta (4-18x faster in test/bench_ws_order_book.py) - nonce/begin_nonce/offset/last_updated_at on the local book track the last applied update instead of staying at the snapshot values - each update is checked with the documented continuity rule (begin_nonce == previous nonce); on a gap the book is dropped, the market is resubscribed for a fresh snapshot and on_order_book_gap is called. verify_order_book_continuity=False turns the check off
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.
WsClientkeeps a local copy of each subscribed order book, but it had three problems:asks/bids, so after a level appears inside the book,bids[0]/asks[0]are no longer the best prices.nonce/offsetstayed at the snapshot values forever.begin_nonceequals the previous update'snonce(this is also what Question: How to ensure local orderbook consistency after offsets became non-contiguous? #105 asked about), but the client applied deltas regardless, so a dropped update left a silently wrong book.This PR:
bisectover a parallel price index instead of scanning and copying the list for every deltanonce,begin_nonce,offset,last_updated_aton the local book from each applied updatebegin_nonce == previous nonce; on a mismatch it stops applying deltas for that market, sendsunsubscribe+subscribeto get a fresh snapshot, and calls a new optionalon_order_book_gap(market_id, expected, received)callback. The server'sunsubscribedack for that resync is not reported as an unhandled message.verify_order_book_continuity=Falseturns the check off.run()andrun_async()(the async path awaits the resubscribe sends)update_orders()unchanged for anyone calling it directlyTesting
test/test_ws_client_order_book.py: 11 new tests (sorting, removals, metadata, gap -> resubscribe -> fresh snapshot, async path, disabling the check, and a 3,000-update randomized comparison against a reference book).pytest --ignore-glob="*api.py"passes, flake8 with the CI flags is clean.Benchmark (
python test/bench_ws_order_book.py, 3,000 updates of 1-8 level changes, median of 5 runs, Apple Silicon arm64):