-
Notifications
You must be signed in to change notification settings - Fork 217
Binance Orderbook Streaming implementation doesn't take the initial snapshot #86
Comments
In current code I think it is possible to do it in a way:
|
I've noticed the same exact problem and I am currently working on implementing the Binance recommended initialization. I.E subscribing to the orderbook update events first, buffering them and then removing anything older than the snapshot from the buffer before processing events in the buffer. I got it to a somewhat functional state, but not ready to be contributed back.See: There are still a few things I'm trying to work out, so if anyone can give me a hand it would be greatly appreciated: In "BinanceStreamingMarketDataService.java" I was trying to skip one orderbook event before making the call to "InitializeOrderBook", but I couldn't make it work, so till I figure it out , I've added a 3 second sleep inside "InitializeOrderBook":
|
FYI, I have discovered that the current implementation of the Binance streaming order book depth updates creates an inconsistent orderbook over time. https://github.com/traviscollins/xchange-stream-tester Build the above project, and run the "BinanceStreamTesterMain". Then let it sit for a day or two. You'll see messages saying "Ask of X is lower than highest bid of Y". It's a simple evaluation of whether a single orderbook contains an ask thats lower than it's highest bid (which should never happen, because that would be a negative spread). |
+1 |
I think the problem traviscollins discovered is on binance end. If you try to listen for depth stream of a low volume trading symbol, you would find out the data is not been pushed every second, and each new event's U is not guaranteed equal to the previous event's u+1. |
interesting thanks for this, I will have to manage my own orderbook |
I've modified DepthCacheExample from https://github.com/binance-exchange/binance-java-api to do the same check as @traviscollins does in his tester and I get errors there too: |
We moved to a completely custom solution for Binance using a binary sort update of a simple list and found that Binance’s stream is sane. Our custom implementation works. Something about this Xchange-stream implementation does not.
… On Jun 24, 2018, at 9:38 AM, David Jirovec ***@***.***> wrote:
I've modified DepthCacheExample from https://github.com/binance-exchange/binance-java-api to do the same check as @traviscollins does in his tester and I get errors there too:
Ask of 0.01311900 is lower than highest bid of 0.01312300
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
That's good to know. But still I am surprised from the result of my experiment, because it does not contain xchange-stream at all. https://github.com/binance-exchange/binance-java-api seems to be reference implementation of java client for binance api. |
@traviscollins Can you please elaborate on how your code works? How do you handle receiving events out of order? Do you also see that a "new event's U is not guaranteed equal to the previous event's u+1"? |
Not positive if this is the best place, but as per @Arshaku instructions, I've had a play with RxJava operators to produce a solution that follow the recommendations. I used the Binance Java API to gather the data, but the logic of manipulating the data should carry across. I've outlined it partially on this gist. The logic of the code gets the snapshot of the orderbook, after the updates begin to queue and buffers the updates such that various validation checks can be made. |
Hello! |
Here's my first stab at the problem properly. A few things I'm uncertain about I've only just started testing, but it implements all the bits, and makes an attempt at what exactly to do when the stream gets out of sync: Thoughts welcome. |
Positive results so far. My trick of re-syncing any time we get an out-of sequence update does indeed seem to work:
|
Also survived a socket reconnect:
|
Will continue to test this in the coming days. If all goes well I'll submit a pull request as soon as we have progress on #224. I doubt this will even work on |
Binance API documentation gives instructions how to manage local orderbook correctly:
https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md
How to manage a local order book correctly
u
is <=lastUpdateId
in the snapshotU
<=lastUpdateId
+1 ANDu
>=lastUpdateId
+1U
should be equal to the previous event'su
+1As of xchange-stream 4.3.1 it doesn't follow the recommendation by Binance API Docs. As you can see the recommendation is to get the snapshot of full orderbook on 3rd step, and then apply updates on it. The current implementation doesn't take the snapshot. As a result the local orderbook doesn't reflect the reality of Binance orderbook on serverside (though it slowly gets closer over time).
The text was updated successfully, but these errors were encountered: