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.02k stars 420 forks source link

how to establish websocket connection #1371

Open MCaglarGuldiken opened 2 months ago

MCaglarGuldiken commented 2 months ago

Hi,

I would like to establish websocket connection but I am not able to get the values.

Should I made any extra API settings to manage it?

var binanceSocketClient = new BinanceSocketClient(options => {

                options.ApiCredentials = new CryptoExchange.Net.Authentication.ApiCredentials(ConfigurationManager.AppSettings["keyBinance"], ConfigurationManager.AppSettings["secretBinance"]);
            });

            var tickerSubscriptionResult = binanceSocketClient.SpotApi.ExchangeData.SubscribeToTickerUpdatesAsync("ETHUSDT", (update) =>
            {
                var lastPrice = update.Data.LastPrice;
            });

Binance.Net 9.6.1 CryptoExchange.Net 7.0.0

JKorf commented 2 months ago

Should work, what do you mean with 'not able to get the values'?

MCaglarGuldiken commented 2 months ago

I run BinanceDataCollector for two coins. However they are in different thread, still values are confusing. (coinName gives coinName2's value, etc).

dataCollector = new Thread(() => { while (true) { BinanceDataCollector(coinName, instantTradeData, totalTradeAmount); BinanceDataCollector(coinName2, instantTradeData2, totalTradeAmount2); }
});

public async void BinanceDataCollector(string coin, TradeData instantTradeData, decimal dataAmount) { BinanceSocketClient binanceSocketClient = binance.getBinanceSocketClient();

             // Local variables to store prices for the current coin
             double spotAskPriceLocal = 0;
             double spotBidPriceLocal = 0;
             double futuresAskPriceLocal = 0;
             double futuresBidPriceLocal = 0;

                              var tickerSubscriptionResultSpot = binanceSocketClient.SpotApi.ExchangeData.SubscribeToTickerUpdatesAsync(coin, (update) =>
             {

                 spotAskPriceLocal = Convert.ToDouble(update.Data.BestAskPrice);
                 spotBidPriceLocal = Convert.ToDouble(update.Data.BestBidPrice);
                 log.Error(update.Data);
             });                 var tickerSubscriptionTaskFutures = binanceSocketClient.UsdFuturesApi.SubscribeToOrderBookUpdatesAsync(coin, null, (update) =>
             {
                                     futuresAskPriceLocal = Convert.ToDouble(update.Data.Asks.First().Price);
                 futuresBidPriceLocal = Convert.ToDouble(update.Data.Bids.First().Price);
                 log.Error(update.Data);
             });

             // Wait for both subscriptions to complete
             await Task.WhenAll(tickerSubscriptionResultSpot, tickerSubscriptionTaskFutures);
         }
     }
     catch (Exception ex)
     {
         log.Error("Bot Error --> " + ex);
         rtbSpotFutures.AppendText("Bot Error --> " + ex);
     }

}

masherak commented 2 months ago

I think you need to learn programming as first a little bit. BinanceSocketClient works perfectly.

Steffx115 commented 2 weeks ago

@MCaglarGuldiken Set opt.SpotOptions.ApiCredentials