# Trading

The Trading session enables order placement and execution management via the FIX 4.4 protocol. Use this session to submit orders and receive real-time execution reports for your trading activity.

{% hint style="warning" %}
For FIX connection settings (host, port, SenderCompID, TargetCompID, credentials), contact your broker.
{% endhint %}

{% hint style="info" %}
This page covers the **Trading** session only. For real-time order book streaming, use the [Market Data](https://api-docs.b2trader.b2broker.com/fix-api/market-data) session.
{% endhint %}

{% file src="<https://506308843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWqnNf2YTrJmFWsk6j8yo%2Fuploads%2Fgit-blob-be8761bf7689579219523ad1da211923485647e7%2FBbpFIX44.xml?alt=media>" %}
FIX 4.4 Data Dictionary
{% endfile %}

## Supported message types

The following values can be assigned to the `<35>` MsgType field:

* `A` — Logon (Client → B2TRADER)
* `0` — Heartbeat (Client ↔ B2TRADER)
* `1` — Test Request (Client ↔ B2TRADER)
* `3` — Reject (Client ← B2TRADER)
* `4` — Sequence Reset (Client ↔ B2TRADER)
* `5` — Logout (Client ↔ B2TRADER)
* `D` — New Order Single (Client → B2TRADER)
* `8` — Execution Report (Client ← B2TRADER)
* `j` — Business Reject (Client ← B2TRADER)

## Getting started

### Connection

To connect to the Trading session, use the following parameters provided by B2TRADER:

* **Host and port**: The Trading endpoint (provided separately from the Market Data endpoint)
* **SenderCompID**: Your client identifier for the Trading session
* **TargetCompID**: The server identifier for the Trading session
* **Protocol**: FIX 4.4

{% hint style="warning" %}
The Trading connection requires SSL with a self-signed certificate.
{% endhint %}

### Message structure

{% hint style="info" %}
**Standard Header**

All FIX messages must begin with a Standard Header containing the following fields:

**`8 BeginString`** `String`

Identifies the FIX version (`FIX.4.4`). Always the first field in a message.

**`9 BodyLength`** `int`

The automatically computed message length, in bytes. Always the second field.

**`35 MsgType`** `String`

The message type. See [Supported message types](#supported-message-types) for possible values. Always the third field.

**`34 MsgSeqNum`** `int`

The message sequence number, incremented by 1 for each consecutive message.

**`49 SenderCompID`** `String`

The identifier of the message sender. Provided by B2TRADER.

**`52 SendingTime`** `Timestamp`

The date and time when the message was sent, in UTC: `YYYYMMDD-HH:MM:SS.sss`.

**`56 TargetCompID`** `String`

The identifier of the message recipient. Provided by B2TRADER.

***

**Standard Trailer**

All FIX messages must end with a Standard Trailer:

**`10 CheckSum`** `int`

A three-digit checksum. Always the last field in a message.
{% endhint %}

### Logon (A)

This message is sent by the client to initiate a FIX session. It must be the first message in each connection.

{% columns %}
{% column width="60%" %}
**`1 Account`** `String`

The account identifier. Required. Provided by B2TRADER.

**`98 EncryptMethod`** `int`

The encryption method. Required. Must be `0` (no encryption).

**`108 HeartBtInt`** `int`

The heartbeat interval, in seconds. Required. Indicates how often the server sends Heartbeat messages as part of a connection health check.

**`141 ResetSeqNumFlag`** `Boolean`

Indicates whether both parties should reset the currently used sequence numbers. Optional.

**`553 Username`** `String`

The client username. Required. Provided by B2TRADER.

**`554 Password`** `String`

The client password. Required. Provided by B2TRADER.
{% endcolumn %}

{% column width="40%" %}
{% code title="Request (Client → B2TRADER)" overflow="wrap" %}

```
8=FIX.4.4^9=117^35=A^1=68a4446ac84827ff5cd35c74^34=1^52=20231218-07:59:06.000^49=sender_b2trader^56=target_b2trader^554=password^553=username^98=0^108=30^10=117^
```

{% endcode %}

{% code title="Response (B2TRADER → Client)" overflow="wrap" %}

```
8=FIX.4.4^9=93^35=A^1=68a4446ac84827ff5cd35c74^34=225^49=target_b2trader^52=20231218-07:59:06.655^56=sender_b2trader^98=0^108=30^10=054^
```

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

### Session maintenance

#### Heartbeat (0)

This message is sent back and forth between the server and the client to check the connection status and in response to Test Request messages.

{% columns %}
{% column width="60%" %}
**`112 TestReqID`** `String`

The identifier of a Test Request in response to which this Heartbeat is sent. Conditional — required when sent in response to a Test Request.
{% endcolumn %}

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

```
8=FIX.4.4^9=79^35=0^34=2^52=20231218-07:59:36.000^49=sender_b2trader^56=target_b2trader^10=156^
```

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

#### Test Request (1)

This message is sent back and forth between the server and the client as a means of connectivity check. If a Heartbeat is not received within the expected interval, a Test Request is sent; the recipient must respond with a Heartbeat containing the same `<112>` TestReqID.

{% columns %}
{% column width="60%" %}
**`112 TestReqID`** `String`

The identifier of a Test Request. Required.
{% endcolumn %}

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

```
8=FIX.4.4^9=87^35=1^34=137^52=20231218-10:12:38.000^49=sender_b2trader^56=target_b2trader^112=2^10=250^
```

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

#### Sequence Reset (4)

This message indicates the sequence number of the next message from the sender, immediately following the Sequence Reset. This may be necessary to recover from a disconnect when some messages were lost or their resending is not desirable.

{% columns %}
{% column width="60%" %}
**`123 GapFillFlag`** `Boolean`

Indicates that this message replaces missing messages that won't be resent. Optional.

Possible values:

* `Y` — Gap fill: `<34>` MsgSeqNum is valid and indicates the beginning of the gap fill range
* `N` — Sequence reset: `<34>` MsgSeqNum is ignored. Should only be used in disaster recovery situations

**`36 NewSeqNo`** `int`

The new sequence number. Required.
{% endcolumn %}

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

```
8=FIX.4.4^9=90^35=4^34=6^49=target_b2trader^52=20231219-21:11:38.578^56=sender_b2trader^123=Y^36=8^10=176^
```

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

#### Logout (5)

This message is sent by the client or server to terminate a session. When terminated, the possible reason is specified in the `<58>` Text field.

{% columns %}
{% column width="60%" %}
**`58 Text`** `String`

The detailed information about the reason for logging out. Optional.
{% endcolumn %}

{% column width="40%" %}
{% code title="Request (Client → B2TRADER)" overflow="wrap" %}

```
8=FIX.4.4^9=105^35=5^34=5^52=20231218-13:40:48.000^49=sender_b2trader^56=target_b2trader^58=Session terminated by client^10=183^
```

{% endcode %}

{% code title="Response (B2TRADER → Client)" overflow="wrap" %}

```
8=FIX.4.4^9=81^35=5^34=748^49=target_b2trader^52=20231218-13:40:49.016^56=sender_b2trader^10=009^
```

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

### Reject (3)

This message is sent by the server upon receiving a malformed message from the client. The rejection reason is specified in the `<373>` SessionRejectReason field.

{% hint style="warning" %}
This message is unrelated to application-level rejections (Execution Report with rejected status and Business Reject).
{% endhint %}

{% columns %}
{% column width="60%" %}
**`45 RefSeqNum`** `int`

The sequence number of the rejected message (`<34>` MsgSeqNum). Required.

**`371 RefTagID`** `int`

The tag number of the field that caused message rejection. Optional.

**`372 RefMsgType`** `String`

The type of the rejected message (`<35>` MsgType). Optional.

**`373 SessionRejectReason`** `int`

The reason why the message is rejected. Optional.

Possible values:

* `0` — Invalid tag number
* `1` — Required tag missing
* `2` — Tag not defined for this message type
* `3` — Undefined tag
* `4` — Tag has no value assigned
* `5` — Value is incorrect (out of range) for this tag
* `6` — Incorrect value data format
* `7` — Decryption issue
* `8` — Signature problem
* `9` — CompID issue
* `10` — SendingTime accuracy issue
* `11` — Invalid MsgType
* `12` — XML validation error
* `13` — Same tag appears more than once
* `14` — Tag specified not in required order
* `15` — Wrong order of repeating group fields
* `16` — Incorrect NumInGroup count for repeating group
* `17` — Non-"Data" value includes field delimiter (SOH character)
* `99` — Other

**`58 Text`** `String`

The detailed information about the rejection reason. Optional.
{% endcolumn %}

{% column width="40%" %}
{% code title="Example (B2TRADER → Client)" overflow="wrap" %}

```
8=FIX.4.4^9=128^35=3^34=193^52=20231219-22:41:16.000^49=target_b2trader^56=sender_b2trader^45=18^371=11^372=D^373=1^58=Required tag missing: ClOrdID^10=126^
```

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

***

## New Order Single (D)

This message is sent by the client to place a new order. The server responds with an Execution Report confirming the order status.

For details on supported order types, see [Order types](https://api-docs.b2trader.b2broker.com/get-started/order-types). For details on time-in-force options, see [Time in force](https://api-docs.b2trader.b2broker.com/get-started/time-in-force).

{% columns %}
{% column width="60%" %}
**`11 ClOrdID`** `String`

The unique client-assigned order identifier. Required.

**`1 Account`** `String`

The account identifier. Required. Provided by B2TRADER.

**`55 Symbol`** `String`

The market identifier. Required. Format: `{marketType}.{baseAssetId}_{quoteAssetId}`, for example: `spot.btc_usdt`.

**`54 Side`** `char`

The order side. Required.

Possible values:

* `1` — Buy
* `2` — Sell

**`38 OrderQty`** `Qty`

The order quantity. Required. Must be greater than zero. The decimal precision must not exceed the market's amount scale, and the value must be at least the market's minimum amount.

**`40 OrdType`** `char`

The order type. Required.

Possible values:

* `1` — Market
* `2` — Limit

**`59 TimeInForce`** `char`

The order's time-in-force policy. Required.

Possible values:

* `0` — Day
* `1` — Good Till Cancel (GTC)
* `3` — Immediate or Cancel (IOC)
* `4` — Fill or Kill (FOK)
* `6` — Good Till Date (GTD)

**`44 Price`** `Price`

The order price. Conditional — required when `<40>` OrdType is `2` (Limit), must not be present when `<40>` OrdType is `1` (Market). Must be greater than zero. The decimal precision must not exceed the market's price scale.

**`126 ExpireTime`** `UTCTimestamp`

The order expiration time. Conditional — required when `<59>` TimeInForce is `6` (GTD), must not be present otherwise.

**`60 TransactTime`** `UTCTimestamp`

The time of order creation. Required.
{% endcolumn %}

{% column width="40%" %}
{% code title="Limit order example (Client → B2TRADER)" overflow="wrap" %}

```
8=FIX.4.4^9=168^35=D^34=3^52=20231220-09:15:30.000^49=sender_b2trader^56=target_b2trader^1=68a4446ac84827ff5cd35c74^11=order001^55=spot.btc_usdt^54=1^38=0.5^40=2^44=42500.00^59=1^60=20231220-09:15:30.000^10=123^
```

{% endcode %}

{% code title="Market order example (Client → B2TRADER)" overflow="wrap" %}

```
8=FIX.4.4^9=155^35=D^34=4^52=20231220-09:16:00.000^49=sender_b2trader^56=target_b2trader^1=68a4446ac84827ff5cd35c74^11=order002^55=spot.btc_usdt^54=2^38=0.1^40=1^59=3^60=20231220-09:16:00.000^10=045^
```

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

## Execution Report (8)

This message is sent by the server to confirm order status changes, including acknowledgment of new orders, fills, partial fills, cancellations, and rejections.

For details on order statuses, see [Order statuses](https://api-docs.b2trader.b2broker.com/get-started/order-statuses).

{% columns %}
{% column width="60%" %}
**`37 OrderID`** `String`

The server-assigned unique order identifier. Required.

**`11 ClOrdID`** `String`

The client-assigned order identifier from the original New Order Single. Required.

**`17 ExecID`** `String`

The unique execution identifier. Present for trade executions.

**`150 ExecType`** `char`

The type of execution being reported. Required.

Possible values:

* `0` — New: order has been accepted
* `4` — Canceled: order has been canceled by the server (e.g., IOC order partially filled, GTD order expired, or market settings changed)
* `8` — Rejected: order has been rejected
* `F` — Trade: order has been partially or fully filled

**`39 OrdStatus`** `char`

The current order status. Required.

Possible values:

* `0` — New
* `1` — Partially filled
* `2` — Filled
* `4` — Canceled
* `8` — Rejected

**`1 Account`** `String`

The account identifier. Required.

**`55 Symbol`** `String`

The market identifier. Format: `{marketType}.{baseAssetId}_{quoteAssetId}`.

**`54 Side`** `char`

The order side. Required.

Possible values:

* `1` — Buy
* `2` — Sell

**`40 OrdType`** `char`

The order type. Required.

Possible values:

* `1` — Market
* `2` — Limit

**`44 Price`** `Price`

The order price. Present for Limit orders.

**`6 AvgPx`** `Price`

The average price of all fills on this order. Required.

**`14 CumQty`** `Qty`

The total filled quantity. Required.

**`151 LeavesQty`** `Qty`

The remaining quantity to be filled. Required. Set to `0` for Canceled or Rejected orders.

**`31 LastPx`** `Price`

The price of the last fill. Present when `<150>` ExecType is `F` (Trade).

**`32 LastQty`** `Qty`

The quantity of the last fill. Present when `<150>` ExecType is `F` (Trade).

**`15 Currency`** `String`

The quote asset identifier. Optional.

**`60 TransactTime`** `UTCTimestamp`

The transaction time. Required.

**`64 SettlDate`** `String`

The settlement date in `YYYYMMDD` format. Required.

**`58 Text`** `String`

Additional information, such as the rejection reason. Optional.
{% endcolumn %}

{% column width="40%" %}
{% code title="New order accepted (B2TRADER → Client)" overflow="wrap" %}

```
8=FIX.4.4^9=220^35=8^34=5^52=20231220-09:15:30.100^49=target_b2trader^56=sender_b2trader^37=01HBXK5V3R8NQ7YP^11=order001^150=0^39=0^1=68a4446ac84827ff5cd35c74^55=spot.btc_usdt^54=1^40=2^44=42500.00^6=0^14=0^151=0.5^60=20231220-09:15:30.100^64=20231220^10=087^
```

{% endcode %}

{% code title="Trade execution (B2TRADER → Client)" overflow="wrap" %}

```
8=FIX.4.4^9=245^35=8^34=6^52=20231220-09:15:30.200^49=target_b2trader^56=sender_b2trader^37=01HBXK5V3R8NQ7YP^11=order001^17=01HBXK5V3R8NQ7YR^150=F^39=2^1=68a4446ac84827ff5cd35c74^55=spot.btc_usdt^54=1^40=2^44=42500.00^6=42500.00^14=0.5^151=0^31=42500.00^32=0.5^15=usdt^60=20231220-09:15:30.200^64=20231220^10=154^
```

{% endcode %}

{% code title="Order rejected (B2TRADER → Client)" overflow="wrap" %}

```
8=FIX.4.4^9=214^35=8^34=7^52=20231220-09:16:00.100^49=target_b2trader^56=sender_b2trader^37=01HBXK5V3R8NQ7YS^11=order002^150=8^39=8^1=68a4446ac84827ff5cd35c74^55=spot.btc_usdt^54=2^40=1^6=0^14=0^151=0^58=Insufficient balance^60=20231220-09:16:00.100^64=20231220^10=201^
```

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

## Business Reject (j)

This message is sent by the server to reject a message due to a business-level issue not addressed by the standard session-level Reject or Execution Report rejection.

{% columns %}
{% column width="60%" %}
**`45 RefSeqNum`** `int`

The sequence number of the rejected message (`<34>` MsgSeqNum). Required.

**`372 RefMsgType`** `String`

The type of the rejected message (`<35>` MsgType). Optional.

**`380 BusinessRejectReason`** `int`

The reason why the request is rejected. Required.

Possible values:

* `0` — Other
* `1` — Unknown ID
* `2` — Unknown Security
* `3` — Unsupported MsgType
* `4` — Application not available
* `5` — Conditionally required field missing
* `6` — Not authorized
* `7` — DeliverTo firm not available at this time

**`58 Text`** `String`

The detailed information about the rejection reason. Optional.
{% endcolumn %}

{% column width="40%" %}
{% code title="Example (B2TRADER → Client)" overflow="wrap" %}

```
8=FIX.4.4^9=100^35=j^34=2^49=target_b2trader^52=20231219-22:30:39.617^56=sender_b2trader^45=133^58=Unsupported Message Type^372=V^380=3^10=006^
```

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