JKorf / Binance.Net

A C# .netstandard client library for the Binance REST and Websocket Spot and Futures API focusing on clear usage and models
https://jkorf.github.io/Binance.Net/
MIT License
1.03k stars 425 forks source link

No data from websocket for user stream? tested by creating/cancelling isolated margin trades #609

Closed SpaceMonkeyForever closed 3 years ago

SpaceMonkeyForever commented 3 years ago

Why isn't there a way to subscribe to margin order updates like we have for spot?

We have Margin.UserStream.StartUserStream() which btw had a bug that I fixed https://github.com/JKorf/Binance.Net/pull/608 but we don't have a corresponding Margin.Subscribe method. I believe I can use Spot.Subscribe because it makes the same request to the same endpoint and listens to the correct WebSocket (looking at https://github.com/binance/binance-spot-api-docs/blob/master/user-data-stream.md), but I don't get any data:


            var result = _client.Margin.UserStream.StartUserStream();
            var userStreamKey = result.Data;

            var client = new BinanceSocketClient();
            var subscription = client.Spot.SubscribeToUserDataUpdates(userStreamKey, 
                info =>
            {
                _log.LogInformation(info.ToString());
            }, orderUpdate =>
            {
                _log.LogInformation($"{orderUpdate.Quantity} {orderUpdate.Symbol} {orderUpdate.Side} @ {orderUpdate.Price}. Fee = {orderUpdate.Commission} {orderUpdate.CommissionAsset}");
            }, x =>
                {
                    _log.LogInformation(x.ToString());
                }, x =>
                {
                    _log.LogInformation(x.ToString());
                },
                x =>
                {
                    _log.LogInformation(x.ToString());
                });
            var updateSubscription = subscription.Data;

Logs suggests websocket is connected:

2021-02-19 23:45:41.9977|Debug| 2021/02/19 23:45:41:989 | Binance    | Debug | Client configuration: LogVerbosity: Debug, Writers: 1, Credentials: Set, BaseAddress: https://api.binance.com/, Proxy: -, RateLimiters: 0, RateLimitBehaviour: Wait, RequestTimeout: 00:00:30 |EventType=Resume 
2021-02-19 23:45:42.0624|Debug| 2021/02/19 23:45:42:062 | Binance    | Debug | [1] Creating request for https://api.binance.com/api/v3/time |EventType=Resume 
2021-02-19 23:45:42.0624|Debug| 2021/02/19 23:45:42:072 | Binance    | Debug | [1] Sending GET request to https://api.binance.com/api/v3/time  |EventType=Resume 
2021-02-19 23:45:42.5021|Debug| 2021/02/19 23:45:42:502 | Binance    | Debug | [1] Response received in 419ms: {"serverTime":1613778342536} |EventType=Resume 
2021-02-19 23:45:42.6272|Debug| 2021/02/19 23:45:42:627 | Binance    | Debug | [2] Creating request for https://api.binance.com/api/v3/time |EventType=Resume 
2021-02-19 23:45:42.6272|Debug| 2021/02/19 23:45:42:627 | Binance    | Debug | [2] Sending GET request to https://api.binance.com/api/v3/time  |EventType=Resume 
2021-02-19 23:45:42.8485|Debug| 2021/02/19 23:45:42:848 | Binance    | Debug | [2] Response received in 219ms: {"serverTime":1613778342893} |EventType=Resume 
2021-02-19 23:45:42.8485|Debug| 2021/02/19 23:45:42:849 | Binance    | Info | Time offset between 0 and 500ms (265.815ms), no adjustment needed |EventType=Resume 
2021-02-19 23:45:42.8485|Debug| 2021/02/19 23:45:42:850 | Binance    | Debug | [3] Creating request for https://api.binance.com/api/v3/userDataStream |EventType=Resume 
2021-02-19 23:45:42.8485|Debug| 2021/02/19 23:45:42:851 | Binance    | Debug | [3] Sending POST request to https://api.binance.com/api/v3/userDataStream with request body  |EventType=Resume 
2021-02-19 23:45:43.0770|Debug| 2021/02/19 23:45:43:077 | Binance    | Debug | [3] Response received in 224ms: {"listenKey":"<REMOVED>"} |EventType=Resume 
2021-02-19 23:45:43.0860|Debug| 2021/02/19 23:45:43:086 | Binance    | Debug | Client configuration: LogVerbosity: Debug, Writers: 1, Credentials: -, BaseAddress: wss://stream.binance.com:9443/, Proxy: -, AutoReconnect: True, ReconnectInterval: 00:00:05, SocketResponseTimeout: 00:00:10, SocketSubscriptionsCombineTarget:  |EventType=Resume 
2021-02-19 23:45:43.0987|Debug| 2021/02/19 23:45:43:098 | Binance    | Debug | Created new socket for wss://stream.binance.com:9443/ws/<REMOVED>|EventType=Resume 
2021-02-19 23:45:43.1182|Debug| 2021/02/19 23:45:43:118 | Binance    | Debug | Socket 1 connecting |EventType=Resume 
2021-02-19 23:45:44.1281|Debug| 2021/02/19 23:45:44:128 | Binance    | Debug | Socket 1 connected |EventType=Resume 

I can see in debugger that the client thinks it's connected:

image

SpaceMonkeyForever commented 3 years ago

It looks like my PR might be wrong. The key to listen to margin orders might have a different endpoint (hard to decipher Binance API, it mentions only one endpoint for all user data). If I revert my PR, I get the following error:

Not a margin account.

Which seems to make sense because on Binance I don't have a cross margin account, only an isolated margin account and the API key I'm using is marked as follows:

image

I wonder if Binance API is really that bad. Maybe I have to fund my cross margin account first then enable this flag then try again.

On the other hand, I believe the current API end-point (without my PR) is deprecated.... and I'm not sure if my PR is using the correct end-point or not.

SpaceMonkeyForever commented 3 years ago

After depositing money into my margin wallet and reverting my PR, I can receive data but only for cross margin orders.

So essentially, the current code works fine but it's only for cross margin data but not for isolated margin data. My PR is wrong and I will cancel it until I find the correct fix (if it exists) to use v3 api for cross margin as well as being able to listen to cross margin orders.

SpaceMonkeyForever commented 3 years ago

I found the documentation:

image

While implementing the isolated stream, I found out it's already implemented.....

This is the correct code:

var result= _client.Margin.IsolatedUserStream.StartIsolatedMarginUserStream("BTCUSDT");