Partially Close Positions
API URL: POST https://api.topstepx.com/api/Position/partialCloseContract
API Reference: /api/Position/partialCloseContract
Description
Partially close a position.
Parameters
| Name | Type | Description | Required | Nullable |
|---|---|---|---|---|
| accountId | integer | The account ID. | Required | false |
| contractId | string | The contract ID. | Required | false |
| size | integer | The size to close. | Required | false |
Error Codes
| errorCode | Name | Meaning |
|---|---|---|
| 0 | Success | The closing order was executed. |
| 1 | AccountNotFound | The account does not exist or is not owned by the caller. |
| 2 | PositionNotFound | No open position exists for the account and contract. |
| 3 | ContractNotFound | The contract ID is not recognized. |
| 4 | ContractNotActive | The contract is not the currently active contract. |
| 5 | InvalidCloseSize | The size is zero, negative, or larger than the open position. |
| 6 | OrderRejected | The closing market order was rejected. See below. |
| 7 | OrderPending | The closing order is still being processed. |
| 8 | UnknownError | An unexpected error occurred. |
| 9 | AccountRejected | The account is not allowed to close positions (live accounts). |
errorCode 6 (OrderRejected)
This code is returned for more than one reason:
- Trading is not allowed for the symbol. The market is closed, halted, or otherwise not tradable at the moment of the request. The closing order is recorded as rejected for being outside trading hours.
- No current price is available for the contract. The engine could not resolve a price to execute the close. The closing order is recorded as rejected with an invalid price.
In both cases errorMessage is null, so the response alone does not indicate which cause applied. Retry once the market is open and quoting.
Example Usage
Example Request
- cURL Request
curl -X 'POST' \
'https://api.topstepx.com/api/Position/partialCloseContract' \
-H 'accept: text/plain' \
-H 'Content-Type: application/json' \
-d '{
"accountId": 536,
"contractId": "CON.F.US.GMET.J25",
"size": 1
}'
Example Response
- Success
- Error
{
"success": true,
"errorCode": 0,
"errorMessage": null
}
{
"success": false,
"errorCode": 6,
"errorMessage": null
}