bitfinexcom / bitfinex-api-go

BITFINEX Go trading API - Bitcoin, Litecoin, and Ether exchange
https://www.bitfinex.com/
MIT License
309 stars 220 forks source link

Question : how to get orderbook from a websocket Client object? #154

Closed gpmn closed 5 years ago

gpmn commented 5 years ago

https://github.com/bitfinexcom/bitfinex-api-go/blob/286a8310973258e9b33e66475f38697f83e813c0/v2/websocket/client.go#L116

Sorry, but I did not found the clue how to get the order book even if I have set ManageOrderbook to true.

Thank so much!!!

JacobPlaster commented 5 years ago

Hi @gpmn currently the local orderbook is not exposed, it is just used to ensure that the websocket stream is in sync. Bitfinex generates a crc32 checksum of the top 25 trades in its book and passes that down the websocket every few seconds. The manage orderbook flag tells the go lib to keep a local copy of the all the trades coming through the websocket and generates its own crc32 checksum using the same method as Bitfinex. If the crc32 checksum is not equal to the bitfinex crc32 checksum then we are not in sync anymore so we reconnect to the websocket and reset the orderbook.

But saying that I can definitely see the benefits of being able to have quick access the local orderbook so Ill try and put something together. Thanks for the suggestion

gpmn commented 5 years ago

Thank you, it should be a great improvement!

JacobPlaster commented 5 years ago

Just giving you an update on this @gpmn. I've created this pull request (which will be merged hopefully tomorrow) https://github.com/bitfinexcom/bitfinex-api-go/pull/156/files which exposes the order book to the consumer.

You can use it like this:

// Load the latest orderbook
ob, _ := wsClient.GetOrderbook(bitfinex.TradingPrefix+bitfinex.BTCUSD)
if ob != nil {
    log.Printf("Orderbook asks: %s", ob.Asks())
    log.Printf("Orderbook asks: %s", ob.Bids())
}