> For the complete documentation index, see [llms.txt](https://docs.hydromancer.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hydromancer.xyz/readme/builder-guides/trading-bots-and-execution-tools.md).

# Trading bots and execution tools

Feed a Hyperliquid trading bot with prices, order books and its own fills, estimate slippage before an order, and follow TWAP orders.

Hydromancer is read-only. Your bot sends orders through Hyperliquid's exchange endpoint and uses Hydromancer for the data around them: what the market looks like before the order, and what happened after it.

Trading bots and execution tools usually need to:

* stream prices and the order book for the markets they trade
* size an order against the available liquidity
* track their own orders and fills
* follow TWAP orders, their own and the market's

**At a glance**

| What you need                | Use                                                                                                                                                                                                                                                                                           |
| ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Best bid and offer           | [bbo](https://docs.hydromancer.xyz/readme/websocket/orderbook-streaming/bbo)                                                                                                                                                                                                                  |
| Order book by price level    | [l2Book](https://docs.hydromancer.xyz/readme/websocket/orderbook-streaming/l2book)                                                                                                                                                                                                            |
| A full local book            | [l2BookDiffSnapshot](https://docs.hydromancer.xyz/readme/rest-api/market-data/l2bookdiffsnapshot), then [l2BookDiff](https://docs.hydromancer.xyz/readme/websocket/orderbook-streaming/l2bookdiff)                                                                                            |
| Oracle and mark prices       | [perpPrices](https://docs.hydromancer.xyz/readme/websocket/prices-data-and-asset-context/perpprices)                                                                                                                                                                                          |
| Market rules and status      | [meta](https://docs.hydromancer.xyz/readme/rest-api/metadata/meta), [exchangeStatus](https://docs.hydromancer.xyz/readme/rest-api/metadata/exchangestatus)                                                                                                                                    |
| Expected slippage for a size | [slippageHistory](https://docs.hydromancer.xyz/readme/rest-api/market-data/slippagehistory)                                                                                                                                                                                                   |
| Depth near the best price    | [marketLiquidity](https://docs.hydromancer.xyz/readme/rest-api/market-data/marketliquidity), [marketLiquidityHistory](https://docs.hydromancer.xyz/readme/rest-api/market-data/marketliquidityhistory), [currentDepth](https://docs.hydromancer.xyz/readme/rest-api/market-data/currentdepth) |
| Size limits                  | [maxMarketOrderNtls](https://docs.hydromancer.xyz/readme/rest-api/market-data/maxmarketorderntls), [activeAssetData](https://docs.hydromancer.xyz/readme/rest-api/user-position-data/activeassetdata)                                                                                         |
| Your fills and order updates | [userFills](https://docs.hydromancer.xyz/readme/websocket/user-data/userfills), [userOrderUpdates](https://docs.hydromancer.xyz/readme/websocket/user-data/userorderupdates)                                                                                                                  |
| One order, or recent orders  | [orderStatusByOid](https://docs.hydromancer.xyz/readme/rest-api/historical-data/orderstatusbyoid), [historicalOrders](https://docs.hydromancer.xyz/readme/rest-api/historical-data/historicalorders), [openOrders](https://docs.hydromancer.xyz/readme/rest-api/user-order-data/openorders)   |
| TWAP orders                  | [allTwapStatusUpdates](https://docs.hydromancer.xyz/readme/websocket/twap-data/alltwapstatusupdates), [perpTwapSnapshots](https://docs.hydromancer.xyz/readme/rest-api/market-data/perptwapsnapshot)                                                                                          |

**Prices and order books**

Subscribe to [bbo](https://docs.hydromancer.xyz/readme/websocket/orderbook-streaming/bbo) for the best bid and offer of a coin. Each message has the bid and the ask with price, size and number of orders.

[l2Book](https://docs.hydromancer.xyz/readme/websocket/orderbook-streaming/l2book) streams the book by price level. To keep a full book locally with less traffic, load [l2BookDiffSnapshot](https://docs.hydromancer.xyz/readme/rest-api/market-data/l2bookdiffsnapshot) once and apply [l2BookDiff](https://docs.hydromancer.xyz/readme/websocket/orderbook-streaming/l2bookdiff) updates:

* Each diff carries a per-coin `seq` and `prev_seq`. If `prev_seq` does not match the last `seq` you applied, you missed an update: reload the snapshot.
* The stream also sends a `resync` message when you need to reload.
* The snapshot is MessagePack, not JSON.

On these three streams, naming your coins is the standard form. Leaving the coin out to receive every market is an add-on.

[perpPrices](https://docs.hydromancer.xyz/readme/websocket/prices-data-and-asset-context/perpprices) streams the oracle and mark price of up to 200 perps, one message per block. [meta](https://docs.hydromancer.xyz/readme/rest-api/metadata/meta) gives each market's `szDecimals` and `maxLeverage` for rounding sizes and checking leverage, and [exchangeStatus](https://docs.hydromancer.xyz/readme/rest-api/metadata/exchangestatus) the current exchange status.

**Sizing an order**

[slippageHistory](https://docs.hydromancer.xyz/readme/rest-api/market-data/slippagehistory) returns the estimated slippage of buying or selling a given dollar amount. Pass the `coin`, the `amount` in USD and a `startTime`. Each row has `halfSpreadBps`, `buySlippageBps` and `sellSlippageBps`.

Slippage is in basis points, and it is `null` when the book cannot fill the amount.

* [marketLiquidity](https://docs.hydromancer.xyz/readme/rest-api/market-data/marketliquidity) - depth within 2, 5, 10 and 25 basis points of mid, in units of the asset, with up to 24 hours of history
* [marketLiquidityHistory](https://docs.hydromancer.xyz/readme/rest-api/market-data/marketliquidityhistory) - the same depth as hourly averages, further back
* [currentDepth](https://docs.hydromancer.xyz/readme/rest-api/market-data/currentdepth) (add-on) - depth in USD at 10 to 500 basis points, for every market in one response
* [maxMarketOrderNtls](https://docs.hydromancer.xyz/readme/rest-api/market-data/maxmarketorderntls) - the largest market order allowed, as `[leverage, notional]` pairs
* [activeAssetData](https://docs.hydromancer.xyz/readme/rest-api/user-position-data/activeassetdata) - for one user and coin: current `leverage`, and `maxTradeSzs` and `availableToTrade` as `[long, short]`

**Tracking orders and fills**

The [userFills](https://docs.hydromancer.xyz/readme/websocket/user-data/userfills) and [userOrderUpdates](https://docs.hydromancer.xyz/readme/websocket/user-data/userorderupdates) streams deliver your bot's fills and order status changes as they happen. An order update has the order and its `status`, such as `open`, `filled` or `canceled`.

Look up one order with [orderStatusByOid](https://docs.hydromancer.xyz/readme/rest-api/historical-data/orderstatusbyoid), by order ID or client order ID. It covers about 90 days for filled and triggered orders, and the last 3 days for other statuses. [historicalOrders](https://docs.hydromancer.xyz/readme/rest-api/historical-data/historicalorders) returns the latest 2,000 order updates, and [openOrders](https://docs.hydromancer.xyz/readme/rest-api/user-order-data/openorders) what is still resting.

[userRateLimit](https://docs.hydromancer.xyz/readme/rest-api/roles-and-agents/userratelimit) shows the address's Hyperliquid request allowance: `nRequestsUsed` against `nRequestsCap`.

The server sends a ping every 60 seconds. Reply with a pong to keep the connection open.

**Following TWAP orders**

[allTwapStatusUpdates](https://docs.hydromancer.xyz/readme/websocket/twap-data/alltwapstatusupdates) streams every TWAP status change on Hyperliquid, with the user, coin, side, size, duration in minutes and executed size. [userTwapStatusUpdates](https://docs.hydromancer.xyz/readme/websocket/user-data/usertwapstatusupdates) does the same for chosen users. [perpTwapSnapshots](https://docs.hydromancer.xyz/readme/rest-api/market-data/perptwapsnapshot) and [spotTwapSnapshots](https://docs.hydromancer.xyz/readme/rest-api/market-data/spottwapsnapshot) return the TWAP orders active right now.

For one user, [userTwapSummaries](https://docs.hydromancer.xyz/readme/rest-api/historical-data/usertwapsummaries) summarizes each TWAP (average price, size, fee, number of slices), [userTwapSliceFills](https://docs.hydromancer.xyz/readme/rest-api/historical-data/usertwapslicefills) lists its fills, and [userTwapStatusesByTime](https://docs.hydromancer.xyz/readme/rest-api/historical-data/usertwapstatusesbytime) pages through its status history.

**Putting it together**

1. At startup, load `meta` and check `exchangeStatus`.
2. Subscribe to `bbo` or `l2BookDiff` for your markets, and to `userFills` and `userOrderUpdates` for your address.
3. Before each order, check `slippageHistory` or `marketLiquidity` for the size you plan to send.
4. Send the order through Hyperliquid, then confirm it from the streams or `orderStatusByOid`.
