Ejemplo n.º 1
0
        public async Task <IActionResult> Get([FromRoute] CandleSticksRequestModel request)
        {
            try
            {
                var candleHistoryService = _candlesServiceProvider.Get(request.Type);

                var candles = await candleHistoryService.GetCandlesHistoryAsync(
                    request.AssetPairId,
                    (CandlePriceType)Enum.Parse(typeof(CandlePriceType), request.PriceType.ToString()),
                    (CandleTimeInterval)Enum.Parse(typeof(CandleTimeInterval), request.TimeInterval.ToString()),
                    request.FromMoment,
                    request.ToMoment);

                var assetPair = (await _assetPairs.Values()).FirstOrDefault(x => x.Id == request.AssetPairId);

                var baseAsset = await _assetsService.AssetGetAsync(assetPair.BaseAssetId);

                var quotingAsset = await _assetsService.AssetGetAsync(assetPair.QuotingAssetId);

                return(Ok(
                           candles.ToResponseModel(
                               baseAsset.DisplayAccuracy ?? baseAsset.Accuracy,
                               quotingAsset.DisplayAccuracy ?? quotingAsset.Accuracy)));
            }
            catch (ErrorResponseException ex)
            {
                var errors = ex.Error.ErrorMessages.Values.SelectMany(s => s.Select(ss => ss));
                return(NotFound($"{string.Join(',', errors)}"));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> GetCandles([FromQuery] CandleSticksRequestModel request)
        {
            try
            {
                var assetPair = await _assetsHelper.GetAssetPairAsync(request.AssetPairId);

                if (assetPair == null)
                {
                    return(NotFound("Asset pair not found"));
                }

                var candleHistoryService = _candlesServiceProvider.Get(request.Type);

                var candles = await candleHistoryService.GetCandlesHistoryAsync(
                    request.AssetPairId,
                    request.PriceType,
                    request.TimeInterval,
                    request.FromMoment,
                    request.ToMoment);

                var baseAsset = await _assetsHelper.GetAssetAsync(assetPair.BaseAssetId);

                var quotingAsset = await _assetsHelper.GetAssetAsync(assetPair.QuotingAssetId);

                return(Ok(
                           candles.ToResponseModel(
                               baseAsset.DisplayAccuracy ?? baseAsset.Accuracy,
                               quotingAsset.DisplayAccuracy ?? quotingAsset.Accuracy)));
            }
            catch (ErrorResponseException ex)
            {
                var errors = ex.Error.ErrorMessages.Values.SelectMany(s => s.Select(ss => ss));
                return(NotFound($"{string.Join(',', errors)}"));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets (a set of) today's Day candle(s) of type Trades and Spot market.
        /// </summary>
        /// <param name="assetPairId">The target asset pair ID. If not specified (is null or empty string), there will be gathered the info about all the registered asset pairs.</param>
        /// <returns>A dictionary where the Key is the asset pair ID and the Value contains the today's Day Spot candle for the asset pair.</returns>
        /// <remarks>When there is no Day Spot Trade candle for some asset pair, it will not be presented in the resulting dictionary. Thus, if assetPairId parameter is specified
        /// but there is no a suitable candle for it, the method will return an empty dictionary.</remarks>
        private async Task <Dictionary <string, Candle> > GetTodaySpotCandlesAsync(string assetPairId = null)
        {
            var historyService = _candlesHistoryProvider.Get(MarketType.Spot);

            var todayCandles = new Dictionary <string, Candle>();

            var dateFromInclusive = DateTime.UtcNow.Date;
            var dateToExclusive   = dateFromInclusive.AddDays(1);

            if (!string.IsNullOrWhiteSpace(assetPairId))
            {
                var todayCandleHistory = await historyService.TryGetCandlesHistoryAsync(assetPairId,
                                                                                        CandlePriceType.Trades, CandleTimeInterval.Day, dateFromInclusive, dateToExclusive);

                if (todayCandleHistory?.History == null ||
                    !todayCandleHistory.History.Any())
                {
                    return(todayCandles);
                }

                if (todayCandleHistory.History.Count > 1) // The unbelievable case.
                {
                    throw new AmbiguousMatchException($"It seems like we have more than one today's Day Spot trade candle for asset pair {assetPairId}.");
                }

                todayCandles.Add(
                    assetPairId,
                    todayCandleHistory
                    .History
                    .Single()
                    );
            }
            else
            {
                var assetPairs = await historyService.GetAvailableAssetPairsAsync();

                var todayCandleHistoryForPairs = await historyService.GetCandlesHistoryBatchAsync(assetPairs,
                                                                                                  CandlePriceType.Trades, CandleTimeInterval.Day, dateFromInclusive, dateToExclusive);

                if (todayCandleHistoryForPairs == null) // Some technical issue has happened without an exception.
                {
                    throw new InvalidOperationException("Could not obtain today's Day Spot trade candles at all.");
                }

                if (!todayCandleHistoryForPairs.Any())
                {
                    return(todayCandles);
                }

                foreach (var historyForPair in todayCandleHistoryForPairs)
                {
                    if (historyForPair.Value?.History == null ||
                        !historyForPair.Value.History.Any())
                    {
                        continue;
                    }

                    if (historyForPair.Value.History.Count > 1) // The unbelievable case.
                    {
                        throw new AmbiguousMatchException($"It seems like we have more than one today's Day Spot trade candle for asset pair {assetPairId}.");
                    }

                    todayCandles.Add(
                        historyForPair.Key,
                        historyForPair.Value
                        .History
                        .Single()
                        );
                }
            }

            return(todayCandles);
        }