Exemplo n.º 1
0
        public void RepositoryHandlesConcurrencyConflictsOnUpdate()
        {
            var      logger   = new LogToMemory();
            DateTime baseTime = new DateTime(2017, 03, 01, 0, 0, 0, DateTimeKind.Utc);

            // Clear table and insert entity
            var storage = CreateStorage <CandleTableEntity>(logger, clear: true);
            var candle  = new FeedCandle()
            {
                DateTime = baseTime, Open = 0, Close = 0, High = 0, Low = 0
            };

            new CandleHistoryRepository(storage).InsertOrMergeAsync(candle, PriceType.Ask, TimeInterval.Hour).Wait();

            // Run multiple update tasks simultaneously
            var tasks = new List <Task>();

            for (int t = 1; t < 11; t++)
            {
                tasks.Add(Task.Run(() =>
                {
                    var repo    = new CandleHistoryRepository(CreateStorage <CandleTableEntity>(logger, clear: false));
                    var candlet = new FeedCandle()
                    {
                        DateTime = baseTime.AddHours(t), Open = t, Close = t, High = t, Low = t
                    };
                    repo.InsertOrMergeAsync(candlet, PriceType.Ask, TimeInterval.Hour).Wait();
                }));
            }
            Task.WhenAll(tasks).Wait();
        }
        public void PriceValuesAreMerged()
        {
            DateTime dt = new DateTime(2017, 1, 1);

            // Case 1: Update values are taken
            //
            var target = new FeedCandle()
            {
                Open = 2, Close = 3, High = 4, Low = 1, IsBuy = true, DateTime = dt
            };
            var update = new FeedCandle()
            {
                Open = 3, Close = 2, High = 5, Low = 0.1, IsBuy = true, DateTime = dt
            };

            IFeedCandle merged = target.MergeWith(update);

            Assert.Equal(update.Open, merged.Open);
            Assert.Equal(update.Close, merged.Close);
            Assert.Equal(update.High, merged.High);
            Assert.Equal(update.Low, merged.Low);
            Assert.True(merged.IsBuy);
            Assert.Equal(merged.DateTime, dt);

            // Case 2: Target values are preserved
            //
            update = new FeedCandle()
            {
                Open = 3, Close = 2, High = 3, Low = 2, IsBuy = true, DateTime = dt
            };

            merged = target.MergeWith(update);
            Assert.Equal(target.High, merged.High);
            Assert.Equal(target.Low, merged.Low);
        }
        public void MergingCandlesWithDiffTimeIsUnsupported()
        {
            var target = new FeedCandle()
            {
                Open = 2, Close = 3, High = 4, Low = 1, IsBuy = true, DateTime = new DateTime(2017, 1, 1)
            };
            var update = new FeedCandle()
            {
                Open = 3, Close = 2, High = 5, Low = 0.1, IsBuy = true, DateTime = new DateTime(2017, 1, 2)
            };

            Assert.Throws(typeof(InvalidOperationException), () => target.MergeWith(update));
        }
Exemplo n.º 4
0
        private static void GenerateAndStoreCandles(CandleHistoryRepository repo, string asset, DateTime baseTime, TimeInterval interval)
        {
            List <IFeedCandle> candles = new List <IFeedCandle>(3600 * 3);

            for (int second = 0; second < 3600 * 3; second++)
            {
                var candle = new FeedCandle()
                {
                    DateTime = baseTime.AddSeconds(second),
                    IsBuy    = true,
                    Open     = second,
                    Close    = second + 1,
                    High     = second + 1,
                    Low      = second
                };
                candles.Add(candle);
            }

            repo.InsertOrMergeAsync(candles, asset, interval).Wait();
        }