LUCIT-Systems-and-Development / unicorn-binance-websocket-api

A Python SDK by LUCIT to use the Binance Websocket API`s (com+testnet, com-margin+testnet, com-isolated_margin+testnet, com-futures+testnet, com-coin_futures, us, tr, dex/chain+testnet) in a simple, fast, flexible, robust and fully-featured way.
https://unicorn-binance-websocket-api.docs.lucit.tech/
Other
680 stars 165 forks source link

Working with timestamps #138

Closed DrPaprikaa closed 3 years ago

DrPaprikaa commented 3 years ago

Hi, Thank you for your amazing library !

I have a hard time converting timestamps to valid datetimes and wouldn't mind a bit of help. Here is what I'm doing :

  1. Create a stream for orderbook depth via create_stream() with depth20@100ms
  2. Fetch the lastest data from the stream via oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
  3. Get the timestamp of the lastest data using oldest_stream_data_from_stream_buffer['last_update_id']

This gives me a timestamp like 2046077850. Fair enough, let's convert it to a datetime object : print(datetime.datetime.utcfromtimestamp(2046077850)) results in 2034-11-02 10:57:30, which is nowhere near the current time.

How can I have the correct datetime, with milliseconds ?

Sorry if this not fully related to this library, I can delete my issue as soon as it's resolved if you want.

DrPaprikaa

oliver-zehentleitner commented 3 years ago

Hi! I guess you are working with milliseconds but expect to be working with seconds. Just do /1000 and try again. Best regards, Oliver

DrPaprikaa commented 3 years ago

Hi! I already tried : print(datetime.datetime.utcfromtimestamp(2046077850)) gives 2034-11-02 10:57:30 print(datetime.datetime.utcfromtimestamp(2046077850/1000)) gives 1970-01-24 16:21:17.850000

I'm a bit lost...

jon4hz commented 3 years ago

Take a look at this: https://stackoverflow.com/questions/18724037/datetime-unix-timestamp-contains-milliseconds#18724098

oliver-zehentleitner commented 3 years ago

oh, you are using the ID not the timestamp: oldest_stream_data_from_stream_buffer['last_update_id']

DrPaprikaa commented 3 years ago

Ok thanks my bad, last_update_id is not a timestamp :) I am using the Partial Book Depth Stream which doesn't return any timestamp. But then, how can I know the timestamp of the data I'm receiving ? Trading is about timing, I need to know if the data I'm receiving is delayed !

DrPaprikaa commented 3 years ago

btw, oldest_stream_data_from_stream_buffer is confusing (from my perspective), since the 'oldest' in the name suggests that we retreive the oldest data and not the newest

oliver-zehentleitner commented 3 years ago

i dont work with depth... isnt there an event_time?

To know if the data you receive is delayed you can do 2 different things.

  1. check the size of the stream_buffer
  2. use stream_signals to know the status of the streams

actually its a FIFO stack and we do a pop(0) which should return the oldest entry from the stack....

DrPaprikaa commented 3 years ago

There is an event_time for the Diff. Depth Stream, but it's not what I need and is different than the Partial Book Depth Stream, which only has an lastUpdateId.

Would you mind explaining how the size of the stream_buffer allows to know if the data is delayed ? By how much milliseconds ? I'm plotting the bids & asks in real time and I need to have the correct time labels.

actually its a FIFO stack and we do a pop(0) which should return the oldest entry from the stack

If it's a FIFO, wouldn't a pop(0) get us the newest entry ?

oliver-zehentleitner commented 3 years ago

for that the stream_buffer doesnt help but if its not empty or not almost empty you know there is a delay...

I think a pop() delivers the latest entry, a pop(0) delivers the oldest entrie... anyway. It seems there are faster and better ways to do so. https://docs.python.org/2/tutorial/datastructures.html#using-lists-as-queues

oliver-zehentleitner commented 2 years ago

Released: https://github.com/oliver-zehentleitner/unicorn-binance-websocket-api/releases/tag/1.32.0