protected override async Task DoWork(PairConfig config, CancellationToken stoppingToken) { _logger.LogTrace($"{Exchange.Description} Candles monitor is started"); while (!stoppingToken.IsCancellationRequested) { try { using (BinanceClient client = new BinanceClient()) { DateTime now = DateTime.UtcNow; IPeriodCode candlePeriod = config.Timeframe.HasValue ? PeriodCode.Create(config.Timeframe.Value) : DefaultCandleInterval; WebCallResult <IEnumerable <BinanceKline> > klines = client.GetKlines(config.Symbol, candlePeriod.ToPeriodCode(), now.AddDays(-30), now, 500); _logger.LogTrace($"Received candles from {Exchange.Description}"); List <Candle> candles = klines.Data.Select(x => x.ToCandle(config.Symbol, candlePeriod)).ToList(); foreach (Candle candle in candles) { await _candleProcessor.Create(candle); } } await Task.Delay(TimeSpan.FromHours(1)); } catch (Exception ex) { _logger.LogError($"{Exchange.Description} Candles service failed with message: {ex.Message}", ex); } } }
private async Task SaveCandle(BinanceStreamKline kline, NatsClient natsClient) { if (kline.Final) { Candle candle = kline.ToCandle(); long? id = await _candlesProcessor.Create(candle); _logger.LogTrace($"Saved candle from {Exchange.Description} - {candle.Symbol}"); await natsClient.PubAsJsonAsync(_settings.Value.CandlesQueueName, new Notification <Candle>() { Code = ActionCode.CREATED.Code, Payload = candle }); } }