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.
maxSlippageBpscaps how far from the best bid or ask the close may fill. A close past it answersfailed_preconditionand closes nothing. Leave it out to close at whatever the book offers.- A retry with the first
clientOrderIdreplays the first close, even once the position is gone, and never closes a remainder. The sameclientOrderIdon a different position, or on an order that was not this close, answersfailed_precondition. - To close whatever remains, send ClosePosition again without a
clientOrderIdor with a new one. - A position that is already closed, or that the account does not hold, answers
failed_precondition, kindPOSITION_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
- 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. - Call CloseAllPositions with the same
accountId. Only success counts: onfailed_preconditionrepeat from step 1, and onunavailableor a timeout retry this step with backoff. - Call GetAccountSnapshot. The account is flat only when step 2 answered success and the snapshot carries a
balancewithpositionsempty. Ifpositionsis not empty, repeat from step 1.
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.
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, kindORDER_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.

