# Deal and order history

Source: https://docs.pipsocket.com/api/history

Deals and past orders in a time range.

All calls: `https://api.pipsocket.com`, with `Authorization: Bearer <your workspace key>`.

## List deals

`GET /v1/accounts/{account}/history/deals`

The account's deals in a time range (MT5's `HistorySelect`), read fresh from its terminal.

### Path

- `account` (string, required): The account's id, `acc_` and 26 characters, as the dashboard shows it.

### Query

- `from` (integer, required): The range's start: broker server time, seconds since 1970, included.
- `to` (integer, required): The range's end: broker server time, seconds since 1970, included. Not before `from`.

### Response

A list of:

- `ticket` (integer, MT5 `DEAL_TICKET`): 
- `order` (integer, MT5 `DEAL_ORDER`): The order that made the deal.
- `position` (integer, MT5 `DEAL_POSITION_ID`): The position's id (`identifier`).
- `symbol` (string, MT5 `DEAL_SYMBOL`): 
- `type` (enum, or null): MT5's `DEAL_TYPE`: `buy`, `sell`, or a balance operation. One of: `buy`, `sell`, `balance`, `credit`, `charge`, `correction`, `bonus`, `commission`, `commission_daily`, `commission_monthly`, `commission_agent_daily`, `commission_agent_monthly`, `interest`, `buy_canceled`, `sell_canceled`, `dividend`, `dividend_franked`, `tax`.
- `entry` (enum): MT5's `DEAL_ENTRY`: `in` opens or adds, `out` closes, `inout` reverses, `out_by` closes by an opposite position. One of: `in`, `out`, `inout`, `out_by`.
- `reason` (enum): MT5's `DEAL_REASON`: who or what made the deal (`expert` for the API, `sl`, `tp`, `so` for a stop out). One of: `client`, `mobile`, `web`, `expert`, `sl`, `tp`, `so`, `other`.
- `volume` (number, MT5 `DEAL_VOLUME`): 
- `price` (number, MT5 `DEAL_PRICE`): 
- `profit` (number, MT5 `DEAL_PROFIT`): 
- `commission` (number, MT5 `DEAL_COMMISSION`): 
- `swap` (number, MT5 `DEAL_SWAP`): 
- `fee` (number, MT5 `DEAL_FEE`): 
- `magic` (integer, MT5 `DEAL_MAGIC`): 
- `comment` (string, MT5 `DEAL_COMMENT`): 
- `time_msc` (integer, MT5 `DEAL_TIME_MSC`): Broker server time, milliseconds since 1970.

### Answers

- 200: Every deal in the range.
- 401: - `missing_api_key`: No API key: send it as `Authorization: Bearer <key>`.; `invalid_api_key`: The key is not a workspace's current key (a rolled key stops working within about a minute).
- 404: - `no_such_account`: No such account, or one the key's workspace doesn't have: the two look the same.
- 422: - `invalid_request`: A field, query parameter or header is missing, malformed or not allowed, or the body is not JSON. Nothing was sent.; `range_too_large`: The history range holds more than one answer can carry: ask for a shorter range.
- 503: - `account_not_connected`: The account's terminal is not connected to the engine (stopped, starting, or restarting). Nothing was sent, except by the kill switch: engaging it still engages it, and may already have deleted pending orders.; `bad_command`: The terminal refused the command, for example a negative `expiration`, or a history range MT5 can't select.; `terminal_error`: The terminal did not give the answer asked for.; `engine_busy`: The account's engine host has too many calls in flight (256). Nothing was done; try again.; `engine_unavailable`: The account's engine host is not connected to Pipsocket, or disconnected while the call waited. A trade it already had is still settled: look it up.
- 504: - `terminal_timeout`: The account's terminal did not answer within 5 seconds.; `engine_timeout`: The account's engine did not answer within 30 seconds.

### Example

```bash
curl "https://api.pipsocket.com/v1/accounts/acc_01m6tx3k9f2qw8r7bznd5yvh4c/history/deals?from=1794009600&to=1794095999" \
  -H "Authorization: Bearer $PIPSOCKET_KEY"
```

200 deals:

```json
[
  {
    "comment": "",
    "commission": -0.35,
    "entry": "in",
    "fee": 0,
    "magic": 0,
    "order": 881402311,
    "position": 881402311,
    "price": 1.08412,
    "profit": 0,
    "reason": "expert",
    "swap": 0,
    "symbol": "EURUSD",
    "ticket": 771203455,
    "time_msc": 1794067801211,
    "type": "buy",
    "volume": 0.1
  },
  {
    "comment": "[sl 1.07900]",
    "commission": -0.35,
    "entry": "out",
    "fee": 0,
    "magic": 0,
    "order": 881402390,
    "position": 881402311,
    "price": 1.079,
    "profit": -51.2,
    "reason": "sl",
    "swap": 0,
    "symbol": "EURUSD",
    "ticket": 771203498,
    "time_msc": 1794071102540,
    "type": "sell",
    "volume": 0.1
  }
]
```

## List past orders

`GET /v1/accounts/{account}/history/orders`

The account's orders no longer pending in a time range (MT5's `HistorySelect`), with their final state, read fresh from its terminal.

### Path

- `account` (string, required): The account's id, `acc_` and 26 characters, as the dashboard shows it.

### Query

- `from` (integer, required): The range's start: broker server time, seconds since 1970, included.
- `to` (integer, required): The range's end: broker server time, seconds since 1970, included. Not before `from`.

### Response

A list of:

- `ticket` (integer, MT5 `ORDER_TICKET`): 
- `symbol` (string, MT5 `ORDER_SYMBOL`): 
- `type` (enum, or null, MT5 `ORDER_TYPE`): MT5's `ENUM_ORDER_TYPE`: `buy` and `sell` are market orders (`ORDER_TYPE_BUY`, `ORDER_TYPE_SELL`); `buy_limit`, `sell_limit`, `buy_stop`, `sell_stop`, `buy_stop_limit` and `sell_stop_limit` are pending orders (`ORDER_TYPE_BUY_LIMIT` and so on); `close_by` is `ORDER_TYPE_CLOSE_BY`. One of: `buy`, `sell`, `buy_limit`, `sell_limit`, `buy_stop`, `sell_stop`, `buy_stop_limit`, `sell_stop_limit`, `close_by`.
- `state` (enum, or null): MT5's `ORDER_STATE`. One of: `started`, `placed`, `canceled`, `partial`, `filled`, `rejected`, `expired`, `request_add`, `request_modify`, `request_cancel`.
- `volume_initial` (number, MT5 `ORDER_VOLUME_INITIAL`): 
- `volume_current` (number, MT5 `ORDER_VOLUME_CURRENT`): 
- `price_open` (number, MT5 `ORDER_PRICE_OPEN`): 
- `magic` (integer, MT5 `ORDER_MAGIC`): 
- `comment` (string, MT5 `ORDER_COMMENT`): 
- `position` (integer, MT5 `ORDER_POSITION_ID`): 
- `time_setup_msc` (integer, MT5 `ORDER_TIME_SETUP_MSC`): Broker server time, milliseconds since 1970.
- `time_done_msc` (integer, MT5 `ORDER_TIME_DONE_MSC`): Broker server time, milliseconds since 1970.

### Answers

- 200: Every order in the range.
- 401: - `missing_api_key`: No API key: send it as `Authorization: Bearer <key>`.; `invalid_api_key`: The key is not a workspace's current key (a rolled key stops working within about a minute).
- 404: - `no_such_account`: No such account, or one the key's workspace doesn't have: the two look the same.
- 422: - `invalid_request`: A field, query parameter or header is missing, malformed or not allowed, or the body is not JSON. Nothing was sent.; `range_too_large`: The history range holds more than one answer can carry: ask for a shorter range.
- 503: - `account_not_connected`: The account's terminal is not connected to the engine (stopped, starting, or restarting). Nothing was sent, except by the kill switch: engaging it still engages it, and may already have deleted pending orders.; `bad_command`: The terminal refused the command, for example a negative `expiration`, or a history range MT5 can't select.; `terminal_error`: The terminal did not give the answer asked for.; `engine_busy`: The account's engine host has too many calls in flight (256). Nothing was done; try again.; `engine_unavailable`: The account's engine host is not connected to Pipsocket, or disconnected while the call waited. A trade it already had is still settled: look it up.
- 504: - `terminal_timeout`: The account's terminal did not answer within 5 seconds.; `engine_timeout`: The account's engine did not answer within 30 seconds.

### Example

```bash
curl "https://api.pipsocket.com/v1/accounts/acc_01m6tx3k9f2qw8r7bznd5yvh4c/history/orders?from=1794009600&to=1794095999" \
  -H "Authorization: Bearer $PIPSOCKET_KEY"
```

200 orders:

```json
[
  {
    "comment": "",
    "magic": 0,
    "position": 881402311,
    "price_open": 0,
    "state": "filled",
    "symbol": "EURUSD",
    "ticket": 881402311,
    "time_done_msc": 1794067801211,
    "time_setup_msc": 1794067801150,
    "type": "buy",
    "volume_current": 0,
    "volume_initial": 0.1
  }
]
```
