Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: 'calculate_market_price' uses asks in descending order instead of ascending order #106

Open
hbr-l opened this issue Dec 19, 2024 · 0 comments

Comments

@hbr-l
Copy link

hbr-l commented Dec 19, 2024

Overview

When creating a market buy order, py-clob-client first computes a marketable limit buy price.
The marketable limit price is the price level, at which the cumulative sum of open asks amount is greater equal to the requested amount.
The open asks are sorted in descending order, but should rather be sorted in ascending order (fill cheapest asks first).

Description

https://github.com/Polymarket/py-clob-client/blob/main/py_clob_client/client.py#L725

    def calculate_market_price(self, token_id: str, side: str, amount: float) -> float:
        """
        Calculates the matching price considering an amount and the current orderbook
        """
        book = self.get_order_book(token_id)   # HAS ASKS IN DESCENDING ORDER
        if book is None:
            raise Exception("no orderbook")
        if side == "BUY":
            if book.asks is None:
                raise Exception("no match")
            return self.builder.calculate_market_price(book.asks, amount)
        else:
            if book.bids is None:
                raise Exception("no match")
            return self.builder.calculate_market_price(book.bids, amount)

In book = self.get_order_book, asks are sorted in descending order (REST response).

Impact

When sorted in descending order, the returned price level does not guarantee, that there is enough open ask volume below the computed price level to fill the order (cheapest asks will be filled first).

Suggestion

Not sure, but sort asks in ascending order before passing self.builder.calculate_market_price(...)

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

No branches or pull requests

1 participant