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
685 stars 165 forks source link

Sorting userdata stream by E #135

Closed tranceporter closed 3 years ago

tranceporter commented 3 years ago

Hi,

Thanks for this amazing codebase. Works very well :) I am curious to know if we are already sorting the userdata stream by E? I merely ask because Binance docs suggest this:

User data stream payloads are not guaranteed to be in order during heavy periods; make sure to order your updates using E

https://binance-docs.github.io/apidocs/futures/en/#user-data-streams

I am trying to implement copy trading functionality and I have it mostly working, but when stop market order is executed (stop hit), I seem to get "stop market expired" followed by "new order in opposite direction of the trade" (so I had a long position open, thne a new order for sell is created). While this seems to work fine on the master account, when it gets copied to slave account, it actually executes the stop (so long position is stopped out), followed by copying of the new SELL order, thus opening a short position :)

Maybe it's an issue in my code, but just wanted to chekc if we are already sorting by the timestamp value in E out of the box :) If not, is there any clean way of doing this once I subscribe to the stream?

Thanks, Shreyas

oliver-zehentleitner commented 3 years ago

hi!

thank you!

no, the lib does not do any sorting! it receives a data record and puts it on the stream_buffer FIFO stack. With pop_stream_data_from_stream_buffer() you get the oldest entry from the stack or if empty False. But oldest means its from all items in the stack the longest remaining one!

As the documentation says, they cant guarantee it, so you have to collect and then sort. Do you store in a database? if you work with the saved data, you can do SELECT with order by E for example...

Best regards, Oliver

tranceporter commented 3 years ago

Yeah looks like the sensible way to do is to dump into a relational database and then sort it, and then process it for copy trade. That will introduce more latency unfortunately. Currently the copy trade relocated in a split second. So super fast. Need to have a bit more think :)

On Fri, 4 Dec 2020 at 13:16, Oliver Zehentleitner notifications@github.com wrote:

hi!

thank you!

no, the lib does not do any sorting! it receives a data record and puts it on the stream_buffer FIFO stack. With pop_stream_data_from_stream_buffer() you get the oldest entry from the stack or if empty False. But oldest means its from all items in the stack the longest remaining one!

As the documentation says, they can guarantee it, so you have to collect and then sort. Do you store in a database? if you work with the saved data, you can do SELECT with order by E for example...

Best regards, Oliver

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/oliver-zehentleitner/unicorn-binance-websocket-api/issues/135#issuecomment-738778317, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALAJ5GKXDZAZO2CTMXH36TSTDOLJANCNFSM4UNNHEUQ .

oliver-zehentleitner commented 3 years ago

then create a stack with pandas and sort it after every entry...

tranceporter commented 3 years ago

Haha yeah. Not very sustainable IMO. Especially if the master account is a scalp account, then I will have to make the E time stamp as primary key to sort it automatically in the table. Will try it out as is to see how far I can stretch it. Else switch to using sql express or similar databases

On Fri, 4 Dec 2020 at 13:21, Oliver Zehentleitner notifications@github.com wrote:

then create a stack with pandas and sort it after every entry...

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/oliver-zehentleitner/unicorn-binance-websocket-api/issues/135#issuecomment-738780517, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALAJ5AR3XYPWXIEL5UCUITSTDO6PANCNFSM4UNNHEUQ .

oliver-zehentleitner commented 3 years ago

Since the initial question (sorting) is answered i hope its ok to close the issue!

tranceporter commented 3 years ago

Yes no problem. Thank you :)

On Fri, 4 Dec 2020 at 21:40, Oliver Zehentleitner notifications@github.com wrote:

Since the initial question (sorting) is answered i hope its ok to close the issue!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/oliver-zehentleitner/unicorn-binance-websocket-api/issues/135#issuecomment-739035813, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALAJ5EXSN43Y7CQAYCOMULSTFJNTANCNFSM4UNNHEUQ .