Repository navigation
Conversation
estimateFill(book, side, size, limit?) walks the opposite side best-first and stops at a market order's protective price. It returns the base filled, the price that fill clears at, its slippage against the best level, and whether the walk ran off the levels it was given. The price follows the batch clearing rule rather than averaging the levels: every match clears at one price and a market order never sets it, so the whole fill pays at least the last level reached. Co-Authored-By: Claude Opus 5.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.
What
estimateFill(book, side, size, limit?)estimates what a taker order ofsizewould fill against anOrderbook. It walks the opposite side best-first (asks for a long, bids for a short) and stops atlimit, which is a market order's protective price. It returns:filledlimit(≤size)priceslippagePctpriceis than the best level, in percentexhaustedlimit. On a depth-truncated book,filledis then a lower boundThe trading app uses it to warn before a market order that would fill only partly, or not at all, within its slippage cap. Today the IOC remainder is canceled and the user is told nothing.
How
pricefollows the batch clearing rule (doc/protocol/orderbook.md, Clearing), not a volume-weighted average. Every match in a batch clears at one price, and a market order never sets it. The clearing range runs from the last level matched to the first level left unmatched (the same level when only part of it was taken). It settles at the midpoint, never past the order's own limit.So the whole fill pays at least the last level reached. With asks of 1 @ 100, 2 @ 101 and 5 @ 103:
priceAssumptions: the order is alone in its batch. Other orders in the same batch can change both the fill and the price. Grouped levels make prices approximate to the grouping.
Testing
npx vitest run src/sync/order-preview.test.tscovers part-taken and whole levels, the limit cap, a level already past the limit, shorts, running off the levels given, and an empty side.🤖 Generated with Claude Code