verata-veritatis / pybit

Python3 API connector for Bybit's HTTP and Websockets APIs.
MIT License
117 stars 193 forks source link

Websocket Orderbook not Properly Ordered #85

Open SebCoding opened 2 years ago

SebCoding commented 2 years ago

When using websockets to get the content of the orderbook (orderBookL2_25.Symbol) the content of the orderbook obtained when calling fetch() is out of order. When Bybitsends the orderbook the first time on the websocket it is ordered properly and then on subsequent delta pushes, pybit updates the local version of the orderbook, but does not re-sort it after delta updates. So when we call fetch() to get the orderbook we might assume the orderbook is sorted, but that is not the case and reading only the top rows for buyers/sellers sometime gives inaccurate results. Here is an example of what fetch() returns after a few delta updates. For example:

Orderbook Top 25
       price   symbol         id  side    size
0   41426.00  BTCUSDT  414260000   Buy  11.807
1   41426.50  BTCUSDT  414265000   Buy   0.049
2   41428.00  BTCUSDT  414280000   Buy   0.003
3   41429.50  BTCUSDT  414295000   Buy   0.027
4   41430.00  BTCUSDT  414300000   Buy   0.505
5   41430.50  BTCUSDT  414305000   Buy   0.404
6   41432.00  BTCUSDT  414320000   Buy   0.012
7   41432.50  BTCUSDT  414325000   Buy   0.540
8   41433.50  BTCUSDT  414335000   Buy   1.052
9   41434.50  BTCUSDT  414345000   Buy   6.034
10  41435.00  BTCUSDT  414350000   Buy   1.110
11  41435.50  BTCUSDT  414355000   Buy   2.517
12  41436.00  BTCUSDT  414360000   Buy   0.774
13  41436.50  BTCUSDT  414365000   Buy   1.093
14  41437.00  BTCUSDT  414370000   Buy   1.283
15  41437.50  BTCUSDT  414375000   Buy   1.052
16  41438.50  BTCUSDT  414385000   Buy   0.616
17  41439.00  BTCUSDT  414390000   Buy   0.034
18  41439.50  BTCUSDT  414395000   Buy   2.354
19  41440.00  BTCUSDT  414400000   Buy   0.854
20  41441.00  BTCUSDT  414410000   Buy   3.300
21  41441.50  BTCUSDT  414415000   Buy  78.825
22  41442.00  BTCUSDT  414420000  Sell   7.434
23  41444.50  BTCUSDT  414445000  Sell   0.081
24  41447.50  BTCUSDT  414475000  Sell  16.000
25  41449.00  BTCUSDT  414490000  Sell   0.744
26  41450.00  BTCUSDT  414500000  Sell   1.097
27  41450.50  BTCUSDT  414505000  Sell   0.035
28  41451.00  BTCUSDT  414510000  Sell   0.677
29  41451.50  BTCUSDT  414515000  Sell   0.331
30  41452.00  BTCUSDT  414520000  Sell   1.350
31  41453.00  BTCUSDT  414530000  Sell   5.000
32  41455.50  BTCUSDT  414555000  Sell   0.679
33  41456.50  BTCUSDT  414565000  Sell   0.020
34  41458.00  BTCUSDT  414580000  Sell   0.837
35  41458.50  BTCUSDT  414585000  Sell   0.334
36  41459.00  BTCUSDT  414590000  Sell   0.003
37  41459.50  BTCUSDT  414595000  Sell   1.687
38  41460.00  BTCUSDT  414600000  Sell   0.641
39  41460.50  BTCUSDT  414605000  Sell   1.337
40  41462.00  BTCUSDT  414620000  Sell   4.463
41  41462.50  BTCUSDT  414625000  Sell   1.948
42  41438.00  BTCUSDT  414380000   Buy   0.719  <--
43  41452.50  BTCUSDT  414525000  Sell   0.570
44  41425.50  BTCUSDT  414255000   Buy   0.050 <--
45  41449.50  BTCUSDT  414495000  Sell   0.777
46  41446.00  BTCUSDT  414460000  Sell   0.010
47  41454.50  BTCUSDT  414545000  Sell   0.007
48  41425.00  BTCUSDT  414250000   Buy   0.362 <--
49  41463.00  BTCUSDT  414630000  Sell   0.010

Maybe pybitshould re-sort the local orderbook in _on_message() after each delta updates, or prior to returning the orderbook to the user in the fetch() method and if not, at least tell the user he needs to re-sort the orderbook himself prior to using it.