JKorf / Bitfinex.Net

A C# .netstandard client library for the Bitfinex REST and Websocket API focusing on clear usage and models
https://jkorf.github.io/Bitfinex.Net/
MIT License
88 stars 50 forks source link

BitfinexSymbolOrderBook - Asks go out of sync with platform #107

Closed bogdanyel closed 4 years ago

bogdanyel commented 4 years ago

First of all thumbs up for the great work. It really helps a lot.

I do want to report an issue I encountered when working with the symbol OrderBook with the Websocket Client: I am using the latest version of Bitfinex.Net and getting the OrderBook via the BitfinexSymbolOrderBook implementation. Everything seems to work perfect when starting, but after a few minutes (~2-3 min) the Asks go out of sync with what I see on the trading platform while the Bids are still in sync.

Another small issue with the OrderBook (using the BitfinexSymbolOrderBook) is that even though you select a limit of 25 for example, the Asks and Bids arrays are of different lengths from each other and also from the 25 selected.

Would you have some time to take a look at it?

JKorf commented 4 years ago

Hi, I've been running it twice for +- half an hour now and it seems fine for me; staying at 25 bids/asks and in sync with the website. Could you maybe enable debug logging and check what that says when this happens?

bogdanyel commented 4 years ago

Hello Jan,

You are actually correct that it's running in sync with the platform now. Debug info also doesn't show anything wrong. Strange I saw something else for a few days... But if it works it works.

There is a mismatch though between bids and asks count in the event handler method:

Asks count: 25; Bids count: 25 2020/01/23 19:17:24:501 | Debug | Socket 2 received data: [336466,[10.429,0,1]] Asks count: 25; Bids count: 24 2020/01/23 19:17:24:504 | Debug | Socket 2 received data: [336466,[10.459,0,-1]] Asks count: 24; Bids count: 24 2020/01/23 19:17:24:507 | Debug | Socket 2 received data: [336466,[10.46,0,-1]] Asks count: 23; Bids count: 24 2020/01/23 19:17:24:509 | Debug | Socket 2 received data: [336466,[10.37,3,78.60240334]] Asks count: 23; Bids count: 25 2020/01/23 19:17:24:512 | Debug | Socket 2 received data: [336466,[10.456,1,-44.74197883]] Asks count: 24; Bids count: 25

The count mismatch doesn't bother me that much.

I will close the issue. Thank you very much for taking the time.

bogdanyel commented 4 years ago

Hi Jan,

It happened again. I attached a screen shoot comparing the two order books (bids = in sync; asks = out of sync). The upper part of the screenshoot is an own implementation using Bitfinex.Net and the lower part is the actual Bitfinex order book. I think an old ask is not removed and stays in the list. It is possible that this is only visible if the price movement has a certain direction.

Screenshoot

It happens quite often but I cannot put the finger on the problem.

JKorf commented 4 years ago

I thought of something, but I'm not sure it's the actual problem. Bitfinex sends the bids/asks as a big list. It seperates bids/asks by setting the quantity to a negative number if it is an ask, and a positive number if it's a bid. In my implementation I check to see if the number is positive or negative and only adjust the bid/ask list based on what type it is.

However, if Bitfinex sends an update for a price switch from a bid to an ask in a single update that won't work. For example; the order book contains a bid entry for BTCUSD with Quantity = 1 and Price = 10000 If an update is received where Quantity = -1 and Price = 10000 it will be added to the ask list, but not removed from the bid list.

Now I would expect the Bitfinex server to also send a message to remove the price from the bid list, but if it doesn't that would cause problems, so I'm monitoring that to see if it happens.

If you find any other information regarding this issue please let me know.

FlashPlayer13 commented 4 years ago

Hi, i think i have an update for this problem. I logged few days in debug mode and finally catch same or similar problem: bid becomes more than ask. I will paste a part of log here, that clearly shows problem:

2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9033.5,0,1]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9031,0,1]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9030.1,0,1]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9023.9,0,1]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9022.4,0,1]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9036.4,0,-1]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9034.4,1,0.0878857]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9033.8,1,1]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9033.6,1,-0.05]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9031.1,2,0.65358673]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9030.4,1,0.2]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9023.8,3,1.2525]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9022.2,2,1.1977]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9034.5,5,-4.88206193]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9040.2,2,-0.77477942]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9040.9,1,-0.85]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9042.4,1,-0.0008]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [7,"tu",[412495852,1580195912669,-0.1321143,9034.4]] 2020.01.28 10:18:32:781 | Debug | Socket 1 received data: [560884,[9030.4,0,1]] 2020.01.28 10:18:32:781 | Debug | Socket 1 received data: [560884,[9023.3,0,1]] 2020.01.28 10:18:32:781 | Debug | Socket 1 received data: [560884,[9036.5,0,-1]] 2020.01.28 10:18:32:781 | Debug | Socket 1 received data: [560884,[9030.5,1,0.2]] 2020.01.28 10:18:32:781 | Debug | Socket 1 received data: [560884,[9022,1,0.0663]] 2020.01.28 10:18:32:781 | Debug | Socket 1 received data: [560884,[9034.5,4,-3.3662898]] 2020.01.28 10:18:32:781 | Debug | Socket 1 received data: [560884,[9040.2,1,-0.0008]] 2020.01.28 10:18:32:781 | Debug | Socket 1 received data: [560884,[9040.4,1,-0.77397942]]

Here you can see that in this lines i got bid > ask:

2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9034.4,1,0.0878857]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9033.8,1,1]] 2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [560884,[9033.6,1,-0.05]]

Bid = 9034.4 and Ask = 9033.6. And it will not change in few next rows. But maybe this row:

2020.01.28 10:18:32:640 | Debug | Socket 1 received data: [7,"tu",[412495852,1580195912669,-0.1321143,9034.4]]

means something we need to take a note?

FlashPlayer13 commented 4 years ago

Any ideas why bid may become more than ask? I got few repeats of this situation.

gx-7 commented 4 years ago

I have the same issue with Bitfinex and Kraken, fortunatly Binance is working like a charm. cf. JKorf/CryptoExchange.Net issues 34

Marfusios commented 4 years ago

Bitfinex offers checksum for order book data, so you can compute validity at any time.

I was also struggling with all this stuff around order book, it is pretty challenging in the case of Bitfinex, Bitmex (etc) because they are sending a lot of data. An efficient data structure is a must, also some trick with busy/wait do its work. I've made it public, check it out: https://github.com/Marfusios/crypto-websocket-extensions

MatheusGrijo commented 4 years ago

Hello people! The code really is fantastic !!!!!

I'm going through the same problems = ( Both in bitfinex and kraken = ( Any idea how to solve this?

JKorf commented 4 years ago

Hi everyone, I think I've found and fixed the issue. Can you try the latest version and see if it's all good now?

For anyone interested; I've fixed the possibility of multiple threads waiting for a lock to update the order book. This can cause the wrong thread to go first as waiting for a look isn't necessarily FIFO. I've added a queue for the updates which fixes this issue.

bogdanyel commented 4 years ago

Hi. I updated to the last version and now both the bids and the asks remain in sync perfectly. It seems that the problem was fixed. Thanks for your help Jan and happy trading everyone!