> For the complete documentation index, see [llms.txt](https://tradesync.gitbook.io/tradesync/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tradesync.gitbook.io/tradesync/fxtr-v2/functions-of-fxtr-v2/open-stream-for-the-account.md).

# Open stream for the account

{% openapi src="/files/QruCxmI4jLh4jCWjCfcX" path="/stream" method="get" %}
[swagger.yaml](https://71865896-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlEheEO2iA7qJYcTvB21a%2Fuploads%2Fxi1m0mf4FvrMaNN5L6gN%2Fswagger.yaml?alt=media\&token=5e82caed-1039-44a7-b1c9-f093f9e7bb79)
{% endopenapi %}

Parameters `Upgrade` and`Coonection` are the standard options for working with web sockets. Together, these options allow the client and server to establish a Web Sockets connection.&#x20;

In our case, the value `Connection: Upgrade`in the request and response headers is a signal to change the protocol from standard HTTP to the web socket protocol and `Upgrade: websocket.`

### Features of working with open stream

Streaming connection: impormant to make ping request each 30 sec.

While opening open stream you should specify the query parameter as `stream-auth` (header `Authorization` in stream will be ignored).

1. If header `Authorization` is specified and `server_code,` but query parameter  `stream-auth` is not specified, the authorization will not be succeed:

```
Handshake Details
Request URL: https://{{HOST}}/api/v2/fxtr/stream?server_code=mt5_trial1
Request Method: GET
Status Code: 422 Unprocessable Entity

Request Headers
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: {{WS_KEY}}
Connection: Upgrade
Upgrade: websocket
Authorization: Bearer eyJ0eXAiOi...jxcohLWGgbw
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Host: {{HOST}}

Response Headers
Date: Tue, 20 Jun 2023 06:24:45 GMT
Content-Type: application/json
Content-Length: 57
Connection: keep-alive
```

2. When the header and the query parameter are specified both, when authorization will succeed:

```
Handshake Details
Request URL: https://{{HOST}}/api/v2/fxtr/stream?server_code=mt5_trial1&stream-auth=eyJ0eXAiOi...jxcohLWGgbw
Request Method: GET
Status Code: 101 Switching Protocols

Request Headers
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: {{WS_KEY}}
Connection: Upgrade
Upgrade: websocket
Authorization: Bearer eyJ0eXAiOi...jxcohLWGgbw
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Host: {{HOST}}

Response Headers
Date: Tue, 20 Jun 2023 06:26:33 GMT
Connection: upgrade
Upgrade: websocket
Sec-WebSocket-Accept: {{WS_ACCEPT}}
```

3. When opening stream query parameter is specified but header is not specified, when authorization will also succeed:

```
Handshake Details
Request URL: https://{{HOST}}/api/v2/fxtr/stream?server_code=mt5_trial1&stream-auth=eyJ0eXAiOi...jxcohLWGgbw
Request Method: GET
Status Code: 101 Switching Protocols

Request Headers
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: {{WS_KEY}}
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Host: {{HOST}}

Response Headers
Date: Tue, 20 Jun 2023 06:29:47 GMT
Connection: upgrade
Upgrade: websocket
Sec-WebSocket-Accept: {{WS_ACCEPT}}
```

4. If authorization is not specified at all, you will get error = 401:

```
Handshake Details
Request URL: https://{{HOST}}/api/v2/fxtr/stream?server_code=mt5_trial1
Request Method: GET
Status Code: 401 Unauthorized

Request Headers
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: {{WS_KEY}}
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Host: {{HOST}}

Response Headers
Date: Tue, 20 Jun 2023 06:34:56 GMT
Content-Type: application/json
Content-Length: 64
Connection: keep-alive
```

### WebSocket request format is a JSON structure with the following fields:

<table><thead><tr><th width="214.33333333333331">Key</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td>id</td><td>big integer</td><td>unique identification of message the will be used in an asynchronous responce to the request</td></tr><tr><td>type</td><td>string</td><td>a request type, defined in below section</td></tr><tr><td>body</td><td>JSON structure</td><td>contains a set of fields/parameters for the request execution</td></tr></tbody></table>

example 1. with empty body:

```json
{"id": 11, "type":"GetServerTime", "body": {}}
```

example 2. Subscription to ticks:

```json
{"id": 11, "type":"TicksSubscribe", "body": {"symbol":"EURUSD.s"}}
```

### Webscoket response structure

| Key  | Type                    | Description                                                                                                                  |
| ---- | ----------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| id   | big integer \[optional] | <p>a unique identification of the message that was specified in the request</p><p>doesn’t used in subscriptions messages</p> |
| type | string                  | a responce type, defined in below section                                                                                    |
| body | JSON structure          | contains a set of fields/values returning after a request execution or in subscription messages                              |

Example 1:

```json
request:{"id": 11, "type":"GetServerTime", "body": {}}

responce:  {"type": "Time", "id":11, "body": {"time":1686296926114}}
```

Example 2. Subscription to ticks:

```json
request: {"id": 11, "type":"TicksSubscribe", "body": {"symbol":"EURUSD.s"}}

response: {"type": "TicksSubscribe", "id":11, "body": {"symbol": "EURUSD.s"}}

subscritption: {"type": "Tick", "body": {"symbol":"EURUSD.s", "time":1686296999397, "bid":1.07668,"ask":1.07688,"rate_to_account_currency":1}}
```

#### In case of an error during a request execution the body of the responce include only two fields:

* `code` - an integer code of the error from the MetaTrader Server if the code > 0 otherwise a system error code;
* `message` - a string with error description.

Also the response will have an object “error” with the following fields:

* `code` - an integer code of the error from the MetaTrader Server if the code > 0 otherwise a system error code
* `message` - a string with error description

Example:

request: `{"id": 11, "type":"CloseOrder", "body": {"ticket":969016, "volume": 0.01, "deviation": 100}}`

response: `{"id": 11, "type": "CloseOrder", "body": {"code":10036, "message":"Position doesn't exist"}, "error": {"code": 10036, "message": "unknown MT5 error"}}`

### The following errors can be generated by the system:

<table><thead><tr><th width="154">Code</th><th>Description</th></tr></thead><tbody><tr><td><code>-1</code></td><td>when the requested type is not supported</td></tr><tr><td><code>-2</code></td><td>when the system throttles recieved message due to number of requests more then expected number of requests in a second</td></tr><tr><td><code>-3</code></td><td>when the system removes a message from the message queue due to big number unprocessed messages in the message queue</td></tr><tr><td><code>-9</code></td><td>timeout when the system did not get a responce from MT5 in expected timefrime specifically for acynchronus request for open/close/midification positions</td></tr><tr><td><code>-10017</code></td><td>Trade (for client) is disabled</td></tr><tr><td><code>-10018</code></td><td>Market closed (open/modificaion/close trade is not allowed)</td></tr></tbody></table>

### Errors from MetaTrader

<table><thead><tr><th width="311.3333333333333">Constant</th><th width="75">	  Value</th><th></th></tr></thead><tbody><tr><td>MT_RET_ERROR</td><td>2</td><td>Common error.</td></tr><tr><td>MT_RET_ERR_PARAMS</td><td>3</td><td>Invalid parameters.</td></tr><tr><td>MT_RET_ERR_DATA</td><td>4</td><td>Invalid information.</td></tr><tr><td>MT_RET_ERR_DISK</td><td>5</td><td>Hard disk error.</td></tr><tr><td>MT_RET_ERR_MEM</td><td>6</td><td>Memory error.</td></tr><tr><td>MT_RET_ERR_NETWORK</td><td>7</td><td>Network error.</td></tr><tr><td>MT_RET_ERR_PERMISSIONS</td><td>8</td><td>Not enough permissions to perform the operation.</td></tr><tr><td>MT_RET_ERR_TIMEOUT</td><td>9</td><td>Timeout expired.</td></tr><tr><td>MT_RET_ERR_CONNECTION</td><td>10</td><td>No connection.</td></tr><tr><td>MT_RET_ERR_NOSERVICE</td><td>11</td><td>Service is not available.</td></tr><tr><td>MT_RET_ERR_FREQUENT</td><td>12</td><td>Too frequent requests.</td></tr><tr><td>MT_RET_ERR_NOTFOUND</td><td>13</td><td>Not found.</td></tr><tr><td>MT_RET_ERR_PARTIAL</td><td>14</td><td>Partial error.</td></tr><tr><td>MT_RET_ERR_SHUTDOWN</td><td>15</td><td>Server shutdown in progress.</td></tr><tr><td>MT_RET_ERR_CANCEL</td><td>16</td><td>The operation has been canceled.</td></tr><tr><td>MT_RET_ERR_DUPLICATE</td><td>17</td><td>Duplicate information.</td></tr></tbody></table>
