Raw Books

The Raw Books channel provides the most granular books.

const ws = require('ws')
const w = new ws('wss://api-pub.bitfinex.com/ws/2')

w.on('message', (msg) => console.log(msg))

let msg = JSON.stringify({ 
  event: 'subscribe', 
  channel: 'book',
  prec: 'R0',
  symbol: 'tBTCUSD' 
})

w.on('open', () => w.send(msg))
wscat -c wss://api-pub.bitfinex.com/ws/2
{ "event": "subscribe", "channel": "book", "prec": "R0", "symbol": "tBTCUSD" }
ws = websocket.WebSocketApp('wss://api-pub.bitfinex.com/ws/2')

ws.on_open = lambda self: self.send('{ "event": "subscribe", "channel": "book", "prec": "R0", "symbol": "tBTCUSD" }')

ws.on_message = lambda self, evt:  print (evt)

📘

Note

PRICE = 0 means that you have to remove the order from your book.

// request
{ 
  event: 'subscribe',
  channel: 'book',
  symbol: SYMBOL,
  prec: PRECISION,
  freq: FREQUENCY,
  len: LENGTH,
	subId: SUBID
}

{"event":"subscribe","channel":"book","symbol":"tBTCUSD","prec":"R0","freq":"F0","len":"25","subId": 123}
// response
{ 
  event: 'subscribed',
  channel: 'book',
  chanId: CHANNEL_ID,
  symbol: SYMBOL,
  prec: PRECISION,
  freq: FREQUENCY,
  len: LENGTH,
	subId: SUBID,
	pair: PAIR 
}

{"event":"subscribed","channel":"book","chanId":10092,"symbol":"tETHUSD","prec":"R0","freq":"F0","len":"25","subId":123,"pair":"ETHUSD"}
// on trading pairs (ex. tBTCUSD)
[
  CHANNEL_ID,
  [
    [
      ORDER_ID,
      PRICE,
      AMOUNT
    ],
    ...
  ]
]

[433290,[[34753002978,7294.7,1.54340371]]]  
  
// on funding currencies (ex. fUSD)
[
  CHANNEL_ID,
  [
    [
      OFFER_ID,
      PERIOD,
      RATE,
      AMOUNT
    ],
    ...
  ]
]

[472778,[[658282397,30,0.000233,-530]]]
// on trading pairs (ex. tBTCUSD)
[
  CHANNEL_ID,
  [
    ORDER_ID,
    PRICE,
    AMOUNT
  ]
]

[433290,[34753006045,0,-1]]
  
// on funding currencies (ex. fUSD)
[
  CHANNEL_ID,
  [
    OFFER_ID,
    PERIOD,
    RATE,
    AMOUNT
  ]
]

[472778,[658286906,2,0,1]]
//Bulk updates can be enabled using a conf event flag of 536870912

// on trading pairs (ex. tBTCUSD)
[
  CHANNEL_ID,
  [
    [
      ORDER_ID,
      PRICE,
      AMOUNT
    ],
    [
      ORDER_ID,
      PRICE,
      AMOUNT
    ],
    ...
  ]
]

[433290,[[34753006045,0,-1],[34753006035,0,-1]]]
  
// on funding currencies (ex. fUSD)
[
  CHANNEL_ID,
  [
    [
      OFFER_ID,
      PERIOD,
      RATE,
      AMOUNT
    ],
    [
      OFFER_ID,
      PERIOD,
      RATE,
      AMOUNT
    ]
    ...
  ]
]

[472778,[[658286906,2,0,1],[65826109,3,0,1]]]

Request Fields

FieldsTypeDescription
SYMBOLStringTrading pair or funding currency
PRECstringLevel of price aggregation (R0 for Raw Books).
LENstringNumber of price points ("1", "25", "100", "250") [default="25"]
SUBIDstringOptional user-defined ID for the subscription

Stream Fields

FieldsTypeDescription
CHANNEL_IDintIdentification number assigned to the channel for the duration of this connection.
ORDER_IDintIdentification number of the order.
OFFER_IDintIdentification number of the funding offer.
PRICEfloatOrder price; if 0 you have to remove the order from your book
PERIODintFunding period in days
RATEfloatFunding rate for the offer; if 0 you have to remove the offer from your book
±AMOUNTfloatAmount of order

Trading: if AMOUNT > 0 then bid else ask; Funding: if AMOUNT < 0 then bid else ask;

Algorithm to create and keep a book instance updated

  1. subscribe to channel with R0 precision
  2. receive the raw book snapshot and create your in-memory book structure
  3. when PRICE > 0 then you have to add or update the order
  4. when PRICE = 0 then you have to delete the order

Note: The algorithm for raw books using R0 is based on the ORDER ID instead of COUNT, PRICE and AMOUNT.
If LENGTH is set to 25, you will get 25 orders (not price levels). If all orders belong to the same price level, your orderbook will just have that one price level.

Checksum
An Order Book Checksum can be requested through the Websocket configuration. You can find out more on the WebSocket Checksum page.

Bulk Updates
Bulk updates can be enabled by sending a conf event with a flag value of 536870912. With bulk updates enabled, updates will arrive as an array of arrays.