Use case: You’re a solver or aggregator evaluating whether to bid on an intent. Instead of requesting a firm quote (which has rate limits and expiry), you estimate the execution price from the live stream to decide if the trade is worth pursuing.
How It Works
The Price API streams order book levels as(price, size) pairs, sorted best-first. To estimate the execution price for a target trade size, you walk through these levels from best to worst, accumulating volume until you’ve filled the target amount.
The VWAP is the notional-weighted average price across all levels you’d consume.
The Algorithm
Step-by-Step Breakdown
1
Sort levels by best price
For a buy, sort asks lowest-first (cheapest prices first).
For a sell, sort bids highest-first (best bid prices first).
2
Walk through levels
For each level, calculate the notional value (
price × size). Take the lesser of the level’s notional and your remaining target - this handles partial fills on the last level.3
Accumulate
Track total base tokens filled and total quote spent. The ratio gives you the VWAP.
4
Check for sufficient liquidity
If
remaining > 0 after exhausting all levels, the stream doesn’t have enough depth for your size. You may want to fall back to a firm quote or split across sources.Full Example
Combining the VWAP estimation with the Price API stream from the Quickstart:Key Considerations
- Check for sufficient depth. If
unfilled > 0after exhausting all levels, the stream doesn’t have enough liquidity for your trade size. Fall back to a firm quote or split across sources. - VWAP diverges from top-of-book at size. For small trades the top level is a reasonable proxy. For larger sizes, the VWAP will be meaningfully worse - that’s the whole point of estimating it.
- Tier cadence varies between pairs. Two pairs with the same top-of-book price can have very different depth profiles. Always estimate at your actual trade size rather than assuming uniform depth.
- Stream prices update frequently. Re-estimate on each new message rather than caching stale VWAP values.