# Orderbook & Price Feed

PaperMarket takes its view of the market directly from the Polymarket CLOB order book.\
The app does not invent its own prices. It always treats the Polymarket book as the source of truth for both execution and valuation.

This page describes how order book snapshots are fetched, how prices are interpreted and how they are used internally.

### Structure of the order book

For each YES or NO token Polymarket exposes a central limit order book (CLOB).\
PaperMarket reads this book in a normalized format with two main sides

* bids list of buy offers from other participants
* asks list of sell offers from other participants

Each level in the book has

* a price in the `[0, 1]` range
* a size in token units

PaperMarket does not modify these levels.\
They are consumed as is and used to drive both simulated fills and mid price calculations.

### Order book snapshots

Internally PaperMarket works with explicit snapshots of the order book.

A typical snapshot fetch

* is scoped to a single token (YES or NO)
* returns the current bids and asks for that token
* is requested just in time when fresh data is needed

The backend takes a snapshot

* when a market is first loaded for display
* whenever a trade simulation needs a reliable view of the book
* when the system needs a current mid price to value positions

The app does not rely only on what is currently drawn in the browser.\
Before simulating a trade the backend always pulls a new snapshot from the Polymarket CLOB API to avoid using stale depth.

### Price interpretation

Polymarket prices are probabilities expressed as dollar values between zero and one.\
PaperMarket keeps this convention without conversion.

For each book level

* `price` is treated as the cost in virtual dollars to buy one unit of the token
* `size` is the number of token units available at that price

PaperMarket does not apply its own ticks or rounding rules beyond what is accepted by the backend validation.\
If a price is outside `[0, 1]` it is rejected at the order level rather than forced into range.

All internal PnL and valuation logic uses these same normalized prices so that the simulation stays aligned with how Polymarket quotes markets.

### Mid price calculation

To value open positions PaperMarket needs a single reference price per token.\
For this it derives a mid price from the best available levels in the book.

For a given snapshot

* the best bid is the highest bid price
* the best ask is the lowest ask price

If both exist, the mid price is computed as

* `mid = (bestBid + bestAsk) / 2`

If only one side is present

* if there is a best bid but no asks, the bid can be used as a conservative proxy
* if there is a best ask but no bids, the ask can be used as a proxy

If the book is empty on both sides the token temporarily has no mid price and the app cannot compute a meaningful current value for that position.\
In this case PaperMarket falls back to showing size and entry price and can delay PnL updates until a valid mid price is available again.

### Using the book for simulated execution

When a trade is simulated the backend walks the snapshot of the book level by level.

For buys

* it starts from the lowest asks and moves upwards
* at each level it takes the minimum of remaining order size, available size and what the cash balance can fund
* it creates trades at the exact prices from the book

For sells

* it starts from the highest bids and moves downwards
* it limits size both by available depth and by the user’s position in that token

No extrapolation is used.\
If there is no liquidity at or inside the relevant price range, the remaining part of the order simply cannot be filled.

This execution logic is always based on the snapshot taken immediately before the simulation step, not on any cached view from the front end.

### Price feed for valuation

Separate from execution, PaperMarket also uses order book data as a continuous price feed for valuation.

For each token with an open position the system periodically

* refreshes the order book snapshot
* recomputes the mid price
* updates the estimated value of the position

Portfolio views then show

* position size
* average entry price based on past trades
* current value based on mid
* unrealized PnL as `currentValue - costBasis`

Because the feed is derived from the live CLOB, position values move in sync with changes on Polymarket rather than according to any local pricing model.

### Handling missing or degraded data

In practice external data can be slow or incomplete.\
PaperMarket treats the Polymarket order book as an external dependency and handles issues explicitly.

Typical behaviours include

* if a snapshot request fails the app returns an error rather than pretending to have executed a trade
* if the book returns with no bids and no asks the system avoids generating mid prices or fills from that snapshot
* if responses are delayed the front end can temporarily display stale data but the backend still requires a fresh snapshot for any new execution

This conservative handling keeps the simulation honest\
either it has a clear view of the current Polymarket book, or it refuses to simulate fills based on assumptions.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://papermarket.gitbook.io/papermarket/market-data-and-charts/orderbook-and-price-feed.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
