Exemple #1
0
        public async Task ImportAllMinutePairCandles(string fromSymbol, string toSymbol, string exchange)
        {
            ExchangePair pair = await _coreDBContext.Pairs
                                .FirstOrDefaultAsync(x =>
                                                     x.CurrencyTo.Symbol == toSymbol &&
                                                     x.CurrencyFrom.Symbol == fromSymbol &&
                                                     x.Exchange.Name == exchange);

            if (pair != null)
            {
                var command = new HistoricalCandleCommand
                {
                    Exchange   = exchange,
                    FromSymbol = fromSymbol,
                    ToSymbol   = toSymbol,
                    Period     = AlphaVantageHistoricalPeriod.OneMinute
                };
                var commandResult = await _cryptoCompareGateway.HistoricalCandle(command);

                if (commandResult?.IsSuccess == true && commandResult.Content.Count > 0)
                {
                    List <Candle> candles = _mapper.Map <List <Candle> >(commandResult.Content);
                    candles.ForEach(c => c.ExchangePairId = pair.Id);
                    if (candles?.Count > 0)
                    {
                        await _coreDBContext.AddRangeAsync(candles);

                        await _coreDBContext.SaveChangesAsync();
                    }
                }
            }
        }
        public async Task <CommandResult <List <CandleDTO> > > HistoricalCandle(HistoricalCandleCommand command)
        {
            var request = new HistoricalCandleRequest(_defaultRequest)
            {
                FromSymbol  = command.FromSymbol,
                ToSymbol    = command.ToSymbol,
                Exchange    = command.Exchange,
                ToTimeStamp = command.BeforeTimestamp ?? DateTime.Now.ToUnixTimeStamp(),
                Limit       = command.Limit
            };

            CommandResult <List <CandleDTO> > response = null;

            switch (command.Period)
            {
            case AlphaVantageHistoricalPeriod.OneMinute:
            {
                response = await _minuteCandleUseCase.Execute(request, _uriBuilder);

                break;
            }

            case AlphaVantageHistoricalPeriod.Hour:
            {
                response = await _hourlyCandleUseCase.Execute(request, _uriBuilder);

                break;
            }

            case AlphaVantageHistoricalPeriod.Day:
            {
                response = await _dailyCandleUseCase.Execute(request, _uriBuilder);

                break;
            }
            }

            return(response);
        }
Exemple #3
0
        public async Task <CommandResult <List <CandleDTO> > > HistoricalData([FromQuery] HistoricalCandleCommand command)
        {
            var result = await _cryptoCompareGateway.HistoricalCandle(command);

            return(result);
        }
Exemple #4
0
        public async Task ImportAllMinutePairCandles_Old(string fromSymbol, string toSymbol, string exchange)
        {
            if (string.IsNullOrEmpty(fromSymbol) == false && string.IsNullOrEmpty(toSymbol) == false && string.IsNullOrEmpty(exchange) == false)
            {
                ExchangePair pair = await _coreDBContext.Pairs
                                    .FirstOrDefaultAsync(x =>
                                                         x.CurrencyTo.Symbol == toSymbol &&
                                                         x.CurrencyFrom.Symbol == fromSymbol &&
                                                         x.Exchange.Name == exchange);


                bool isActive = true;

                if (pair != null)
                {
                    int           currentRequestNumber = 0;
                    int           maxRequestNumber     = 10000;
                    long          currentTimestamp     = DateTime.Now.ToUnixTimeStamp();
                    List <Candle> candles = new List <Candle>(2000);

                    while (isActive && currentRequestNumber < maxRequestNumber)
                    {
                        long interval = 60;
                        //var result = await _cryptoCompareGateway.MinuteCandle(fromSymbol, toSymbol, exchange, currentTimestamp, 3);
                        var command = new HistoricalCandleCommand
                        {
                            FromSymbol      = fromSymbol,
                            ToSymbol        = toSymbol,
                            Exchange        = exchange,
                            BeforeTimestamp = currentTimestamp,
                            Limit           = 3,
                            Period          = AlphaVantageHistoricalPeriod.OneMinute
                        };

                        var result = await _cryptoCompareGateway.HistoricalCandle(command);


                        if (result?.IsSuccess == true)
                        {
                            List <CandleDTO> candleDTOs = result?.Content;
                            currentRequestNumber++;
                            candles.Clear();

                            if (candleDTOs?.Count > 0)
                            {
                                if (currentRequestNumber > 1)
                                {
                                    candleDTOs = candleDTOs.Take(candleDTOs.Count - 1).ToList();
                                }
                                foreach (var candleDTO in candleDTOs)
                                {
                                    Candle candle = _mapper.Map <Candle>(candleDTO);
                                    candle.ExchangePairId = pair.Id;
                                    candle.TimeClose      = candle.TimeOpen + interval;
                                    candle.Interval       = interval;

                                    candles.Add(candle);
                                }

                                currentTimestamp = candleDTOs.First().Time;

                                try
                                {
                                    var dif = _coreDBContext.Candles
                                              .Include(x => x.ExchangePair)
                                              .AsEnumerable()
                                              .Where(c => c.ExchangePairId == pair.Id && candles.Any(x => x.TimeOpen == c.TimeOpen && x.TimeClose == c.TimeClose))
                                              .ToList();

                                    await _coreDBContext.AddRangeAsync(candles);

                                    await _coreDBContext.SaveChangesAsync();

                                    _logger.Information($"Load {candles.Count} candles for {fromSymbol}-{toSymbol} from {exchange}");
                                }
                                catch (DbUpdateException ex)
                                {
                                    _logger.Error(ex.GetBaseException(), ex?.InnerException?.Message ?? ex.Message);
                                }
                                catch (InvalidOperationException ex)
                                {
                                    var dif = _coreDBContext.Candles
                                              .Include(x => x.ExchangePair)
                                              .AsEnumerable()
                                              .Where(c => c.ExchangePairId == pair.Id && candles.Any(x => x.TimeOpen == c.TimeOpen && x.TimeClose == c.TimeClose))
                                              .ToList();
                                    //var c = candles.GroupBy(x => new { x.TimeOpen, x.TimeClose, x.PairId }).ToArray().Count();
                                    _logger.Error(ex, "");
                                }
                                catch (Exception ex)
                                {
                                    //var c = candles.GroupBy(x => new { x.TimeOpen, x.TimeClose, x.PairId }).ToArray().Count();
                                    _logger.Error(ex, "");
                                }
                            }
                            else
                            {
                                isActive = false;
                            }
                        }
                    }
                }
                else
                {
                    //пара отсутствует на бирже
                }
            }
        }