示例#1
0
 public static IFeedCandle ToCandle(this CandleItem candle, bool isBuy, DateTime baseTime, TimeInterval interval)
 {
     if (candle != null)
     {
         return(new FeedCandle
         {
             Open = candle.Open,
             Close = candle.Close,
             High = candle.High,
             Low = candle.Low,
             IsBuy = isBuy,
             DateTime = baseTime.AddIntervalTicks(candle.Tick, interval)
         });
     }
     return(null);
 }
示例#2
0
        private async Task CacheAssetPairCandlesAsync(AssetPair assetPair, DateTime now, SlotType slotType)
        {
            try
            {
                _log.Info(nameof(InitializeCacheAsync), $"Caching {assetPair.Id} candles history...");

                foreach (CandlePriceType priceType in Constants.StoredPriceTypes)
                {
                    foreach (CandleTimeInterval timeInterval in Constants.InitFromDbIntervals)
                    {
                        DateTime alignedToDate   = now.TruncateTo(timeInterval).AddIntervalTicks(1, timeInterval);
                        DateTime alignedFromDate = alignedToDate.AddIntervalTicks(-_amountOfCandlesToStore[timeInterval] - 1, timeInterval);

                        if (alignedFromDate < _minDate)
                        {
                            alignedFromDate = _minDate.TruncateTo(timeInterval);
                        }

                        ICandle[] candles = (await _candlesHistoryRepository.GetCandlesAsync(assetPair.Id, timeInterval, priceType, alignedFromDate, alignedToDate)).ToArray();

                        if (!candles.Any())
                        {
                            continue;
                        }

                        var intervals = _candlesCacheService.GetRedisCacheIntervals(timeInterval);

                        foreach (var interval in intervals)
                        {
                            ICandle[] intervalCandles = interval != timeInterval
                                ? CandlesMerger.MergeIntoBiggerIntervals(candles, interval).ToArray()
                                : candles;

                            await _candlesCacheService.InitializeAsync(assetPair.Id, priceType, interval, intervalCandles, slotType);
                        }
                    }
                }

                _log.Info($"{assetPair.Id} candles history is cached.");
            }
            catch (Exception ex)
            {
                _log.Error(nameof(CacheAssetPairCandlesAsync), ex);
            }
        }
        public ICandle ToCandle(string assetPairId, CandlePriceType priceType, DateTime baseTime, CandleTimeInterval timeInterval)
        {
            var normalizedTick = Tick - GetIntervalTickOrigin(timeInterval);

            return(Candle.Create
                   (
                       open: Open,
                       close: Close,
                       high: High,
                       low: Low,
                       assetPair: assetPairId,
                       priceType: priceType,
                       timeInterval: timeInterval,
                       timestamp: baseTime.AddIntervalTicks(normalizedTick, timeInterval),
                       tradingVolume: TradingVolume,
                       tradingOppositeVolume: TradingOppositeVolume,
                       lastTradePrice: LastTradePrice,
                       lastUpdateTimestamp: LastUpdateTimestamp
                   ));
        }
        public void AddIntervalTicks_BasicChecks()
        {
            DateTime baseTime = new DateTime(2017, 1, 1, 0, 0, 0);

            // Sec
            Assert.Equal(baseTime.AddSeconds(0), baseTime.AddIntervalTicks(0, 0, TimeInterval.Sec));
            Assert.Equal(baseTime.AddSeconds(1), baseTime.AddIntervalTicks(0, 1, TimeInterval.Sec));
            // Minute
            Assert.Equal(baseTime.AddMinutes(0), baseTime.AddIntervalTicks(0, 0, TimeInterval.Minute));
            Assert.Equal(baseTime.AddMinutes(1), baseTime.AddIntervalTicks(0, 1, TimeInterval.Minute));
            // Min5
            Assert.Equal(baseTime.AddMinutes(0), baseTime.AddIntervalTicks(0, 0, TimeInterval.Min5));
            Assert.Equal(baseTime.AddMinutes(5), baseTime.AddIntervalTicks(0, 1, TimeInterval.Min5));
            // Min15
            Assert.Equal(baseTime.AddMinutes(0), baseTime.AddIntervalTicks(0, 0, TimeInterval.Min15));
            Assert.Equal(baseTime.AddMinutes(15), baseTime.AddIntervalTicks(0, 1, TimeInterval.Min15));
            // Min30
            Assert.Equal(baseTime.AddMinutes(0), baseTime.AddIntervalTicks(0, 0, TimeInterval.Min30));
            Assert.Equal(baseTime.AddMinutes(30), baseTime.AddIntervalTicks(0, 1, TimeInterval.Min30));
            // Hour
            Assert.Equal(baseTime.AddHours(0), baseTime.AddIntervalTicks(0, 0, TimeInterval.Hour));
            Assert.Equal(baseTime.AddHours(1), baseTime.AddIntervalTicks(0, 1, TimeInterval.Hour));
            Assert.Equal(baseTime.AddDays(1).AddHours(0), baseTime.AddIntervalTicks(1, 0, TimeInterval.Hour));
            Assert.Equal(baseTime.AddDays(1).AddHours(1), baseTime.AddIntervalTicks(1, 1, TimeInterval.Hour));

            // Hour4
            Assert.Equal(baseTime.AddHours(0), baseTime.AddIntervalTicks(0, 0, TimeInterval.Hour4));
            Assert.Equal(baseTime.AddHours(4), baseTime.AddIntervalTicks(0, 1, TimeInterval.Hour4));
            Assert.Equal(baseTime.AddDays(1).AddHours(0), baseTime.AddIntervalTicks(1, 0, TimeInterval.Hour4));
            Assert.Equal(baseTime.AddDays(1).AddHours(4), baseTime.AddIntervalTicks(1, 1, TimeInterval.Hour4));

            // Days ticks are in [1..31]
            Assert.Equal(baseTime.AddDays(0), baseTime.AddIntervalTicks(0, 1, TimeInterval.Day));
            Assert.Equal(baseTime.AddDays(1), baseTime.AddIntervalTicks(0, 2, TimeInterval.Day));

            Assert.Equal(baseTime.AddMonths(1).AddDays(0), baseTime.AddIntervalTicks(1, 1, TimeInterval.Day));
            Assert.Equal(baseTime.AddMonths(1).AddDays(1), baseTime.AddIntervalTicks(1, 2, TimeInterval.Day));

            // Week
            Assert.Equal(baseTime.AddDays(7), baseTime.AddIntervalTicks(0, 1, TimeInterval.Week));
            Assert.Equal(baseTime.AddDays(14), baseTime.AddIntervalTicks(0, 2, TimeInterval.Week));
            Assert.Equal(baseTime.AddDays(7 * 52), baseTime.AddIntervalTicks(0, 52, TimeInterval.Week));

            Assert.Equal(baseTime.AddDays(7), baseTime.AddIntervalTicks(1, 1, TimeInterval.Week));

            // Months ticks are in [1..12]
            Assert.Equal(baseTime.AddMonths(0), baseTime.AddIntervalTicks(0, 1, TimeInterval.Month));
            Assert.Equal(baseTime.AddMonths(1), baseTime.AddIntervalTicks(0, 2, TimeInterval.Month));
        }