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

# Trading API

> Place, amend and cancel orders, close positions and add take-profit/stop-loss on the exchange accounts you connected to TakeProfit, and follow every order, fill and balance live.

The Trading API trades on the exchange accounts you connected to TakeProfit, from your own code. Place, amend and cancel orders, close positions and protect them with take-profit/stop-loss over REST, and follow every order, fill, position and balance over WebSocket.

It trades through the same connection as the platform's order form, so you don't share your exchange API keys with your script: the script signs its requests with a TakeProfit key of its own.

> [card] [Create a trading key](https://takeprofit.com/docs/platform-api/trading/api-keys.md)
>
> A key with the trading scope and a private key that signs every request.

> [card] [Quickstart](https://takeprofit.com/docs/platform-api/trading/quickstart.md)
>
> List your accounts, read an instrument's rules, place an order and watch it fill.

Base URL: `https://public-api.charting.com/api/v1/trading`, for REST and WebSocket alike.

## Endpoints

| Endpoint | Does |
| - | - |
| `GET /account/list` | Lists your connected exchange accounts with their `accountId` and connectors. |
| `GET /connector/capabilities` | Tells what a connector supports: order types, time in force, trailing stops, take-profit/stop-loss. |
| `POST /order/create` | Places a market or limit order, optionally conditional or trailing, with an optional take-profit/stop-loss. |
| `POST /order/amend` | Changes a working order's price, quantity, trigger or take-profit/stop-loss. |
| `POST /order/cancel` | Cancels one order. |
| `POST /order/cancel-all` | Cancels orders in bulk, by instrument, market, side, kind or label. |
| `POST /position/close` | Closes one position at market. |
| `POST /position/close-all` | Closes positions in bulk, by instrument, market, side or profit and loss. |
| `POST /position/add-tpsl` | Adds a take-profit/stop-loss to an open position. |
| `/events/stream` over WebSocket | Your account's orders, fills, positions and balances, live. See [Events stream](https://takeprofit.com/docs/platform-api/trading/events-stream.md). |
| `/order/data/stream` over WebSocket | An instrument's price and quantity steps, your account settings and live buying power. See [Order data stream](https://takeprofit.com/docs/platform-api/trading/order-data-stream.md). |

Each REST endpoint has its own page in the sidebar with every field.

## How a request is put together

* **The account is its `accountId`**, and the connector is its `connectorCode`, such as `binance_spot` or `binance_usdm`. Both come from `GET /account/list`. Every trading request names the connector, even when the account has only one.
* **The instrument is `{"exchange": ..., "symbol": ...}`**, the same pair the market data [Securities endpoints](https://takeprofit.com/docs/platform-api/rest/overview) return, for example `CXBNCE` and `BTC/USDT`.
* **An order has exactly one of `market` and `limit`**, and at most one of `stop` (conditional) and `trailingStop`. `takeProfit` and `stopLoss` attach to it.
* **Prices and quantities are decimal strings**, such as `"0.001"`, never JSON numbers. Check them against the [order data stream](https://takeprofit.com/docs/platform-api/trading/order-data-stream.md) first: the exchange rejects a price off the price step or a quantity below the minimum.
* **`label` tags the orders of one script.** A bulk cancel narrowed to a label leaves your manual orders and other scripts' orders alone, and the events stream can be filtered to it too.
* **Unknown fields are rejected** with `400`, so a typo fails loudly.

## Requests are asynchronous

A `202 Accepted` means TakeProfit checked the request and took it for processing. It doesn't mean the order reached the exchange, was placed or was filled: the exchange can still reject it, for example for insufficient balance.

Follow the outcome on the [events stream](https://takeprofit.com/docs/platform-api/trading/events-stream.md). `order`, `execution` and `position` frames show what happened, and `commandError` and `notification` frames say why a request failed.

## Retrying safely

| Request | Safe to retry? |
| - | - |
| Place an order | Yes, with the same `Idempotency-Key` header and body. A repeat within 24 hours returns the first order's `orderId` instead of placing a second one. |
| Cancel orders, close positions | Yes. There is nothing left to cancel or close the second time. |
| Amend an order | No. A retry after a timeout can apply the change twice. Check the order on the events stream first. |
| Add take-profit/stop-loss to a position | No. The exchange doesn't deduplicate it, so a retry can add a second pair. |

`502`, `503` and `504` are temporary: retry with exponential backoff, and wait at least `Retry-After` when the response has it.

## Connectors

Each connected account trades through one or more connectors, one per market: Binance, for example, has `binance_spot` and `binance_usdm`. Connectors differ in what they accept, and a field a connector doesn't support is rejected. Read `GET /connector/capabilities` before building requests for a new connector.

Take-profit/stop-loss is the field that differs most:

* **Bybit:** attached to the order. On derivatives, once the order fills, it becomes a separate take-profit/stop-loss order on the position.
* **Binance spot:** the order and its take-profit/stop-loss form one order list. It needs a `limit` order, and cancelling any part cancels the whole list.
* **Binance USD-M:** not accepted on the order. Place the order, then protect the position with `POST /position/add-tpsl`.
* **Exness:** price levels on the order and its position, not separate orders.

## Cashback

Orders placed through the API trade on your connected account, so they earn [cashback](https://takeprofit.com/docs/guide/rewards-tools/rebates.md) the same way as orders from the platform. `userRebateRate` in `GET /account/list` and in the order data snapshot is your cashback rate, and each `execution` frame on the events stream carries that fill's cashback in `userRebateUsdt`.

## Credits and limits

The Trading API draws on the same [credits](https://takeprofit.com/docs/platform-api/limits.md) as the rest of the Platform API, shared by all keys of your account:

* **Every REST request costs 1,000 credits** from the REST quota of 100,000 per 60 seconds. Only a successful request is billed.
* **Every stream costs 25 credits** per 300-second window from the streaming quota, charged when it opens and again in every window it stays open.
* **At most 4 REST requests in flight** per account. A fifth is rejected with `429`.
* **At most 5 trading streams open** per account. A sixth handshake is rejected with `429`.

## Errors

Every error has the same shape:

```json theme={null}
{ "error": { "code": "invalid_argument", "message": "field \"price\" is required for a limit order" } }
```

| Status | Code | Means |
| - | - | - |
| `400` | `invalid_argument` | A field is missing, unknown or out of range. |
| `401` | `unauthenticated` | A signature header is missing or wrong, the timestamp is outside the window, or the key is unknown, revoked or expired. See [Signing requests](https://takeprofit.com/docs/platform-api/trading/api-keys.md#sign-every-request). |
| `403` | `permission_denied` | The key doesn't have the trading scope. |
| `404` | `not_found` | The account, connector or instrument isn't available to this key. |
| `429` | `rate_limited` | Out of credits, or too many requests in flight. Wait `Retry-After` seconds. |
| `502`, `503`, `504` | `bad_gateway`, `unavailable`, `deadline_exceeded` | Temporary. Retry with backoff. |


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