# Trading (Account REST API (/users/current))

> 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/trading/

### POST /users/current/accounts/{account_id}/calculate-margin

**Calculate Margin**

Auth: `auth-token`

Margin for an order: `{margin}`, or `{}` if not calculable.

| Parameter | In | Type | Required | Description |
|---|---|---|---|---|
| `account_id` | path | `string` | yes |  |

Request body: `object` (required)

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/)
- `404` [`account_not_found`](https://tickwire.io/docs/errors/account_not_found/)
- `503` [`not_connected`](https://tickwire.io/docs/errors/not_connected/)

curl:

```bash
curl -sS -X POST 'https://api.tickwire.io/users/current/accounts/YOUR_ACCOUNT_ID/calculate-margin' \
  -H 'auth-token: YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{"symbol":"EURUSD","type":"ORDER_TYPE_BUY","volume":0.01,"openPrice":1.1}'
```

Python:

```python
from tickwire import Tickwire

with Tickwire("https://api.tickwire.io", "YOUR_API_KEY") as tw:
    result = tw.request(
        "POST",
        "/users/current/accounts/YOUR_ACCOUNT_ID/calculate-margin",
        json={
            "symbol": "EURUSD",
            "type": "ORDER_TYPE_BUY",
            "volume": 0.01,
            "openPrice": 1.1,
        },
        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", "/users/current/accounts/YOUR_ACCOUNT_ID/calculate-margin", {
  body: {"symbol":"EURUSD","type":"ORDER_TYPE_BUY","volume":0.01,"openPrice":1.1},
  idempotencyKey: "my-request-1",
  reason: "why you are doing this",
});
console.log(result);
tw.close();
```

### POST /users/current/accounts/{account_id}/trade

**Trade** · takes `Idempotency-Key`

Auth: `auth-token`

Execute a trade (MT4 and MT5).

Body: a `MetatraderTrade`. `actionType` is one of `ORDER_TYPE_BUY|SELL`,
`ORDER_TYPE_BUY_LIMIT|SELL_LIMIT|BUY_STOP|SELL_STOP`,
`ORDER_TYPE_BUY_STOP_LIMIT|SELL_STOP_LIMIT` (MT5 only), `POSITION_MODIFY`,
`POSITION_PARTIAL`, `POSITION_CLOSE_ID`, `POSITION_CLOSE_BY`,
`POSITIONS_CLOSE_SYMBOL`, `ORDER_MODIFY` or `ORDER_CANCEL`. Relative
SL/TP units, `fillingModes`, `expiration`, `slippage`, `magic`,
`comment` + `clientId` (30 characters together) and `trailingStopLoss`
are supported.

**HTTP 200 even when the broker rejects the trade**:
`{numericCode, stringCode, message, orderId?, positionId?}`. Success codes
are 0, 10008, 10009, 10010 and 10025. Tickwire adds `outcome`
(`applied|no_change|partial|rejected|unknown`), `isEffective`,
`retcode` (the terminal's own code), `replayed` and, for
`POSITIONS_CLOSE_SYMBOL`, a per-position `positions` list.
`outcome=unknown` means the order may have executed: check positions and
orders before retrying. Tickwire never resends new exposure on its own.

Idempotency: the `Idempotency-Key` header, else `clientId`. A replay
returns the stored result with `replayed: true`; the same key with a
different body is 409 `ConflictError`. `X-Actor` and `X-Reason` go
to the audit trail. A malformed request is 400 `ValidationError` with
`details: [{parameter, message}]`; what MT4 can't do (stop-limit, DAY
expiry) answers `numericCode` -4 with `error: not_supported_on_mt4`.

| Parameter | In | Type | Required | Description |
|---|---|---|---|---|
| `account_id` | path | `string` | yes |  |
| `Idempotency-Key` | header | `string \| null` | no |  |
| `X-Actor` | header | `string` | no |  |
| `X-Reason` | header | `string` | no |  |

Request body: `object` (required)

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/)
- `404` [`account_not_found`](https://tickwire.io/docs/errors/account_not_found/)
- `503` [`not_connected`](https://tickwire.io/docs/errors/not_connected/)
- `409` [`idempotency_conflict`](https://tickwire.io/docs/errors/idempotency_conflict/)
- `500, 502` [`backend_error`](https://tickwire.io/docs/errors/backend_error/)

curl:

```bash
curl -sS -X POST 'https://api.tickwire.io/users/current/accounts/YOUR_ACCOUNT_ID/trade' \
  -H 'auth-token: YOUR_API_KEY' \
  -H 'Idempotency-Key: my-request-1' \
  -H 'X-Reason: why you are doing this' \
  -H 'Content-Type: application/json' \
  -d '{"actionType":"ORDER_TYPE_BUY","symbol":"EURUSD","volume":0.01}'
```

Python:

```python
from tickwire import Tickwire

with Tickwire("https://api.tickwire.io", "YOUR_API_KEY") as tw:
    result = tw.request(
        "POST",
        "/users/current/accounts/YOUR_ACCOUNT_ID/trade",
        json={"actionType": "ORDER_TYPE_BUY", "symbol": "EURUSD", "volume": 0.01},
        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", "/users/current/accounts/YOUR_ACCOUNT_ID/trade", {
  body: {"actionType":"ORDER_TYPE_BUY","symbol":"EURUSD","volume":0.01},
  idempotencyKey: "my-request-1",
  reason: "why you are doing this",
});
console.log(result);
tw.close();
```

### POST /users/current/accounts/{account_id}/trade/check

**Trade Check**

Auth: `auth-token`

Dry-run a trade: the same body as `/trade`. Runs every pre-flight
check (and, on MT5, the terminal's own order check) and returns the
resolved request plus margin figures. Nothing is sent.

| Parameter | In | Type | Required | Description |
|---|---|---|---|---|
| `account_id` | path | `string` | yes |  |

Request body: `object` (required)

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/)
- `404` [`account_not_found`](https://tickwire.io/docs/errors/account_not_found/)
- `503` [`not_connected`](https://tickwire.io/docs/errors/not_connected/)
- `500, 502` [`backend_error`](https://tickwire.io/docs/errors/backend_error/)

curl:

```bash
curl -sS -X POST 'https://api.tickwire.io/users/current/accounts/YOUR_ACCOUNT_ID/trade/check' \
  -H 'auth-token: YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{"actionType":"ORDER_TYPE_BUY","symbol":"EURUSD","volume":0.01}'
```

Python:

```python
from tickwire import Tickwire

with Tickwire("https://api.tickwire.io", "YOUR_API_KEY") as tw:
    result = tw.request(
        "POST",
        "/users/current/accounts/YOUR_ACCOUNT_ID/trade/check",
        json={"actionType": "ORDER_TYPE_BUY", "symbol": "EURUSD", "volume": 0.01},
        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", "/users/current/accounts/YOUR_ACCOUNT_ID/trade/check", {
  body: {"actionType":"ORDER_TYPE_BUY","symbol":"EURUSD","volume":0.01},
  idempotencyKey: "my-request-1",
  reason: "why you are doing this",
});
console.log(result);
tw.close();
```

### GET /users/current/accounts/{account_id}/trailing-stop-losses

**List Trailing**

Auth: `auth-token`

Trailing stop rules Tickwire runs for this account (`state`:
`active` | `pending`; `pending` waits for its pending order to fill).

| Parameter | In | Type | Required | Description |
|---|---|---|---|---|
| `account_id` | path | `string` | yes |  |

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/)
- `404` [`account_not_found`](https://tickwire.io/docs/errors/account_not_found/)
- `503` [`not_connected`](https://tickwire.io/docs/errors/not_connected/)

curl:

```bash
curl -sS -X GET 'https://api.tickwire.io/users/current/accounts/YOUR_ACCOUNT_ID/trailing-stop-losses' \
  -H 'auth-token: YOUR_API_KEY'
```

Python:

```python
from tickwire import Tickwire

with Tickwire("https://api.tickwire.io", "YOUR_API_KEY") as tw:
    result = tw.request(
        "GET",
        "/users/current/accounts/YOUR_ACCOUNT_ID/trailing-stop-losses",
    )
    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("GET", "/users/current/accounts/YOUR_ACCOUNT_ID/trailing-stop-losses");
console.log(result);
tw.close();
```

### DELETE /users/current/accounts/{account_id}/trailing-stop-losses/{position_id}

**Delete Trailing** · takes `Idempotency-Key`

Auth: `auth-token`

Stop trailing a position (the current SL stays where it is).

| Parameter | In | Type | Required | Description |
|---|---|---|---|---|
| `account_id` | path | `string` | yes |  |
| `position_id` | path | `string` | yes |  |
| `Idempotency-Key` | header | `string \| null` | no |  |
| `X-Actor` | header | `string` | no |  |
| `X-Reason` | header | `string` | no |  |

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/)
- `404` [`account_not_found`](https://tickwire.io/docs/errors/account_not_found/)
- `503` [`not_connected`](https://tickwire.io/docs/errors/not_connected/)
- `409` [`idempotency_conflict`](https://tickwire.io/docs/errors/idempotency_conflict/)

curl:

```bash
curl -sS -X DELETE 'https://api.tickwire.io/users/current/accounts/YOUR_ACCOUNT_ID/trailing-stop-losses/POSITION_ID' \
  -H 'auth-token: YOUR_API_KEY' \
  -H 'Idempotency-Key: my-request-1' \
  -H 'X-Reason: why you are doing this'
```

Python:

```python
from tickwire import Tickwire

with Tickwire("https://api.tickwire.io", "YOUR_API_KEY") as tw:
    result = tw.request(
        "DELETE",
        "/users/current/accounts/YOUR_ACCOUNT_ID/trailing-stop-losses/POSITION_ID",
        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("DELETE", "/users/current/accounts/YOUR_ACCOUNT_ID/trailing-stop-losses/POSITION_ID", {
  idempotencyKey: "my-request-1",
  reason: "why you are doing this",
});
console.log(result);
tw.close();
```
