public BinanceExchangeUtil( BinanceHttpClient binanceHttpClient ) { _binanceHttpClient = binanceHttpClient; // get info // TODO: handle exception _exchangeInfo = _binanceHttpClient.ExchangeInformationAsync().GetAwaiter().GetResult(); }
public async Task StartAsync(CancellationToken cancellationToken) { _logger.LogInformation("Starting..."); var isAvailable = await _binanceHttpClient.TestConnectivityAsync(); if (!isAvailable) { throw new Exception($"Binance isn't available!"); } var exchangeInfo = await _binanceHttpClient.ExchangeInformationAsync(); exchangeInfo.Symbols = exchangeInfo.Symbols.Where(x => x.Status == "TRADING").ToList(); int depth = 20; // 5, 10 ,20 var bookStreams = CirclePahtAlgorithmConfig.AllowedSymbols.Select(symbol => $"{symbol.ToLowerInvariant()}@depth{depth}").ToList(); // use separate handler instance for each stream var wssStreamMessageHandleManager = new WssStreamMessageHandleManager(); foreach (var stream in bookStreams) { wssStreamMessageHandleManager.RegisterStreamHandler(stream, _serviceProvider.GetRequiredService <WssBookDepthHandler>()); } _binanceWssClient.SetWssStreamMessageHandleManager(wssStreamMessageHandleManager); await _binanceWssClient.ConnectAsync(); //// subscribe // Partial Book Depth Streams // Stream Names: <symbol>@depth<levels> OR <symbol>@depth<levels>@100ms await Task.WhenAll(bookStreams.Select(stream => _binanceWssClient.SubscribeToStreamAsync(stream))); // save order book to file periodically const string orderBookSaveDirPath = "./data-logs/binance/order-book-store"; this._SetInterval(async() => { Directory.CreateDirectory(orderBookSaveDirPath); string fileName = $"{DateTime.UtcNow.ToString("yyyyMMdd'T'HHmmss'.'fff'Z'")}.log"; var orderBookStore = _serviceProvider.GetService <OrderBookStore>(); string contents = orderBookStore.SerializeToJson(Formatting.None); File.WriteAllText(Path.Combine(orderBookSaveDirPath, fileName), contents); _logger.LogInformation("Saved order book on disk."); }, TimeSpan.FromSeconds(10), cancellationToken); // }