# Batch orders (Terminal routes and batch orders)

> Tickwire API reference, generated from the OpenAPI document the services export. Base URL: https://api.tickwire.io. Replace YOUR_API_KEY and YOUR_ACCOUNT_ID with your own values; use a sandbox key (twsk_…) to experiment.

Source: https://tickwire.io/docs/reference/batch-orders/

### POST /v1/orders

**Batch orders**

Auth: `X-API-Key`

Send one order to several accounts at once (at most 20 legs).

Every leg inherits `defaults` and may override any of it, including with a
falsy value (`slippage: 0` means zero). Fields are snake_case: `symbol`,
`operation` (buy, sell, buyLimit, sellLimit, buyStop, sellStop,
buyStopLimit, sellStopLimit), `volume`, `price`, `slippage`,
`stop_loss`, `take_profit`, `stop_limit_price`, `expiration`,
`comment`, `expert_id` and, on the leg, `account_id`. `results`
mirrors `orders` one for one.

Leg `status`: `filled` (the broker accepted), `rejected` (the broker
refused; `retcode` says why), `invalid` (the terminal refused the
request), `unavailable` (terminal up but not trading), `unreachable` (no
terminal), `timeout` (**unknown**: it may have executed, including when the
broker's reply was lost; reconcile or replay with the same
`Idempotency-Key`, never blind-retry).

Validation is all-or-nothing, execution is not. Errors: 400
`invalid_batch` / `unknown_account` / `unreachable_account` (with
`require_reachable`), 401, 403 (read-only key), 409
`idempotency_in_flight`, 422 `idempotency_key_reused`, 429
`rate_limited` (60 batches a minute per organisation, with
`Retry-After`), 503 `idempotency_unavailable`. `Idempotency-Key` (at
most 128 characters) replays the stored response for 15 minutes with
`idempotent_replay: true`; each leg also carries its own derived key, so a
retry never double-fills a leg that already executed. Every leg is in its
account's audit trail.

Responses:

- `200` Successful Response
- `422` Validation Error (`HTTPValidationError`)

Common errors:

- `401` [`unauthorized`](https://tickwire.io/docs/errors/unauthorized/)
- `429` [`rate_limited`](https://tickwire.io/docs/errors/rate_limited/)
- `400, 422` [`validation_error`](https://tickwire.io/docs/errors/validation_error/)

curl:

```bash
curl -sS -X POST 'https://api.tickwire.io/v1/orders' \
  -H 'X-API-Key: YOUR_API_KEY'
```

Python:

```python
from tickwire import Tickwire

with Tickwire("https://api.tickwire.io", "YOUR_API_KEY") as tw:
    result = tw.request(
        "POST",
        "/v1/orders",
        idempotency_key="my-request-1",
        reason="why you are doing this",
    )
    print(result)
```

TypeScript:

```typescript
import { TickwireClient } from "@tickwire/sdk";

const tw = new TickwireClient({ baseUrl: "https://api.tickwire.io", token: "YOUR_API_KEY" });
const result = await tw.request("POST", "/v1/orders", {
  idempotencyKey: "my-request-1",
  reason: "why you are doing this",
});
console.log(result);
tw.close();
```

### POST /v1/orders/close

**Batch close**

Auth: `X-API-Key`

Close by selector (or explicit tickets) across several accounts at once
(at most 20 accounts, 20 orders each; more come back `skipped`).

Each account entry is a selector (`symbol` required, `*` for all, typed
literally; `symbol_match` `exact` or `base` to accept broker suffixes
like `.sd` / `m`; `side` long, short or any; `kind` position
(default), pending or any; `magic`) or an explicit `tickets` list, never
both. `volume` closes part of each matched position. Accounts run
concurrently; the tickets of one account run in sequence.

Account `status`: closed, partial, failed, nothing_matched (not an error),
unavailable / unreachable / invalid (the lookup failed, so nothing is
known), timeout (orders may have closed). Ticket `status`: closed,
rejected, invalid, unavailable, unreachable, timeout (may have closed),
skipped (never sent). A ticket that was already gone counts as `closed`
with a message saying so. Errors and `Idempotency-Key` work as on
`POST /v1/orders`.

Responses:

- `200` Successful Response
- `422` Validation Error (`HTTPValidationError`)

Common errors:

- `401` [`unauthorized`](https://tickwire.io/docs/errors/unauthorized/)
- `429` [`rate_limited`](https://tickwire.io/docs/errors/rate_limited/)
- `400, 422` [`validation_error`](https://tickwire.io/docs/errors/validation_error/)

curl:

```bash
curl -sS -X POST 'https://api.tickwire.io/v1/orders/close' \
  -H 'X-API-Key: YOUR_API_KEY'
```

Python:

```python
from tickwire import Tickwire

with Tickwire("https://api.tickwire.io", "YOUR_API_KEY") as tw:
    result = tw.request(
        "POST",
        "/v1/orders/close",
        idempotency_key="my-request-1",
        reason="why you are doing this",
    )
    print(result)
```

TypeScript:

```typescript
import { TickwireClient } from "@tickwire/sdk";

const tw = new TickwireClient({ baseUrl: "https://api.tickwire.io", token: "YOUR_API_KEY" });
const result = await tw.request("POST", "/v1/orders/close", {
  idempotencyKey: "my-request-1",
  reason: "why you are doing this",
});
console.log(result);
tw.close();
```
