Example #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();
        }
Example #2
0
        public void WriteReadMinutes()
        {
            var      asset    = "EURUSD";
            var      interval = TimeInterval.Minute;
            var      logger   = new LogToMemory();
            var      storage  = CreateStorage <CandleTableEntity>(logger);
            var      repo     = new CandleHistoryRepository(storage);
            DateTime baseTime = new DateTime(2017, 03, 01, 0, 0, 0, DateTimeKind.Utc);

            var candlesIn = from i in Enumerable.Range(0, 60 * 24 * 3) // 3 days
                            select new FeedCandle()
            {
                DateTime = baseTime.AddMinutes(i), IsBuy = true, Open = i, Close = i, High = i, Low = i
            };

            repo.InsertOrMergeAsync(candlesIn, asset, interval).Wait();

            // Select for 1 hour
            var queriedCandles = new CandleHistoryRepository(storage).GetCandlesAsync(asset, interval, true,
                                                                                      from: baseTime.AddHours(1),
                                                                                      to: baseTime.AddHours(2)).Result;

            Assert.NotNull(queriedCandles);
            Assert.Equal(60, queriedCandles.Count());

            Assert.True(queriedCandles.First().IsEqual(
                            new FeedCandle()
            {
                DateTime = baseTime.AddHours(1), IsBuy = true, Open = 60, Close = 60, High = 60, Low = 60
            }));
            Assert.True(queriedCandles.Last().IsEqual(
                            new FeedCandle()
            {
                DateTime = baseTime.AddHours(1).AddMinutes(59), IsBuy = true, Open = 119, Close = 119, High = 119, Low = 119
            }));

            // Select for 1 day
            queriedCandles = new CandleHistoryRepository(storage).GetCandlesAsync(asset, interval, true,
                                                                                  from: baseTime.AddDays(1),
                                                                                  to: baseTime.AddDays(2)).Result;

            Assert.NotNull(queriedCandles);
            Assert.Equal(60 * 24 * 1, queriedCandles.Count());

            // Read/write 1 candle
            new CandleHistoryRepository(storage).InsertOrMergeAsync(
                new FeedCandle()
            {
                DateTime = baseTime.AddHours(1), IsBuy = true, Open = 99, Close = 99, High = 99, Low = 99
            }, asset, interval).Wait();

            var candle = new CandleHistoryRepository(storage).GetCandleAsync(asset, interval, true, baseTime.AddHours(1)).Result;

            Assert.True(candle.IsEqual(
                            new FeedCandle()
            {
                DateTime = baseTime.AddHours(1), IsBuy = true, Open = 99, Close = 99, High = 99, Low = 60
            }));
        }
Example #3
0
        public void WriteReadHours()
        {
            var      interval = TimeInterval.Hour;
            var      logger   = new LogToMemory();
            var      storage  = CreateStorage <CandleTableEntity>(logger);
            var      repo     = new CandleHistoryRepository(storage);
            DateTime baseTime = new DateTime(2017, 03, 01, 0, 0, 0, DateTimeKind.Utc);

            var candlesIn = from i in Enumerable.Range(0, 24 * 31 * 3) // 3 months
                            select new FeedCandle()
            {
                DateTime = baseTime.AddHours(i), Open = i, Close = i, High = i, Low = i
            };

            repo.InsertOrMergeAsync(candlesIn, PriceType.Bid, interval).Wait();

            // Select for 1 day
            var queriedCandles = new CandleHistoryRepository(storage).GetCandlesAsync(PriceType.Bid, interval,
                                                                                      @from: baseTime.AddDays(1),
                                                                                      to: baseTime.AddDays(2)).Result;

            Assert.NotNull(queriedCandles);
            Assert.Equal(24, queriedCandles.Count());

            Assert.True(queriedCandles.First().IsEqual(
                            new FeedCandle()
            {
                DateTime = baseTime.AddDays(1), IsBuy = true, Open = 24, Close = 24, High = 24, Low = 24
            }));
            Assert.True(queriedCandles.Last().IsEqual(
                            new FeedCandle()
            {
                DateTime = baseTime.AddDays(1).AddHours(23), IsBuy = true, Open = 47, Close = 47, High = 47, Low = 47
            }));

            // Select for 1 month
            queriedCandles = new CandleHistoryRepository(storage).GetCandlesAsync(PriceType.Bid, interval,
                                                                                  @from: baseTime.AddMonths(1),
                                                                                  to: baseTime.AddMonths(2)).Result;

            Assert.NotNull(queriedCandles);
            Assert.Equal(24 * 30, queriedCandles.Count());

            // Read/write 1 candle
            new CandleHistoryRepository(storage).InsertOrMergeAsync(
                new FeedCandle()
            {
                DateTime = baseTime.AddDays(1), IsBuy = true, Open = 99, Close = 99, High = 99, Low = 99
            }, PriceType.Bid, interval).Wait();

            var candle = new CandleHistoryRepository(storage).GetCandleAsync(PriceType.Bid, interval, baseTime.AddDays(1)).Result;

            Assert.True(candle.IsEqual(
                            new FeedCandle()
            {
                DateTime = baseTime.AddDays(1), IsBuy = true, Open = 99, Close = 99, High = 99, Low = 24
            }));
        }
Example #4
0
        public void WriteRead30Minutes()
        {
            var      interval = TimeInterval.Min30;
            var      logger   = new LogToMemory();
            var      storage  = CreateStorage <CandleTableEntity>(logger);
            var      repo     = new CandleHistoryRepository(storage);
            DateTime baseTime = new DateTime(2017, 03, 01, 0, 0, 0, DateTimeKind.Utc);

            var candlesIn = from i in Enumerable.Range(0, 2 * 24 * 3) // 3 days
                            select new FeedCandle()
            {
                DateTime = baseTime.AddMinutes(30 * i), Open = i, Close = i, High = i, Low = i
            };

            repo.InsertOrMergeAsync(candlesIn, PriceType.Mid, interval).Wait();

            // Select for 1 hour
            var queriedCandles = new CandleHistoryRepository(storage).GetCandlesAsync(PriceType.Mid, interval,
                                                                                      @from: baseTime.AddHours(1),
                                                                                      to: baseTime.AddHours(2)).Result;

            Assert.NotNull(queriedCandles);
            Assert.Equal(2, queriedCandles.Count());

            Assert.True(queriedCandles.First().IsEqual(
                            new FeedCandle()
            {
                DateTime = baseTime.AddHours(1), Open = 2, Close = 2, High = 2, Low = 2
            }));
            Assert.True(queriedCandles.Last().IsEqual(
                            new FeedCandle()
            {
                DateTime = baseTime.AddHours(1).AddMinutes(30), Open = 3, Close = 3, High = 3, Low = 3
            }));

            // Select for 1 day
            queriedCandles = new CandleHistoryRepository(storage).GetCandlesAsync(PriceType.Mid, interval,
                                                                                  @from: baseTime.AddDays(1),
                                                                                  to: baseTime.AddDays(2)).Result;

            Assert.NotNull(queriedCandles);
            Assert.Equal(2 * 24, queriedCandles.Count());

            // Read/write 1 candle
            new CandleHistoryRepository(storage).InsertOrMergeAsync(
                new FeedCandle()
            {
                DateTime = baseTime.AddHours(1), Open = 99, Close = 99, High = 99, Low = 99
            }, PriceType.Mid, interval).Wait();

            var candle = new CandleHistoryRepository(storage).GetCandleAsync(PriceType.Mid, interval, baseTime.AddHours(1)).Result;

            Assert.True(candle.IsEqual(
                            new FeedCandle()
            {
                DateTime = baseTime.AddHours(1), Open = 99, Close = 99, High = 99, Low = 2
            }));
        }
Example #5
0
        public void WriteReadMonths()
        {
            var      asset    = "EURUSD";
            var      interval = TimeInterval.Month;
            var      logger   = new LogToMemory();
            var      storage  = CreateStorage <CandleTableEntity>(logger);
            var      repo     = new CandleHistoryRepository(storage);
            DateTime baseTime = new DateTime(2017, 03, 01, 0, 0, 0, DateTimeKind.Utc);

            var candlesIn = from i in Enumerable.Range(0, 12 * 3) // 3 years
                            select new FeedCandle()
            {
                DateTime = baseTime.AddMonths(i), IsBuy = true, Open = i, Close = i, High = i, Low = i
            };

            repo.InsertOrMergeAsync(candlesIn, asset, interval).Wait();

            // Select for 1 year
            var queriedCandles = new CandleHistoryRepository(storage).GetCandlesAsync(asset, interval, true,
                                                                                      from: baseTime.AddYears(1),
                                                                                      to: baseTime.AddYears(2)).Result;

            Assert.NotNull(queriedCandles);
            Assert.Equal(12, queriedCandles.Count());

            Assert.True(queriedCandles.First().IsEqual(
                            new FeedCandle()
            {
                DateTime = baseTime.AddYears(1), IsBuy = true, Open = 12, Close = 12, High = 12, Low = 12
            }));
            Assert.True(queriedCandles.Last().IsEqual(
                            new FeedCandle()
            {
                DateTime = baseTime.AddYears(1).AddMonths(11), IsBuy = true, Open = 23, Close = 23, High = 23, Low = 23
            }));

            // Select for 1 month
            queriedCandles = new CandleHistoryRepository(storage).GetCandlesAsync(asset, interval, true,
                                                                                  from: baseTime,
                                                                                  to: baseTime.AddMonths(1)).Result;

            Assert.NotNull(queriedCandles);
            Assert.Equal(1, queriedCandles.Count());
        }
Example #6
0
        public void WriteReadMultipleIntervals()
        {
            var      asset    = "EURUSD";
            var      logger   = new LogToMemory();
            var      storage  = CreateStorage <CandleTableEntity>(logger);
            var      repo     = new CandleHistoryRepository(storage);
            DateTime baseTime = new DateTime(2017, 03, 01, 0, 0, 0, DateTimeKind.Utc);

            var candlesMinutes = from i in Enumerable.Range(0, 60 * 24 * 3) // 3 days
                                 select new FeedCandle()
            {
                DateTime = baseTime.AddMinutes(i), IsBuy = true, Open = i, Close = i, High = i, Low = i
            };

            var candlesHours = from i in Enumerable.Range(0, 24 * 31 * 3) // 3 months
                               select new FeedCandle()
            {
                DateTime = baseTime.AddHours(i), IsBuy = true, Open = i, Close = i, High = i, Low = i
            };

            repo.InsertOrMergeAsync(new Dictionary <TimeInterval, IEnumerable <IFeedCandle> >()
            {
                { TimeInterval.Minute, candlesMinutes },
                { TimeInterval.Hour, candlesHours }
            }, asset).Wait();

            // Select minutes for 1 day
            var queriedCandles = new CandleHistoryRepository(storage).GetCandlesAsync(asset, TimeInterval.Minute, true,
                                                                                      from: baseTime.AddDays(1),
                                                                                      to: baseTime.AddDays(2)).Result;

            Assert.NotNull(queriedCandles);
            Assert.Equal(60 * 24 * 1, queriedCandles.Count());

            // Select hours for 1 month
            queriedCandles = new CandleHistoryRepository(storage).GetCandlesAsync(asset, TimeInterval.Hour, true,
                                                                                  from: baseTime.AddMonths(1),
                                                                                  to: baseTime.AddMonths(2)).Result;

            Assert.NotNull(queriedCandles);
            Assert.Equal(24 * 30, queriedCandles.Count());
        }
Example #7
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();
        }