Skip to content

fix: sort-agnostic orderbook walk for market price calculation#23

Closed
deipeypaanchal wants to merge 1 commit into
Polymarket:mainfrom
deipeypaanchal:fix/orderbook-walk-sort-agnostic
Closed

fix: sort-agnostic orderbook walk for market price calculation#23
deipeypaanchal wants to merge 1 commit into
Polymarket:mainfrom
deipeypaanchal:fix/orderbook-walk-sort-agnostic

Conversation

@deipeypaanchal

@deipeypaanchal deipeypaanchal commented Apr 17, 2026

Copy link
Copy Markdown

Summary

  • calculate_buy_market_price / calculate_sell_market_price iterated reversed(positions), assuming the server returns asks descending and bids ascending. That's an undocumented contract. If the server ever changes ordering (or a caller passes a pre-sorted snapshot), the walk returns the worst price rather than the marginal price required to fill the amount.
  • Sort locally instead:
    • buys walk asks ascending (cheapest first)
    • sells walk bids descending (highest first)
  • Added a _normalize_positions helper to consolidate the dict-vs-dataclass access.

Test plan

  • BUY walk returns the same marginal price for ascending and descending ask inputs.
  • SELL walk returns the same marginal price for ascending and descending bid inputs.
  • FOK with insufficient book still raises.
  • FAK with insufficient book returns the worst price walked.

🤖 Generated with Claude Code


Note

Medium Risk
Changes the core market-price calculation used for building market orders, which can alter execution price selection and edge-case behavior when books are partially fillable.

Overview
Fixes market price estimation to be independent of server/orderbook input ordering.

calculate_buy_market_price and calculate_sell_market_price now normalize position entries (dict vs object), locally sort by price (asks ascending, bids descending), and return the marginal price needed to fill amount_to_match; for non-FOK orders with insufficient depth, they return the worst price reached during the walk instead of relying on the first entry.

Reviewed by Cursor Bugbot for commit 8c81a12. Bugbot is set up for automated code reviews on this repo. Configure here.

`calculate_buy_market_price` and `calculate_sell_market_price` iterated
`reversed(positions)` under the assumption that the server returns asks
in descending-price order and bids in ascending-price order. That's an
undocumented contract — if the server ever changes ordering (or a caller
passes a pre-sorted snapshot), the walk returns the *worst* price
instead of the marginal price required to fill the requested amount.

Sort locally by price instead:
  * buys walk asks ascending (cheapest first)
  * sells walk bids descending (highest first)

Also consolidate the dict/dataclass position parsing into a single
`_normalize_positions` helper.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@deipeypaanchal deipeypaanchal closed this by deleting the head repository May 23, 2026
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.

2 participants