Market Data Protocol Specification
The CryptoStruct market data protocol uses JSON arrays for every message: requests, responses, and events. The first element of the array is always an integer message type; the rest is type-specific.
[
msgType,
...
]
Tip
The stand-alone release of this specification ships sample data for every message, in both JSON and SBE encoding. Download it from cryptostruct.com/download and test your parser against it rather than hand-copying the examples below.
Requests
Login
Login is required before subscribing to any instrument.
Request
[
13,
organization,
application_name,
application_version,
process_id
]
[
13,
"ACME",
"MyApp",
"1.2.3",
"MyApp_Tokyo"
]
Response
[
14,
process_id,
application_version,
protocol,
capabilities
]
[
14,
"bitmex-master-a",
"1.23.4",
"5",
{
"depthTopic": {
"eventIdType": "ORDERED",
"exchangeTimestampType": "UNKNOWN",
"exchangeTimestampPrecision": "MILLIS"
},
"topOfBookTopic": null,
"tradesTopic": null,
"crossTopicBookEventId": true
}
]
Note
There is no explicit logout. To change login data, reconnect.
Subscription
[
11,
instrument,
options
]
Options (defaults shown):
{
"depthTopic": true,
"tradesTopic": true,
"topOfBookTopic": true,
"indexPriceTopic": true,
"markPriceTopic": true,
"fundingRateTopic": true,
"liquidationsTopic": false,
"topOfBookCoalescing": false
}
- Topic flags: pick which market-data topics to subscribe to.
topOfBookCoalescing: when enabled, the Market Data Adapter drops outdated top-of-book updates during bursts or under load and publishes only the latest event.
[
11,
22,
{
"depthTopic": false
}
]
Behaviour:
-
On a successful subscription, the first message received is an initial snapshot of the order book, followed by other initial data and events.
-
If the subscription fails, an error state event is received for the instrument.
-
An instrument cannot be subscribed multiple times.
Unsubscription
[
12,
instrument
]
[
12,
22
]
Events
Common
Every event except Instrument State uses the same shell:
[
msgType,
instrument,
prevEventId,
eventId,
adapterTimestamp,
exchangeTimestamp,
data
]
Common fields:
Message types:
Snapshot
A snapshot is the full state of the order book.
The Snapshot event differs from other events in one way: the trailing
data slot is replaced by two top-level fields, the levels array and a
forceReset boolean.
[
[side, price, quantity, ordercount],
[side, price, quantity, ordercount],
...
],
forceReset
Note
forceReset only matters when consuming multiple feeds for feed
arbitrage. On a single feed, every snapshot should be accepted and
reset the local book.
[
0,
123456,
"12345678-1",
"12345678-2",
1580143531008103451,
1580143530031129024,
[
[
0,
"7098.25",
"10",
1
],
[
1,
"7099.25",
"20",
1
]
],
true
]
Book Update
A book update is a level-based change to the order book.
[
[side, price, quantity, ordercount],
[side, price, quantity, ordercount],
...
]
[
1,
123456,
"12345678-1",
"12345678-2",
1580143531008103451,
1580143530031129024,
[
[
0,
"7098.25",
"10",
1
],
[
1,
"7099.25",
"20",
1
]
]
]
Trades
A list of trades that occurred.
[
[side, price, quantity, tradeId, exchangeTradeTimestamp],
...
]
[
2,
123456,
"12345678-2",
"12345679-3",
1580143531008103451,
1580143530031129024,
[
[
0,
"7098.25",
"10",
"6as78d678asdf78as789fas",
11580143530031129024
]
]
]
Top-of-Book
An array with up to two levels (the top level per side).
[
[side, price, quantity, ordercount],
[side, price, quantity, ordercount]
]
Behaviour:
-
If one side is empty, the array carries one element (the non-empty side).
-
If both sides are empty, the array is empty.
-
Bid and ask order is undefined.
[
6,
123456,
"12345678-1",
"12345678-2",
1580143531008103451,
1580143530031129024,
[
[
0,
"7098.25",
"10",
1
],
[
1,
"7099.25",
"20",
1
]
]
]
Mark Price
Mark price for the instrument. Option instruments also publish Greeks
and implied volatilities; non-option instruments leave those fields
null.
markPrice, delta, gamma, vega, theta, markVolatility, bidVolatility, askVolatility
[
7,
123456,
"12345678-1",
"12345678-2",
1580143531008103451,
1580143530031129024,
"123.456",
null,
null,
null,
null,
null,
null,
null
]
[
7,
123456,
"12345678-1",
"12345678-2",
1640995200300000000,
1640995200200000000,
"62604.69",
"-0.9999672034",
"0.0000000002",
"0.0000114332",
"28.2649858387",
"0.3675503331",
"0.36",
"0.375"
]
Index Price
Index price for the instrument.
price
[
8,
123456,
"12345678-1",
"12345678-2",
1580143531008103451,
1580143530031129024,
"456.789"
]
Funding Rate
Last funding rate, time of next funding, and the predicted next funding rate.
[
lastFundingRate,
nextFundingTime,
nextFundingRate
]
[
9,
123456,
"12345678-1",
"12345678-2",
1580143531008103451,
1580143530031129024,
[
"0.123",
1580163530031129024,
"0.123"
]
]
Liquidations
A list of liquidation orders.
Note
Some exchanges publish only a sample of liquidations rather than all of them.
[
[side, price, quantity, exchangeLiquidationTimestamp],
...
]
[
17,
123456,
"12345678-2",
"12345679-3",
1580143531008103451,
1580143530031129024,
[
[
0,
"7098.25",
"10",
11580143530031129024
]
]
]
Instrument State
The state of the instrument. Moves to ERROR when the connection to the
exchange drops or when the subscription fails (e.g. unknown instrument).
Warning
The instrument state message is shaped differently from the others: no event IDs, no exchange timestamp.
[
msgType,
instrument,
adapterTimestamp,
state,
message
]
[
5,
123456,
1580143531008103451,
"ERROR",
"connection lost"
]
Market Data Capabilities
Available capabilities depend on the exchange and on the Market Data Adapter version. Capabilities describe which data feeds are available and how they behave.
Capability structure
{
"depthTopic": {
"eventIdType": "ORDERED",
"exchangeTimestampType": "UNKNOWN",
"exchangeTimestampPrecision": "MILLIS"
},
"topOfBookTopic": {
"eventIdType": "ORDERED",
"exchangeTimestampType": "UNKNOWN",
"exchangeTimestampPrecision": "MILLIS"
},
"tradesTopic": {
"eventIdType": "UNORDERED",
"exchangeTimestampType": "MATCHING_ENGINE",
"exchangeTimestampPrecision": "MICROS"
},
"fundingRateTopic": null,
"markPriceTopic": null,
"indexPriceTopic": null,
"liquidationsTopic": null,
"crossTopicBookEventId": true,
"predictedFundingRate": false
}
Topic fields
Each *Topic field describes a market-data topic. If the topic isn't
available, the field is null. Available topic fields:
depthTopic, topOfBookTopic, tradesTopic, markPriceTopic,
indexPriceTopic, fundingRateTopic, liquidationsTopic.
Event ID Type
The nature of the event ID.
Exchange Timestamp Type
Where the exchange timestamp comes from.
Exchange Timestamp Precision
Precision of the exchange timestamp. null when exchangeTimestampType
is NONE.
Cross-Topic Book Event ID
Field: crossTopicBookEventId
- Type: Boolean.
- Description: if
true,depthTopicandtopOfBookTopicuse ordered event IDs and share the same exchange-side sequence. The event ID can be used to determine whether depth data or top-of-book data is more recent.
Predicted Funding Rate
Field: predictedFundingRate
- Type: Boolean.
- Description: if
true, the exchange supports the predicted funding rate.
See Also
- Accessing Data. How to connect and subscribe.
- SBE Encoding. Simple Binary Encoding for market data.
- Binary Framing. Message framing for domain sockets.