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

# Close a position

> Four ways out — partial close, flatten one instrument, flatten the account, reverse — and what each cancels first.

There are four ways out of a position, and they differ in exactly one thing: what they cancel
before they trade. Pick by that, not by convenience.

| Route                                                                       | Cancels first                      | Then                          | Leaves                                           |
| --------------------------------------------------------------------------- | ---------------------------------- | ----------------------------- | ------------------------------------------------ |
| [Close part of a position](/api-reference/trading/close-part-of-a-position) | nothing                            | a market order for `qty`      | the rest of the position and every working order |
| [Flatten a position](/api-reference/trading/flatten-a-position)             | that instrument's working orders   | closes its position at market | every other instrument untouched                 |
| [Flatten the account](/api-reference/trading/flatten-the-account)           | every working order on the account | closes every position         | nothing                                          |
| [Reverse a position](/api-reference/trading/reverse-a-position)             | that position's working orders     | opens the opposite position   | a position the other way round                   |

<Steps>
  <Step title="Take some off">
    Close part of a position places a market order for `qty` and cancels nothing — the resting
    orders stay exactly where they are.

    ```bash theme={null}
    curl -X POST "$TRDRS_API_BASE_URL/api/trading/close" \
      -H "Authorization: Bearer $TRDRS_TENANT_KEY" \
      -H "Content-Type: application/json" \
      -d '{
        "instrument": "ESU6",
        "qty": 1,
        "clientOrderId": "close-esu6-1787582700"
      }'
    ```

    `qty` must not exceed the open position. An over-sized close is rejected with
    `close_exceeds_position` rather than silently clamped, because clamping would turn "close 3"
    into "close everything" on a position that moved between your read and your write. If you meant
    everything, flatten.

    It places an order, so it is claimed like one: `clientOrderId` is the idempotency key and a
    duplicate answers `409`.
  </Step>

  <Step title="Get flat on one instrument">
    Flatten cancels the instrument's working orders, then closes its position at market. The two
    steps run in that order so a resting order can never re-open the position the close just
    flattened.

    ```bash theme={null}
    curl -X POST "$TRDRS_API_BASE_URL/api/trading/flatten" \
      -H "Authorization: Bearer $TRDRS_TENANT_KEY" \
      -H "Content-Type: application/json" \
      -d '{ "instrument": "ESU6" }'
    ```

    This is the guide rail for every demo and the last step of every test run.
  </Step>

  <Step title="Get flat on everything">
    Flatten the account closes every position and cancels every working order on the account.

    ```bash theme={null}
    curl -X POST "$TRDRS_API_BASE_URL/api/trading/flatten-account?broker=rithmic&account=PA-4821-07" \
      -H "Authorization: Bearer $TRDRS_TENANT_KEY"
    ```

    Irreversible — confirm before you call it. Scope it with `broker` and `account`: it targets
    exactly the one resolved account, never every connected account at once. With no connected
    broker it answers `400`.
  </Step>

  <Step title="Turn the position around">
    Reverse cancels the position's working orders, then opens the opposite position. It is one
    operation under one idempotency key, and it runs server-side because a client-side
    cancel-and-place pair could crash in between and leave the trader flat or without exits.

    ```bash theme={null}
    curl -X POST "$TRDRS_API_BASE_URL/api/trading/reverse" \
      -H "Authorization: Bearer $TRDRS_TENANT_KEY" \
      -H "Content-Type: application/json" \
      -d '{
        "instrument": "ESU6",
        "clientOrderId": "reverse-esu6-1787582400"
      }'
    ```

    The reversal is a market order for twice the position quantity. The response reports the new
    `side` and `qty`, the fill, and `cancelledOrders` — how many working orders the sweep took
    down.
  </Step>
</Steps>

## Which one is right

* **Scaling out of a winner?** Close part. It leaves your stop and target resting, so the rest of
  the position stays protected.
* **Done with this trade?** Flatten the instrument. It clears the resting orders first, so nothing
  puts you back in.
* **Done for the day, or something is wrong?** Flatten the account.
* **Wrong side of the market?** Reverse. Doing it as cancel-then-close-then-enter yourself is three
  chances to end up half-done.

If you want to clear the resting orders without touching the position, that is [cancel all working
orders](/api-reference/trading/cancel-all-working-orders), not a flatten.

## What the risk lock does here

A risk-locked account answers `423` to the partial close and to reverse — both place an order that
can re-shape exposure. Neither flatten route lists a `423`, and the reason is in the risk monitor
itself: when a control fires, the monitor flattens the account and locks trading until the period
resets.

## Where to go deeper

* [Manage working orders](/guides/manage-working-orders) — replacing and cancelling by broker order id.
* [Track fills and P\&L](/guides/track-fills-and-pnl) — what the close left behind, in the ledgers.
* [Set risk controls](/guides/manage-risk-controls) — the controls that flatten and lock an account for you.
* [Errors](/partner-platform/overview/errors) — `409`, `423`, and the rest of the refusal shapes.
