Search code examples
c#apiwebsocketbinancebinance-api-client

C# Binance API do not return all symbols when get "All Market Tickers Streams"


I'm using Websocket.Client to get All Market Tickers Streams (document here).

But I can't figure out why each time the API only returns a different number of symbols.

The result format:

             {
              "e": "24hrTicker",  // Event type
              "E": 123456789,     // Event time
              "s": "BNBBTC",      // Symbol
              "p": "0.0015",      // Price change
              "P": "250.00",      // Price change percent
              "w": "0.0018",      // Weighted average price
              "x": "0.0009",      // First trade(F)-1 price (first trade before the 24hr rolling window)
              "c": "0.0025",      // Last price
              "Q": "10",          // Last quantity
              "b": "0.0024",      // Best bid price
              "B": "10",          // Best bid quantity
              "a": "0.0026",      // Best ask price
              "A": "100",         // Best ask quantity
              "o": "0.0010",      // Open price
              "h": "0.0025",      // High price
              "l": "0.0010",      // Low price
              "v": "10000",       // Total traded base asset volume
              "q": "18",          // Total traded quote asset volume
              "O": 0,             // Statistics open time
              "C": 86400000,      // Statistics close time
              "F": 0,             // First trade ID
              "L": 18150,         // Last trade Id
              "n": 18151          // Total number of trades
            }

So, I have a MarketTicker class

    public class MarketTicker
    {
        public string e { get; set; }
        public long E { get; set; }
        public string s { get; set; }
        public string p { get; set; }
        public string P { get; set; }
        public string w { get; set; }
        public string x { get; set; }
        public string c { get; set; }
        public string Q { get; set; }
        public string b { get; set; }
        public string B { get; set; }
        public string a { get; set; }
        public string A { get; set; }
        public string o { get; set; }
        public string h { get; set; }
        public string l { get; set; }
        public string v { get; set; }
        public string q { get; set; }
        public long O { get; set; }
        public long C { get; set; }
        public int F { get; set; }
        public int L { get; set; }
        public int n { get; set; }
    }

1. Init

var url = new Uri("wss://fstream.binance.com/ws/!ticker@arr");
var client = new WebsocketClient(url);
client.MessageReceived.Subscribe(OnReceived);
await client.Start();

2. OnReceived event

 private async void OnReceived(ResponseMessage responseMessage)
        {
            var strResponse = responseMessage.ToString();
            var rps = JsonConvert.DeserializeObject<List<MarketTicker>>(strResponse);
            if (rps != null)
            {
                Console.WriteLine($"List<MarketTicker>: {rps.Count}");
            }
        }

The bad results:

>>List<MarketTicker>: 51
>>List<MarketTicker>: 60
>>List<MarketTicker>: 82
>>List<MarketTicker>: 120
>>List<MarketTicker>: 140
>>List<MarketTicker>: 90

The expect result is all symbols in a web socket response message, but almost is only a part of it. I've tried to get it many times but all failed, it just returns any number of it, varying between times.

What I need is all of them.

What's wrong with the Binance API or my code? Can anyone help me in this case? Any positive comments are appreciated.

Thank in advance.


Solution

  • This Stream returns only the tickers whose price has changed in relation to previous data, so if BTCUSDT price doesn't move for 5 minutes you will not receive this ticker in this stream, you shall receive it when it updates to a new value, as I understand it.

    From Binance: Note that only tickers that have changed will be present in the array.