Kucoin / kucoin-python-sdk

MIT License
40 stars 11 forks source link

Building local orderbook #68

Open thephilippk opened 2 years ago

thephilippk commented 2 years ago

I am able to get a Rest-API snapshot of an orderbook. I built the logic to update the orderbook-snapshot from the Rest-API with updates from the Websocket-API. But I really struggle with the calibration procedure. How can I combine the continuous data stream, from the right msg onwards, with the snapshot? I did not succeed in feeding both the msg and the orderbook in my function "update orderbook".

Thx a lot

progressivehed commented 2 years ago

Calibration procedure:

After receiving the websocket Level 2 data flow, cache the data. Initiate a REST request to get the snapshot data of Level 2 order book. Playback the cached Level 2 data flow. Apply the new Level 2 data flow to the local snapshot to ensure that the sequence of the new Level 2 update lines up with the sequence of the previous Level 2 data. Discard all the message prior to that sequence, and then playback the change to snapshot. Update the level2 full data based on sequence according to the price and size. If the price is 0, ignore the messages and update the sequence. If the size=0, update the sequence and remove the price of which the size is 0 out of level 2. For other cases, please update the price.

progressivehed commented 2 years ago

I will post an example code for python in Learning center soon.