コード例 #1
0
        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);
                }
            }
        }
コード例 #2
0
        protected override async Task DoWork(PairConfig pair, CancellationToken stoppingToken)
        {
            _logger.LogInformation($"{Exchange.Description} Candles worker is started");

            IPeriodCode candlePeriod = pair.Timeframe.HasValue ? PeriodCode.Create(pair.Timeframe.Value) : DefaultCandleInterval;

            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    using (BinanceSocketClient client = new BinanceSocketClient())
                    {
                        ConnectionInfo cnInfo = new ConnectionInfo(_settings.Value.BusConnectionString);
                        using (NatsClient natsClient = new NatsClient(cnInfo))
                        {
                            if (!natsClient.IsConnected)
                            {
                                natsClient.Connect();
                            }

                            CallResult <UpdateSubscription> successKline = client.SubscribeToKlineUpdates(pair.Symbol, candlePeriod.ToPeriodCode(), async(data) =>
                            {
                                await SaveCandle(data.Data, natsClient);
                            });

                            successKline.Data.ConnectionLost     += () => { _logger.LogError($"Connection to {Exchange} is lost"); };
                            successKline.Data.ConnectionRestored += (data) => { _logger.LogError($"Connection to {Exchange} is Restored"); };

                            while (!stoppingToken.IsCancellationRequested)
                            {
                            }

                            natsClient.Disconnect();
                            await client.Unsubscribe(successKline.Data);
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError($"{Exchange.Description} Candles service failed with message: {ex.Message}", ex);
                }
            }
        }