> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lean.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Limit chases

> Rest a limit order near the best price and let Lean move it as the book moves.

A limit chase rests a post only limit order at a set position relative to the best price on its own
side of the book, and moves it whenever that price moves, until the order fills, you cancel it, or it
expires. A buy follows the best bid and a sell follows the best ask. Lean moves the order for you,
so your program does not need to stay connected.

Limit chases run on Kalshi, Polymarket, Polymarket US and Hyperliquid. One naming `lighter` is
refused with `422` and reason `NO_EXECUTABLE_VENUE`. This example uses a test key, so nothing real
moves:

```bash theme={"dark"}
curl -sS -X POST "$LEAN_API_URL/v1/limit-chases" \
  -H "Authorization: Bearer $LEAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "market_id": "'"$MARKET_ID"'",
    "venue": "polymarket",
    "action": "buy",
    "quantity": "25",
    "offset_ticks": 1,
    "price_limit": "0.45"
  }'
```

| Field | Meaning |
| - | - |
| `market_id` | A `mkt_…` ID from `GET /v1/markets`. |
| `venue` | `kalshi`, `polymarket`, `polymarket_us` or `hyperliquid`: the venue the order rests on. |
| `action` | `buy` or `sell`. |
| `quantity` | Shares to buy or sell, as a decimal string. |
| `offset_ticks` | Optional. How many price ticks behind the best price the order rests, from `0` to `10`. With no offset, the order rests at the best price. |
| `offset_price` | Optional. The same distance in dollars, from `0` to `0.1`. |
| `offset_shares` | Optional. The most shares the order lets rest ahead of it in the queue, in steps of `0.01` up to `10000000`. The order rests one tick inside the first price level that would put more shares ahead of it. |
| `offset_beat_best` | Optional. Only `true` is accepted: send it to rest one tick better than the best price, first in the queue. It stops one tick short of the other side, so it never trades at once, and when the spread is a single tick it joins the best price instead. |
| `price_limit` | Optional. The worst price the order rests at, in dollars: a buy never rests above it and a sell never below it. It must fit the market's price ticks. Leave it out to follow the book wherever it goes. |
| `expires_at` | Optional. When the limit chase lapses if it has not filled, as an ISO 8601 time in UTC ending in `Z`, in the future and at most 30 days ahead. On Polymarket, and on any venue with a test key, it must be at least 2 minutes ahead. Leave it out to keep it until you cancel it. |

Give at most one of `offset_ticks`, `offset_price`, `offset_shares` and `offset_beat_best`. The
limit chase reads back the one it uses and `null` for the others, with `offset_beat_best` `false`
unless it beats the best price.

A limit chase made in the Lean app can instead rest as far back as it can within a set distance of
the midpoint. It reads back that distance in dollars as `offset_midpoint`, which is `null` for
every other limit chase. A request cannot set `offset_midpoint`, but an edit can keep it by leaving
the offset out or replace it with one of the offsets above. On Polymarket the midpoint is the one
Polymarket uses for liquidity rewards, which leaves out small orders at the top of the book.

The key needs the `trade` scope. The answer is `200` with the limit chase, which reads `active`
while it follows the book. `current_price` is the price the order rests at now, and is `null` while
the chase waits for a book to follow.

## How the order moves

Lean places the order where its offset puts it relative to the best price on its own side: behind
it by `offset_ticks` or `offset_price`, inside the queue depth `offset_shares` allows, or one tick
better with `offset_beat_best`. On Polymarket and Hyperliquid a buy rests higher when needed to stay
worth at least 1 USD. It never rests at a price that would trade at once against the other side of
the book, and never beyond `price_limit`. When its target price moves by a price tick or more, Lean
moves the order there, waiting 2 seconds after the last move unless the order would otherwise trade
at once or sit beyond `price_limit`. With `offset_shares`, it moves by the queue ahead of it rather
than by each price change. On Kalshi the order is changed in place; on other venues Lean cancels it
and places it again. Each fill adds to `filled_quantity` and
`avg_fill_price`, and the rest keeps following the book.

| `status` | Final | Meaning |
| - | - | - |
| `active` | No | Resting on the book and following the best price. |
| `filled` | Yes | Every share bought or sold. |
| `canceled` | Yes | Canceled by you, or the market closed or resolved first. Shares already bought or sold stay with the account. |
| `expired` | Yes | `expires_at` passed before the order filled. |
| `failed` | Yes | The venue refused the order 5 times in a row with nothing filled in between, Lean could not place it for about 10 minutes, or the order is too small to place at any price within `price_limit`. If part had filled, that last case ends `filled`. |

## Limits on what can be traded

* A sell takes shares the account holds, and shares already offered by a resting sell order or
  covered by a stop loss count as taken. A sell above what is left is refused with
  `400 VALIDATION_ERROR`.
* A test key's buy needs enough simulated cash on the venue for `quantity` shares at
  `price_limit`, or at \$1 a share without one, and is refused with `400 VALIDATION_ERROR`
  otherwise. A live key's buy is not checked against the account's cash when you create it, so
  check `GET /v1/balances` first: the venue refuses an order the account can't pay for.
* An order must be at least the market's minimum number of shares. On Hyperliquid it must also fit
  the venue's size step. On Polymarket and Hyperliquid, a buy with a `price_limit` must be worth at
  least 1 USD at that price.
* The market must be open and must trade on the venue you name. A market that cannot be traded on
  that venue is refused with `422 VALIDATION_ERROR` and `details.reason: "NO_EXECUTABLE_VENUE"`,
  and a venue the trading account does not allow with `details.reason: "VENUE_NOT_ALLOWED"`.
* A limit chase counts against the key's limits as an order of the most it can trade for, from when
  it is created: a buy as `quantity` shares at `price_limit`, or at the top price of \$1 a share
  without one, and a sell at that same top price. A buy above the key's per order limit is refused
  with `details.reason: "ORDER_NOTIONAL_LIMIT"`, and one past the key's or account's daily limit
  with `details.reason: "DAILY_NOTIONAL_LIMIT"`. A sell counts toward the day too, but only takes
  risk off, so neither limit refuses it. A test key's limit chases count against its own limits.
* With a live key, a limit chase is refused with `403 FORBIDDEN` when the account can't trade on
  the venue: for example, on Kalshi and Polymarket US no API key is connected in the Lean app, or on
  Polymarket the wallet isn't set up or its approvals need refreshing in the Lean app.
* Creating a limit chase is refused with `503 FEATURE_DISABLED` while trading is paused, or, for a
  live key, while Lean has paused limit chases on the venue. See [Pause trading](https://docs.lean.xyz/orders.md#pause-trading).

## Track and cancel

`GET /v1/limit-chases` lists the limit chases of the key's trading account, newest first: every one
still active, and those that ended in the last hour. A live key also sees the limit chases made in
the Lean app on that account. Kalshi and Polymarket US limit chases belong to you rather than to a
trading account, so a live key sees all of yours on those venues. `GET /v1/limit-chases/{id}` reads one.

Cancel one with `DELETE /v1/limit-chases/{id}`:

```bash theme={"dark"}
curl -sS -X DELETE "$LEAN_API_URL/v1/limit-chases/$LIMIT_CHASE_ID" \
  -H "Authorization: Bearer $LEAN_API_KEY"
```

The answer is `200` with the limit chase and `cancel_requested_at` set. A live limit chase keeps its
status for a moment while Lean cancels its resting order, then reads `canceled`. A test limit chase
reads `canceled` at once. Canceling works while trading is paused, and a limit chase that has
already ended is refused with `400 VALIDATION_ERROR`.

Pausing trading does not stop a limit chase that is already on the book. Cancel it if you want it
gone.

## Change a limit chase

Change the size, offset or price limit of a limit chase with `PATCH /v1/limit-chases/{id}`. It
keeps its id, and fields you leave out keep their values:

```bash theme={"dark"}
curl -sS -X PATCH "$LEAN_API_URL/v1/limit-chases/$LIMIT_CHASE_ID" \
  -H "Authorization: Bearer $LEAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "quantity": "40", "offset_beat_best": true }'
```

| Field | Meaning |
| - | - |
| `quantity` | Optional. The new total shares, counting what has already filled. It can't be less than `filled_quantity`. |
| `offset_ticks`, `offset_price`, `offset_shares`, `offset_beat_best` | Optional. One new offset, which replaces the current one. |
| `price_limit` | Optional. The new worst price the order rests at. Send `null` to remove it. |

The new values are checked as they are when you create one. A buy is checked as a whole against
your per order limit, and whatever the edit adds to the most it can trade for, from a larger size
or a higher or removed `price_limit`, counts toward your day. The answer is `200` with the limit chase as
edited. A live one answers with `edit_pending: true` and moves its resting order a moment later.
Read it by id to confirm: once Lean applies the edit, `edit_pending` turns `false`. If Lean refuses
the edit, for example because fills overtook a smaller size, the limit chase keeps its earlier
values and `last_error` says why. A list shows a pending edit the same way, though it can trail an
edit accepted a moment earlier.
On Kalshi, Lean changes the resting order in place rather than canceling and placing it again, so a
smaller size at the same price keeps its place in the queue. Only an `active` limit chase can
change: one that has ended is refused with `400 VALIDATION_ERROR`, and one with a cancel pending with `409 CONFLICT`.
A limit chase that changed while you edited it also answers `409 CONFLICT` on test and live keys
alike, and so does a live one whose earlier edit is still pending; send the edit again.

## Test keys

A test key's limit chase is checked like a live one, against the market and the test account's
simulated cash and shares. It follows the venue's public book and fills in simulation once the best
price on the other side reaches its price. It is brought up to date with the book each time you
list or read it, so its price and fills change between reads. It never reaches the venue, and its
fills show in the test account's positions and balances. See [Test mode and live mode](https://docs.lean.xyz/test-mode.md).

## Fills

A limit chase is not an order you placed, so its fills are listed in `GET /v1/fills` with `order_id`
set to `null`, and `GET /v1/fills?order_id=` never returns them. A live key sees each fill the venue
reports for the chase's resting order. A test key sees one fill per limit chase for what it has
filled so far, at `avg_fill_price`, with `liquidity` `maker` and `filled_at` the time the chase was
created. It is listed once something fills and grows as more fills, keeping the same `id`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.