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

# Closing and cancelling

> ClosePosition, CloseAllPositions, CancelOrder and CancelAllOrders - flatten a position or clear an account's orders, on their own rate-limit budget.

All four need a **write key** and draw on the **exit** [rate limit](https://hypernova.xyz/docs/api/conventions.md#rate-limits), a budget of their own that placements cannot use up.

## ClosePosition

`ClosePosition` · Body: `{ "accountId": "…", "positionId": "…", "clientOrderId": "…", "maxSlippageBps": "50" }`, with the last two optional

Closes one position at market.
It takes its symbol and side from the position and closes the size the position holds when the close executes, so it needs no quantity and a fill landing in between can neither leave it closing a stale size nor turn the position around.

| Field | Type | Description |
| - | - | - |
| `orderId` | string | The close's order id; your `clientOrderId` if you sent one. |
| `status` | OrderStatus | Such as `ORDER_STATUS_FILLED`. |
| `closedQuantity` *optional* | decimal | The size the booked fills closed, less than the position when the visible book could not take it all. Left out when nothing filled, and on a retry while the first fill is still being booked. |

* `maxSlippageBps` caps how far from the best bid or ask the close may fill. A close past it answers `failed_precondition` and closes nothing. Leave it out to close at whatever the book offers.
* A retry with the first `clientOrderId` replays the first close, even once the position is gone, and never closes a remainder. The same `clientOrderId` on a different position, or on an order that was not this close, answers `failed_precondition`.
* To close whatever remains, send ClosePosition again without a `clientOrderId` or with a new one.
* A position that is already closed, or that the account does not hold, answers `failed_precondition`, kind `POSITION_ALREADY_CLOSED`. That is how a close that lost a race to a take-profit, a stop-loss or another close answers.
* A leg of a pair trade answers `failed_precondition`: close the pair in the app, or the account with CloseAllPositions.

## CloseAllPositions

`CloseAllPositions` · Body: `{ "accountId": "…" }` · Response: `{}`

Closes every open position at market, each by its own market order at the size it holds when that order executes; the orders appear in ListOrderHistory, and a pair trade is closed as a pair.

* It is never slippage-capped, so a flatten never refuses on price. There is no symbol or side filter.
* Every call is a new flatten, so a retry closes whatever is still open, and an account with nothing open answers success.
* A flatten that could not close everything, because a market had no usable book or too little depth, closes all it can and answers `failed_precondition`. Call it again, then check GetAccountSnapshot.
* It does not cancel open orders, and a resting order that opens a position can fill after it. To take an account flat, follow the recipe below.

## Flattening an account in an emergency

1. Call CancelAllOrders twice with only `accountId`. The second call catches orders that became open during the first, but one can still become open during the second.
2. Call CloseAllPositions with the same `accountId`. Only success counts: on `failed_precondition` repeat from step 1, and on `unavailable` or a timeout retry this step with backoff.
3. Call GetAccountSnapshot. The account is flat only when step 2 answered success and the snapshot carries a `balance` with `positions` empty. If `positions` is not empty, repeat from step 1.

A snapshot with no `balance` proves nothing: when our live copy of the account cannot be read, GetAccountSnapshot can answer `positions: []` while a position is still open, and CloseAllPositions answers `unavailable`. Read it again until it carries a `balance`.

Every step is safe to repeat.

> [!WARNING]
>
> The recipe cannot keep the account flat on its own. Pair trades and TWAP orders are placed only from the app, ListOpenOrders does not show their legs or scheduled slices, and no API call cancels them, so a pair's leg or a TWAP's next slice can open a position afterwards. Cancel their parents in the app.

## CancelOrder

`CancelOrder` · Body: `{ "accountId": "…", "orderId": "…" }` · Response: `{}`

* Safe to retry: an order that is already cancelled, or was rejected, also answers 200 `{}`.
* An order that has filled answers `failed_precondition`, kind `ORDER_ALREADY_FILLED`: a lost race, not a bad request.
* A TWAP slice still scheduled for a later release, or one leg of a pair trade, answers `failed_precondition`; those are cancelled through their parent, and a released slice is an ordinary open order.
* An order that does not belong to the account answers `not_found`.

## CancelAllOrders

`CancelAllOrders` · Body: `{ "accountId": "…", "symbol": "BTC", "side": "ORDER_SIDE_SELL" }`, with `symbol` and `side` optional · Response: `{ "cancelledOrderIds": ["…"] }`

Cancels every open order matching the filter, with no limit on how many, so it reaches orders past the newest 1,000 that ListOpenOrders returns.

* An order that becomes open while the call runs, such as a take-profit or stop-loss created by a fill at the same moment, is not cancelled, so in an emergency call it twice.
* A cancelled order cannot fill afterwards. A second call cancels whatever is still open, and an account with nothing open answers an empty list.
* Pair-trade legs stay open, and so do TWAP slices still scheduled for a later release; both are cancelled through their parent in the app.


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