Before you start
The examples use the variables from the programmatic quickstart and a long position of 2ESZ6, the December 2026 E-mini S&P 500 contract, on a Demo in
production. A Demo prices futures only from your own futures login, as Place your first
order explains, so in the sandbox you run the same
calls on a HYPERLIQUID:BTC position, sized in bitcoin, as Test it end to end
does.
The TypeScript SDK isn’t published, so call the routes over HTTP, as the curl
examples do. The TypeScript examples show the same calls through the SDK’s client, with
provider and account set.
On a Demo, a futures close fills only while the CME session is open. Outside it, a flatten still
cancels the working orders, but the position stays and the call answers
contract_not_trading.1
Take some off
Close part of a position trades The response names the close order with
qty
against the position at market and cancels nothing, so the bracket keeps protecting what’s
left.providerOrderId, filledQty and avgFillPrice, and
requestedQty repeats the quantity you asked to close. The position is now 1 contract. A close is
an order, so it carries a clientOrderId like any other, and a retry with the same id can’t close
twice.qty can’t be more than the position. A close that’s too big is refused rather than cut down
to fit, because the position may have changed since you read it, and “close 3” must never
quietly become “close everything”. To close it all, flatten.On an account that holds each entry as its own ticket, send the ticket’s positionId to close
that ticket alone. Add positionRevision, the revision you read the ticket at, and the close is
refused if the ticket has changed since.2
Get flat on one instrument
Flatten a position cancels the instrument’s
working orders first, then closes the position at market. That order matters: a resting order
left behind could open the position again after the close.The response is
{ "ok": true }. Flattening a flat instrument changes nothing, so it’s safe
to repeat, and it’s the right last step of every test run.3
Get flat on everything
Flatten the account cancels every working order
and closes every position on the one account you name. It never touches your other accounts.The response is
{ "ok": true }.4
Turn the position around
Reverse a position cancels the instrument’s
working orders, then trades twice the position at market, so a long of 2 becomes a short of 2.
It runs on the server as one operation under one For a long of 2, the response says what traded and how many orders it cancelled:The new position has no bracket, because the old one protected the other direction. Put a new
one on it with Protect a position.
clientOrderId. Doing it yourself as a cancel
followed by an order could stop half way and leave the trader flat, or turned around without
protection.Which one to use
- Scaling out of a winner: close part. The bracket keeps protecting the rest.
- Done with the trade: flatten the instrument. It clears the resting orders first, so nothing puts the trader back in.
- Done for the day, or something is wrong: flatten the account.
- On the wrong side of the market: reverse. Doing it by hand takes three calls, and each is a chance to stop half way.
When a close is refused
Neither flatten checks the risk lock, because getting flat is what a lock asks for.
Test it end to end
On your Demo in the sandbox, withHYPERLIQUID:BTC sized in bitcoin:
- Buy 0.02 with a take profit and a stop loss, then close 0.01. The position is long 0.01.
- Reverse. The position is short 0.01, and
cancelledOrderscounts the bracket’s orders it took down. - Flatten the account, then read the snapshot: no positions and no working orders.
Next steps
Track fills and P&L
Read the fills the close produced and what the trade earned.
Manage working orders
Move or cancel resting orders without closing anything.