Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to establish websocket connection #1371

Open
MCaglarGuldiken opened this issue Apr 26, 2024 · 4 comments
Open

how to establish websocket connection #1371

MCaglarGuldiken opened this issue Apr 26, 2024 · 4 comments

Comments

@MCaglarGuldiken
Copy link

MCaglarGuldiken commented Apr 26, 2024

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
Copy link
Owner

JKorf commented Apr 26, 2024

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

@MCaglarGuldiken
Copy link
Author

MCaglarGuldiken commented Apr 26, 2024

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
Copy link
Sponsor

masherak commented May 1, 2024

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

@Steffx115
Copy link

Steffx115 commented Jun 16, 2024

@MCaglarGuldiken Set opt.SpotOptions.ApiCredentials

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants