# Trading

## Get open orders

### Connection

{% code title="URL" %}

```
/frontoffice/ws/v4/account?access_token={YOUR_ACCESS_TOKEN}
```

{% endcode %}

{% columns %}
{% column width="60%" %}
**`arguments`** `array`

The connection parameters.

In the first element, provide the `accountId` as a string.

**`invocationId`** `string`

The invocation identifier.

Must be unique and increase by 1 for each sent message.

**`target`** `string`

The stream name.

Specify `"OpenOrders"`.

**`type`** `int`

The operation type.

Set to `4` to indicate a subscription to the stream.
{% endcolumn %}

{% column width="40%" %}
{% code title="Example" overflow="wrap" %}

```json
{
  "arguments": [
    "67d0456f8c7b1108e4cf5d46"
  ],
  "invocationId": "0",
  "target": "OpenOrders",
  "type": 4
}
```

{% endcode %}
{% endcolumn %}
{% endcolumns %}

***

### Message

{% columns %}
{% column width="60%" %}
**`type`** `string`

The operation type.

`2` indicates the streaming is in progress.

**`invocationId`** `string`

The invocation identifier. Same as in the request.

**`item`** `array of objects`

The array of market objects.

**`item.marketId`** `string`

The market identifier, in the following format: `{marketType}.{baseAssetId}_{quoteAssetId}`, for example: `cfd.eth_eur`.

**`item.marketType`** `string`

The market type.

Possible values:

* `Spot`
* `Cfd`
* `Perp`

**`item.marketDisplayName`** `string`

The market ticker.

**`item.marketFullName`** `string`

The market full name or description (optional).

**`item.orderId`** `string`

The unique identifier of the order assigned by the system.

**`item.timeInForce`** `string`

The [time-in-force policy](/get-started/time-in-force.md) for the order, controlling its lifetime.

Possible values:

* `Gtc`
* `Ioc`
* `Fok`
* `Gtd`
* `Day`

**`item.status`** `string`

The current [order status](/get-started/order-statuses.md).

Possible values:

* `Started`
* `Pending`
* `Working`

**`item.source`** `string`

The source of the order.

Possible values:

* `Manual` — the order was created manually via UI or API.

**`item.reason`** `string`

The reason for placing the order.

Possible values:

* `Trader`
* `StopOut`
* `MarketHalted`
* `MarketDisabled`
* `TakeProfit`
* `StopLoss`
* `Admin`

**`item.side`** `string`

The order side, indicating the direction of the trade.

Possible values:

* `Buy`
* `Sell`

**`item.orderType`** `string`

The [order type](/get-started/order-types.md).

Possible values:

* `Market`
* `Limit`

**`item.requestedAmount`** `decimal string`

The quantity of the base asset to buy or sell. For market orders, this represents the total base amount to fill; the executed amount may be lower if liquidity is insufficient.

**`item.remainingAmount`** `decimal string`

The order amount that hasn't yet been filled, in the base asset.

**`item.requestedPrice`** `decimal string`

The limit price for Limit orders; `null` for Market orders.

**`item.executionPrice`** `decimal string`

The volume-weighted average price of the order executions.

**`item.createdAt`** `dateTime`

The timestamp when the order was created, in ISO 8601 format.

**`item.updatedAt`** `dateTime`

The timestamp of the most recent update to the order, in ISO 8601 format.

**`item.cancellationDate`** `dateTime`

The timestamp when the order was cancelled or expired, in ISO 8601 format; `null` if not cancelled.

**`item.commissionAssetId`** `string`

The currency in which the commission was held.

**`item.commissionAmount`** `decimal string`

The total commissions put on hold for executing the order.

**`item.leverage`** `int`

*Applicable only to CFD markets.*

The leverage ratio used when placing the order.

**`item.fillFactor`** `decimal string`

The proportion of the order amount filled so far, where `1` represents 100% fulfillment.

**`item.comment`** `string | nullable`

The text note attached to the order, up to 100 characters.

**`item.takeProfit`** `decimal string`

The Take Profit price, if set.

**`item.stopLoss`** `decimal string`

The Stop Loss price, if set.
{% endcolumn %}

{% column width="40%" %}
{% code title="" overflow="wrap" %}

```json
{
  "type": 2,
  "invocationId": "0",
  "item": [
    {
      "marketId": "cfd.eur_chf",
      "marketType": "Cfd",
      "marketDisplayName": "EUR/CHF",
      "marketFullName": "",
      "orderId": "01JVQBFSTVC40VK03A0AY7K016",
      "timeInForce": "Gtc",
      "status": "Pending",
      "source": "Manual",
      "reason": "Trader",
      "side": "Buy",
      "orderType": "Limit",
      "requestedAmount": "10000",
      "remainingAmount": "10000",
      "requestedPrice": "0.9",
      "executionPrice": "0",
      "createdAt": "2025-05-20T17:22:31.899Z",
      "updatedAt": "2025-05-20T17:22:31.9001213Z",
      "cancellationDate": null,
      "commissionAssetId": "eur",
      "commissionAmount": "0",
      "leverage": 1,
      "fillFactor": "0",
      "takeProfit": null,
      "stopLoss": null,
      "comment": null
    }
  ]
}
```

{% endcode %}
{% endcolumn %}
{% endcolumns %}

## Get open positions

### Connection

{% code title="URL" overflow="wrap" %}

```
/frontoffice/ws/v4/account?access_token={YOUR_ACCESS_TOKEN}
```

{% endcode %}

{% columns %}
{% column width="60%" %}
**`arguments`** `array`

The connection parameters.

In the first element, provide the `accountId` as a string.

**`invocationId`** `string`

The invocation identifier.

Must be unique and increase by 1 for each sent message.

**`target`** `string`

The stream name.

Specify `"OpenPositions"`.

**`type`** `int`

The operation type.

Set to `4` to indicate a subscription to the stream.
{% endcolumn %}

{% column width="40%" %}
{% code title="Example" overflow="wrap" %}

```json
{
  "arguments": [
    "67d0456f8c7b1108e4cf5d46"
  ],
  "invocationId": "0",
  "target": "OpenPositions",
  "type": 4
}
```

{% endcode %}
{% endcolumn %}
{% endcolumns %}

***

### Message

{% columns %}
{% column width="60%" %}
**`type`** `string`

The operation type.

`2` indicates the streaming is in progress.

**`invocationId`** `string`

The invocation identifier. Same as in the request.

**`item`** `object`

The dataset object.

**`item.item`** `array of objects`

The array of position objects.

**`item.item.positionId`** `string`

The unique identifier of the position assigned by the system.

**`item.item.marketId`** `string`

The market identifier, in the following format: `{marketType}.{baseAssetId}_{quoteAssetId}`, for example: `cfd.eth_eur`.

**`item.item.marketType`** `string`

The market type.

Possible values:

* `Spot`
* `Cfd`
* `Perp`

**`item.item.marketDisplayName`** `string`

The market ticker.

**`item.item.marketFullName`** `string`

The market full name or description (optional).

**`item.item.createdAt`** `dateTime`

The timestamp when the position was opened, in ISO 8601 format.

**`item.item.updatedAt`** `dateTime`

The timestamp of the most recent update to the position, in ISO 8601 format.

**`item.item.side`** `string`

The position side. Possible values:

* `Buy`
* `Sell`

**`item.item.status`** `string`

The current position status. Always `"Open"`.

**`item.item.leverage`** `int`

*Applicable only to CFD markets.*

The leverage ratio used when opening the position.

**`item.item.positionLotAmount`** `decimal string`

The position volume, in lots.

**`item.item.positionPriceInRAT`** `decimal string`

The current position price, in conversion to RAT.

**`item.item.rateToRAT`** `decimal string`

The current exchange rate of a quote asset to RAT.

**`item.item.usedMarginInRAT`** `decimal string`

The amount of trader’s funds used for maintaining a position, in conversion to RAT.

**`item.item.openPrice`** `decimal string`

The volume-weighted average price (VWAP) at which the position was opened.

**`item.item.currentMarketPrice`** `decimal string`

The current market price of the base asset: bid for Long positions and ask for Short positions.

**`item.item.unrealizedPnlDayInRAT`** `decimal string`

The potential profit or loss earned for a current day, in conversion to RAT.

For **Long** positions, this value is calculated as *Position size* × (*Current bid price* – *First bid price for today*).

For **Short** positions, this value is calculated as *Position size* × (*First ask price for today* – *Current ask price*).

If a position was opened today, then the *Open VWAP* is used instead of the *First price for today*.

**`item.item.unrealizedPnlDayPercent`** `decimal string`

The potential profit or loss earned for a current day, in percents.

**`item.item.unrealizedPnlTotalInRAT`** `decimal string`

The potential profit or loss earned for the entire period from the moment the position was opened, in conversion to RAT.

For **Long** positions, this value is calculated as *Position size* × (*Current bid price* – *Open VWAP*).

For **Short** positions, this value is calculated as *Position size* × (*Open VWAP* – *Current ask price*).

**`item.item.unrealizedPnlTotalPercent`** `decimal string`

The potential profit or loss earned for the entire period from the moment the position was opened, in conversion to RAT, in percents.

**`item.item.takeProfit`** `decimal string`

The Take Profit price, if set.

**`item.item.stopLoss`** `decimal string`

The Stop Loss price, if set.

**`item.item.positionModifier`** `string`

The reason for the latest position update.

**`item.item.comment`** `string | nullable`

The text note inherited from the opening order, up to 100 characters.

**`snapshot`** `boolean`

The data snapshot.

If `snapshot: true`, it indicates the message contains the full dataset. Subsequent messages with `snapshot: false` only include updates or changes since the initial snapshot.
{% endcolumn %}

{% column width="40%" %}
{% code title="Example" overflow="wrap" %}

```json
{
  "type": 2,
  "invocationId": "0",
  "item": {
    "item": [
      {
        "positionId": "01JP4H3AMS7Q1H6Y6H3XJ52JTA",
        "marketId": "cfd.eur_chf",
        "marketType": "Cfd",
        "marketDisplayName": "EUR/CHF",
        "marketFullName": "",
        "createdAt": "2025-03-12T06:36:31.257Z",
        "updatedAt": "2025-03-12T06:36:31.257Z",
        "side": "Buy",
        "status": "Open",
        "leverage": 1,
        "positionLotAmount": "0.01",
        "positionPriceInRAT": "1000.46",
        "rateToRAT": "1.07",
        "usedMarginInRAT": "1000.53",
        "openPrice": "0.96304",
        "currentMarketPrice": "0.93501",
        "unrealizedPnlDayInRAT": "1.86",
        "unrealizedPnlDayPercent": "0.0018",
        "unrealizedPnlTotalInRAT": "-29.93",
        "unrealizedPnlTotalPercent": "-0.0291",
        "takeProfit": null,
        "stopLoss": null,
        "positionModifier": "Trader",
        "comment": null
      },
      {
        "positionId": "01JVQB9ZWJ6G4QV0P98X0QWNA7",
        "marketId": "cfd.eur_chf",
        "marketType": "Cfd",
        "marketDisplayName": "EUR/CHF",
        "marketFullName": "",
        "createdAt": "2025-05-20T17:19:21.49Z",
        "updatedAt": "2025-05-20T17:19:21.491321Z",
        "side": "Buy",
        "status": "Open",
        "leverage": 100,
        "positionLotAmount": "0.1",
        "positionPriceInRAT": "10004.6",
        "rateToRAT": "1.07",
        "usedMarginInRAT": "100.06",
        "openPrice": "0.93666",
        "currentMarketPrice": "0.93501",
        "unrealizedPnlDayInRAT": "18.61",
        "unrealizedPnlDayPercent": "0.0018",
        "unrealizedPnlTotalInRAT": "-17.02",
        "unrealizedPnlTotalPercent": "-0.0017",
        "takeProfit": null,
        "stopLoss": null,
        "positionModifier": "Trader",
        "comment": null
      }
    ],
    "snapshot": false
  }
}
```

{% endcode %}
{% endcolumn %}
{% endcolumns %}

## Get closed positions

### Connection

{% code title="URL" overflow="wrap" %}

```
/frontoffice/ws/v4/account?access_token={YOUR_ACCESS_TOKEN}
```

{% endcode %}

{% columns %}
{% column width="60%" %}
**`arguments`** `array`

The connection parameters.

In the first element, provide the `accountId` as a string.

**`invocationId`** `string`

The invocation identifier.

Must be unique and increase by 1 for each sent message.

**`target`** `string`

The stream name.

Specify `"ClosePositionsOrders"`.

**`type`** `int`

The operation type.

Set to `4` to indicate a subscription to the stream.
{% endcolumn %}

{% column width="40%" %}
{% code title="Example" overflow="wrap" %}

```json
{
  "arguments": [
    "67d0456f8c7b1108e4cf5d46"
  ],
  "invocationId": "0",
  "target": "ClosePositionsOrders",
  "type": 4
}
```

{% endcode %}
{% endcolumn %}
{% endcolumns %}

***

### Message

{% columns %}
{% column width="60%" %}
**`type`** `string`

The operation type.

`2` indicates the streaming is in progress.

**`invocationId`** `string`

The invocation identifier. Same as in the request.

**`item`** `array of objects`

The array of position objects.

**`item.marketId`** `string`

The market identifier, in the following format: `{marketType}.{baseAssetId}_{quoteAssetId}`, for example: `cfd.eth_eur`.

**`item.marketType`** `string`

The market type.

Possible values:

* `Spot`
* `Cfd`
* `Perp`

**`item.marketDisplayName`** `string`

The market ticker.

**`item.marketFullName`** `string`

The market full name or description (optional).

**`item.orderId`** `string`

The unique identifier of the order assigned by the system.

**`item.orderType`** `string`

The [order type](/get-started/order-types.md).

Possible values:

* `Market`
* `Limit`

**`item.timeInForce`** `string`

The [time-in-force policy](/get-started/time-in-force.md) for the order, controlling its lifetime.

Possible values:

* `Gtc`
* `Ioc`
* `Fok`
* `Gtd`
* `Day`

**`item.side`** `string`

The order side, indicating the direction of the trade.

Possible values:

* `Buy`
* `Sell`

**`item.positionCloseLotAmount`** `decimal string`

The closed volume, in lots, which is equivalent to the corresponding filled order volume.

**`item.reason`** `string`

The reason for position closing.

Possible values:

* `Trader`
* `StopOut`
* `MarketHalted`
* `MarketDisabled`
* `TakeProfit`
* `StopLoss`
* `Admin`

**`item.realizedPnlInRAT`** `decimal string`

The actual profit or loss earned, in conversion to RAT.

For **Long** positions, this value is calculated as *Position size* × (*Close price* – *Open price*).

For **Short** positions, this value is calculated as *Position size* × (*Open price* – *Close price*).

**`item.closedAt`** `dateTime`

The timestamp when the position was closed, in ISO 8601 format.

**`item.positionId`** `string`

The unique identifier of the position assigned by the system.

**`item.openPrice`** `decimal string`

The volume-weighted average price (VWAP) at which the position was opened.

**`item.closePrice`** `decimal string`

The volume-weighted average price (VWAP) of trades related to a position-closing order.

**`item.positionPriceInRAT`** `decimal string`

The position price, in conversion to RAT.

**`item.rateToRAT`** `decimal string`

The conversion rate to RAT.

**`item.openedAt`** `dateTime`

The timestamp when the position was opened, in ISO 8601 format.
{% endcolumn %}

{% column width="40%" %}
{% code title="Example" overflow="wrap" %}

```json
{
  "type": 2,
  "invocationId": "0",
  "item": [
    {
      "marketId": "cfd.eur_chf",
      "marketFullName": "",
      "marketDisplayName": "EUR/CHF",
      "marketType": "Cfd",
      "orderId": "01JVSQ8WFA3QZ6AQTKYPXVXDWA",
      "orderType": "Market",
      "timeInForce": "Ioc",
      "side": "Sell",
      "positionCloseLotAmount": "0.01",
      "reason": "Trader",
      "realizedPnlInRAT": "-29.25",
      "closedAt": "2025-05-21T15:26:57.0027785Z",
      "positionId": "01JP4H3AMS7Q1H6Y6H3XJ52JTA",
      "openPrice": "0.96304",
      "closePrice": "0.93571",
      "positionPriceInRAT": "1001.2",
      "rateToRAT": "1.07",
      "openedAt": "2025-03-12T06:36:31.257Z"
    }
  ]
}
```

{% endcode %}
{% endcolumn %}
{% endcolumns %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://api-docs.b2prime.com/websocket-api/trading.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
