Level2 - Market Data

{
  "id": 1545910660740,
  "type": "subscribe",
  "topic": "/contractMarket/level2:XBTUSDTM",
  "response": true
}

Topic:/contractMarket/level2:{symbol}

Subscribe this topic to get Level 2 order book data.

The websocket system will send the incremental feed to you.

  • Push frequency: real-time
  {
    "subject": "level2",
    "topic": "/contractMarket/level2:XBTUSDTM",
    "type": "message",
    "data": {
      "sequence": 18,					//Sequence number which is used to judge the continuity of pushed messages
      "change": "5000.0,sell,83"		//Price, side, quantity
      "timestamp": 1551770400000

      }
  }

Calibration procedure:

  1. After receiving the websocket Level 2 data flow, cache the data.
  2. Initiate REST [Level 2] (/docs/rest/futures-trading/market-data/get-full-order-book-level-2) request to get the snapshot data of Level 2 order book.
  3. Playback the cached Level 2 data flow.
  4. Apply the new Level 2 data flow to the local snapshot to ensure that the sequence of the new Level 2 update lines up with the sequence of the previous Level 2 data. Discard all the message prior to that sequence, and then playback the change to snapshot.
  5. Update the Level 2 full data based on the sequence according to the size. If the size equals to 0, you can update the sequence and remove the price of which the size is 0 out of Level 2. For other cases, please update the price and size.
  6. If the sequence of the newly pushed message does not line up to the sequence of the last message, you could pull through REST Level 2 request to get the updated messages. Please note that the difference between the start and end parameters cannot exceed 500.

The change property of Level 2 updates is a string value of "price,size,sequence". Please note that size is the updated size at that price Level. A size of "0" indicates that the price Level can be removed.

Example

Get the snapshot of the order book through REST request Level 2 to build a local order book. Suppose we get the data as following:

Sequence:16

{
  "sequence": 16,
  "asks": [
    ["3988.59", 3],
    ["3988.60", 47],
    ["3988.61", 32],
    ["3988.62", 8]
  ],
  "bids": [
    ["3988.51", 56],
    ["3988.50", 15],
    ["3988.49", 100],
    ["3988.48", 10]
  ]
}

Thus, the current order book is as following:

Price Size Side
3988.62 8 Sell 4
3988.61 32 Sell 3
3988.60 47 Sell 2
3988.59 3 Sell 1
3988.51 56 Buy 1
3988.50 15 Buy 2
3988.49 100 Buy 3
3988.48 10 Buy 4

After subscribing you will receive change message as following:

  "data": {
    "sequence": 17,
    "change": "3988.50,buy,44"     //Price, side, quantity
  }
  "data": {
    "sequence": 18,
    "change": "3988.61,sell,0"     //Price, side, quantity
  }

In the beginning, the sequence of the order book is 16. Discard the feed data of sequence that is below or equals to 16, and apply playback the sequence [17,18] to update the snapshot of the order book. Now the sequence of your order book is 18 and your local order book is up-to-date.

Diff:

  • Update size of 3988.50 to 44 (Sequence 17)
  • Remove 3988.61 (Sequence 18)

Now your order book is up-to-date and the final data is as following:

Price Size Side
3988.62 8 Sell 3
3988.60 47 Sell 2
3988.59 3 Sell 1
3988.51 56 Buy 1
3988.50 44 Buy 2
3988.49 100 Buy 3
3988.48 10 Buy 4