Exemplo n.º 1
0
        public void ShouldIncludesNodaTimeRange()
        {
            var batchdr = new BatchDateRange(DateTimeExtensions.CreateNodaTime(2019, 2, 10, 0, 0, 0),
                                             DateTimeExtensions.CreateNodaTime(2019, 2, 16, 23, 59, 59));
            // start == batchdr.Start && end == batchdr.Start
            var dr1 = new BatchDateRange(DateTimeExtensions.CreateNodaTime(2019, 2, 10, 0, 0, 0), DateTimeExtensions.CreateNodaTime(2019, 2, 10, 0, 0, 0));

            Assert.True(batchdr.Includes(dr1));
            // start == batchdr.Start && end == batchdr.End
            var dr2 = new BatchDateRange(DateTimeExtensions.CreateNodaTime(2019, 2, 10, 0, 0, 0), DateTimeExtensions.CreateNodaTime(2019, 2, 16, 23, 59, 59));

            Assert.True(batchdr.Includes(dr2));
            // start == batchdr.End && end == batchdr.End
            var dr3 = new BatchDateRange(DateTimeExtensions.CreateNodaTime(2019, 2, 16, 23, 59, 59), DateTimeExtensions.CreateNodaTime(2019, 2, 16, 23, 59, 59));

            Assert.True(batchdr.Includes(dr3));
            // start > batchdr.Start && end < batchdr.End
            var dr4 = new BatchDateRange(DateTimeExtensions.CreateNodaTime(2019, 2, 10, 1, 59, 59), DateTimeExtensions.CreateNodaTime(2019, 2, 15, 23, 59, 59));

            Assert.True(batchdr.Includes(dr4));
            // start > batchdr.Start && end > batchdr.End
            var dr5 = new BatchDateRange(DateTimeExtensions.CreateNodaTime(2019, 2, 10, 1, 59, 59), DateTimeExtensions.CreateNodaTime(2019, 2, 17, 23, 59, 59));

            Assert.False(batchdr.Includes(dr5));
            // start < batchdr.Start && end < batchdr.End
            var dr6 = new BatchDateRange(DateTimeExtensions.CreateNodaTime(2019, 2, 9, 1, 59, 59), DateTimeExtensions.CreateNodaTime(2019, 2, 15, 23, 59, 59));

            Assert.False(batchdr.Includes(dr6));
            // start < batchdr.Start && end > batchdr.End
            var dr7 = new BatchDateRange(DateTimeExtensions.CreateNodaTime(2019, 2, 9, 1, 59, 59), DateTimeExtensions.CreateNodaTime(2019, 2, 17, 23, 59, 59));

            Assert.False(batchdr.Includes(dr7));
        }
Exemplo n.º 2
0
        public void ShouldReturnPrettyString()
        {
            var    batchdr = new BatchDateRange(DateTimeExtensions.CreateNodaTime(2019, 2, 10, 0, 0, 0), DateTimeExtensions.CreateNodaTime(2019, 2, 16, 23, 59, 59));
            string str     = batchdr.ToString();

            Assert.Equal("Start: 2019-02-10 00:00:00 End: 2019-02-16 23:59:59", str);
        }
Exemplo n.º 3
0
        public void ShouldIncludesNodaTime()
        {
            var batchdr = new BatchDateRange(DateTimeExtensions.CreateNodaTime(2019, 2, 10, 0, 0, 0),
                                             DateTimeExtensions.CreateNodaTime(2019, 2, 16, 23, 59, 59));
            // start
            bool included = batchdr.Includes(DateTimeExtensions.CreateNodaTime(2019, 2, 10, 0, 0, 0));

            Assert.True(included);
            // > start && < end
            bool included1 = batchdr.Includes(DateTimeExtensions.CreateNodaTime(2019, 2, 13, 23, 22, 22));

            Assert.True(included1);
            // end
            bool included2 = batchdr.Includes(DateTimeExtensions.CreateNodaTime(2019, 2, 16, 23, 59, 59));

            Assert.True(included2);
            // < start
            bool included3 = batchdr.Includes(DateTimeExtensions.CreateNodaTime(1, 2, 13, 23, 22, 22));

            Assert.False(included3);
            // > end
            bool included4 = batchdr.Includes(DateTimeExtensions.CreateNodaTime(2019, 2, 17, 0, 0, 0));

            Assert.False(included4);
        }
Exemplo n.º 4
0
 private IRange <NodaTime.Instant> createLongRange()
 {
     return(new BatchDateRange(
                DateTimeExtensions.CreateNodaTime(1, 1, 1, 1, 1, 1),
                DateTimeExtensions.CreateNodaTime(3001, 1, 1, 1, 1, 1)
                ));
 }
        public void ShouldGetHistoricalPrices()
        {
            ExchangeSharpPoloniex poloniexApi = new ExchangeSharpPoloniex();

            CandleEvent[] candleEvents = poloniexApi.GetHistoricalPrices("USDC_BTC", 300,
                                                                         DateTimeExtensions.CreateNodaTime(2019, 2, 10, 0, 0, 0),
                                                                         DateTimeExtensions.CreateNodaTime(2019, 2, 10, 12, 04, 0));
            // Uncomment in order to print all the symbols. You have to run the test with "Debug Test"
            // and go to the View: "Debug Console".
            // foreach (Event<Candle> candleEvent in candleEvents)
            // {
            //     Debug.WriteLine(String.Format("{0} - {1}", candleEvent.Date, candleEvent.Value));
            // }

            // [GlobalDate: 15497565000000000] - 3628.599, 3628.599, 3628.599, 3628.599
            var firstEvent = candleEvents[0];

            Assert.Equal(15497565000000000, firstEvent.Date);
            //Assert.Equal(DateTimeExtensions.CreateNodaTime(2019,2,10,0,0,0).ToUnixTimeTicks(), firstEvent.Date);
            var tuple = CheckCandlesEqual(new Candle(3628.599f, 3628.599f, 3628.599f, 3628.599f), firstEvent.Value);

            Assert.True(tuple.Item1, tuple.Item2);

            // [GlobalDate: 15497568000000000] - 3628.599, 3628.6, 3624.248, 3625.45
            var secondEvent = candleEvents[1];

            Assert.Equal(15497568000000000, secondEvent.Date);
            //Assert.Equal(DateTimeExtensions.CreateNodaTime(2019,2,10,0,05,0).ToUnixTimeTicks(), secondEvent.Date);
            var tuple2 = CheckCandlesEqual(new Candle(3628.599f, 3628.599f, 3628.599f, 3628.599f), secondEvent.Value);

            Assert.True(tuple2.Item1, tuple2.Item2);
        }
Exemplo n.º 6
0
        public void ShouldWriteWhenSeriesHasFirstItem_ShortDateRange()
        {
            CandleEvent[] candleEvents = new CandleEvent[] {
                // date NOT included in date range, event will NOT be added
                new CandleEvent(new Candle(11f, 34f, 12f, 15f), DateTimeExtensions.CreateNodaTime(1999, 12, 31, 0, 0, 0).ToUnixTimeTicks()),
                // date NOT included in date range, event will NOT be added
                new CandleEvent(new Candle(11f, 34f, 12f, 15f), DateTimeExtensions.CreateNodaTime(1999, 12, 31, 15, 0, 0).ToUnixTimeTicks()),
                // date included in date range, event will be added (mock that it was already written to a file)
                new CandleEvent(new Candle(33f, 34f, 12f, 15f), DateTimeExtensions.CreateNodaTime(2000, 1, 1, 0, 0, 0).ToUnixTimeTicks()),
                // date included in date range, event will be added
                new CandleEvent(new Candle(15f, 34f, 12f, 15f), DateTimeExtensions.CreateNodaTime(2000, 1, 1, 1, 0, 0).ToUnixTimeTicks()),
                // date included in date range, event will be added
                new CandleEvent(new Candle(15f, 66f, 12f, 15f), DateTimeExtensions.CreateNodaTime(2000, 1, 1, 1, 15, 0).ToUnixTimeTicks()),
                // date included in date range, event will be added
                new CandleEvent(new Candle(15f, 88f, 12f, 15f), DateTimeExtensions.CreateNodaTime(2000, 1, 1, 1, 54, 0).ToUnixTimeTicks()),
                // date NOT included in date range, event will NOT be added
                new CandleEvent(new Candle(99f, 99f, 12f, 15f), DateTimeExtensions.CreateNodaTime(2000, 1, 2, 1, 0, 0).ToUnixTimeTicks()),
            };
            string filePath = Path.Combine(Environment.CurrentDirectory, "test5.csv");

            Cleanup(filePath);

            // manually add 1st event
            using (StreamWriter sw = new StreamWriter(filePath, true))
            {
                // write to the file
                sw.WriteLine("2000-01-01T00:00:00,33,34,12,15");
            }

            var seriesWriter = new EventSavingService();
            IRange <NodaTime.Instant> oneDayRange = new BatchDateRange(
                DateTimeExtensions.CreateNodaTime(2000, 1, 1, 0, 0, 0),
                DateTimeExtensions.CreateNodaTime(2000, 1, 1, 23, 59, 59)
                );

            int addedCandles = seriesWriter.Write(candleEvents, filePath, cts.Token, oneDayRange);

            // writing should have added 4-1=3 events;
            Assert.Equal(3, addedCandles);
            Assert.True(File.Exists(filePath));
            var fileContents = File.ReadAllText(filePath);

            Assert.Contains("2000-01-01T00:00:00,33,34,12,15", fileContents);
            Assert.Contains("2000-01-01T01:15:00,15,66,12,15", fileContents);
            Assert.Contains("2000-01-01T01:54:00,15,88,12,15", fileContents);
            Assert.DoesNotContain("99,99,12,15", fileContents);
            // assert that there are 4 candle events written to file (file has 5 lines)
            Assert.Equal(5, fileContents.SplitByNewLine().Length);

            int addedCandles2 = seriesWriter.Write(candleEvents, filePath, cts.Token, oneDayRange);

            // writing again should not have added any more events
            Assert.Equal(0, addedCandles2);
            // assert that there are 4 candle events written to file (file has 5 lines) (no changes)
            Assert.Equal(5, fileContents.SplitByNewLine().Length);
            Cleanup(filePath);
        }
Exemplo n.º 7
0
        public void ShouldParseDateTime2()
        {
            string line            = "1970-01-01T00:02:00";
            string dateTimePattern = "yyyy-MM-ddTHH:mm:ss";
            var    service         = new EventSavingService();
            var    parsedDT        = service.ParseDateTime(line, dateTimePattern);

            Assert.Equal(DateTimeExtensions.CreateNodaTime(1970, 1, 1, 0, 2, 0), parsedDT);
        }
Exemplo n.º 8
0
        public void ShouldThrowExceptionWhenRangeIsTooShort()
        {
            var    range    = new BatchDateRange(DateTimeExtensions.CreateNodaTime(2019, 2, 10, 0, 0, 0), DateTimeExtensions.CreateNodaTime(2019, 2, 2, 23, 59, 59));
            Action testCode = () => { range.Verify(); };
            var    ex       = Record.Exception(testCode);

            Assert.NotNull(ex);
            Assert.IsType <ArgumentException>(ex);
            Assert.Equal("Expected end datetime - start datetime to be 1 week - 1 hour, was: -168", ex.Message);
        }
Exemplo n.º 9
0
        public void ShouldThrowExceptionWhenEndIsNotAt235959()
        {
            var    range    = new BatchDateRange(DateTimeExtensions.CreateNodaTime(2019, 2, 10, 0, 0, 0), DateTimeExtensions.CreateNodaTime(2019, 2, 16, 23, 59, 58));
            Action testCode = () => { range.Verify(); };
            var    ex       = Record.Exception(testCode);

            Assert.NotNull(ex);
            Assert.IsType <ArgumentException>(ex);
            Assert.Equal("Expected end dateTime to be HH:MM:SS 23:59:59, was: 23:59:58", ex.Message);
        }
Exemplo n.º 10
0
        public void ShouldThrowExceptionWhenEndIsNotSaturday()
        {
            var    range    = new BatchDateRange(DateTimeExtensions.CreateNodaTime(2019, 2, 10, 0, 0, 0), DateTimeExtensions.CreateNodaTime(2019, 2, 15, 23, 59, 59));
            Action testCode = () => { range.Verify(); };
            var    ex       = Record.Exception(testCode);

            Assert.NotNull(ex);
            Assert.IsType <ArgumentException>(ex);
            Assert.Equal("Expected end dateTime to be Saturday, was: Friday", ex.Message);
        }
        public void ShouldCreateNodaTime()
        {
            var nodaTime = DateTimeExtensions.CreateNodaTime(1970, 1, 1, 0, 0, 0);

            Assert.Equal(new DateTime(1970, 1, 1, 0, 0, 0), nodaTime.ToDateTime());

            var nodaTime1 = DateTimeExtensions.CreateNodaTime(2011, 1, 3, 0, 44, 0);

            Assert.Equal(new DateTime(2011, 1, 3, 0, 44, 0), nodaTime1.ToDateTime());
        }
Exemplo n.º 12
0
        public void ShouldThrowExceptionWhenStartIsNotMidnight()
        {
            var range = new BatchDateRange(
                DateTimeExtensions.CreateNodaTime(2019, 2, 10, 0, 1, 0), DateTimeExtensions.CreateNodaTime(2019, 2, 16, 23, 59, 59));
            Action testCode = () => { range.Verify(); };
            var    ex       = Record.Exception(testCode);

            Assert.NotNull(ex);
            Assert.IsType <ArgumentException>(ex);
            Assert.Equal("Expected start dateTime to be HH:MM:SS 00:00:00, was: 00:01:00", ex.Message);
        }
Exemplo n.º 13
0
        public void ShouldReturnNextBatchDateRange()
        {
            var batchdr = new BatchDateRange(DateTimeExtensions.CreateNodaTime(2019, 2, 10, 0, 0, 0), DateTimeExtensions.CreateNodaTime(2019, 2, 16, 23, 59, 59));
            var next    = batchdr.GetNext();

            Assert.Equal(DateTimeExtensions.CreateNodaTime(2019, 2, 17, 0, 0, 0), next.Start);
            Assert.Equal(DateTimeExtensions.CreateNodaTime(2019, 2, 23, 23, 59, 59), next.End);
            var next2 = next.GetNext();

            Assert.Equal(DateTimeExtensions.CreateNodaTime(2019, 2, 24, 0, 0, 0), next2.Start);
            Assert.Equal(DateTimeExtensions.CreateNodaTime(2019, 3, 2, 23, 59, 59), next2.End);
        }
        public void ShouldGetWiderBatchDateRange()
        {
            var range = new BatchDateRange(
                DateTimeExtensions.CreateNodaTime(2019, 2, 10, 0, 0, 0),
                DateTimeExtensions.CreateNodaTime(2019, 2, 16, 23, 59, 59));

            var service    = new BatchNumberService();
            var widerRange = service.GetWiderBatchDateRange(range);

            Assert.Equal(DateTimeExtensions.CreateNodaTime(2019, 2, 9, 23, 45, 0), widerRange.Start);
            Assert.Equal(DateTimeExtensions.CreateNodaTime(2019, 2, 17, 0, 14, 59), widerRange.End);
        }
        public void ShouldReturnValidBatchNumberToBeDownloaded()
        {
            var service     = new BatchNumberService();
            int batchNumber = service.GetBatchNumberToBeDownloaded(DateTimeExtensions.CreateNodaTime(2000, 2, 19, 1, 1, 1));

            Assert.Equal(0, batchNumber);
            // the first batch data range has not yet passed
            int batchNumber0 = service.GetBatchNumberToBeDownloaded(DateTimeExtensions.CreateNodaTime(2019, 2, 16, 23, 59, 59));

            Assert.Equal(0, batchNumber0);

            // batch1 == DateTimeExtensions.CreateNodaTime(2019,2,10,0,0,0), DateTimeExtensions.CreateNodaTime(2019,2,16,23,59,59), so dates that will
            // result in getting this batch are: DateTimeExtensions.CreateNodaTime(2019,2,17,0,0,0) to DateTimeExtensions.CreateNodaTime(2019,2,23,23,59,59)
            int batchNumber1a = service.GetBatchNumberToBeDownloaded(DateTimeExtensions.CreateNodaTime(2019, 2, 17, 0, 0, 0));

            Assert.Equal(1, batchNumber1a);
            int batchNumber1 = service.GetBatchNumberToBeDownloaded(DateTimeExtensions.CreateNodaTime(2019, 2, 19, 1, 1, 1));

            Assert.Equal(1, batchNumber1);
            int batchNumber2 = service.GetBatchNumberToBeDownloaded(DateTimeExtensions.CreateNodaTime(2019, 2, 23, 1, 1, 1));

            Assert.Equal(1, batchNumber2);
            int batchNumber2a = service.GetBatchNumberToBeDownloaded(DateTimeExtensions.CreateNodaTime(2019, 2, 23, 23, 59, 59));

            Assert.Equal(1, batchNumber2a);

            // batch2 == DateTimeExtensions.CreateNodaTime(2019,2,17,0,0,0), DateTimeExtensions.CreateNodaTime(2019,2,23,23,59,59)
            // result in getting this batch are: DateTimeExtensions.CreateNodaTime(2019,2,24,0,0,0) to DateTimeExtensions.CreateNodaTime(2019,3,2,23,59,59)
            int batchNumber3 = service.GetBatchNumberToBeDownloaded(DateTimeExtensions.CreateNodaTime(2019, 2, 24, 1, 1, 1));

            Assert.Equal(2, batchNumber3);
            int batchNumber4 = service.GetBatchNumberToBeDownloaded(DateTimeExtensions.CreateNodaTime(2019, 2, 25, 1, 1, 1));

            Assert.Equal(2, batchNumber4);

            int batchNumber5 = service.GetBatchNumberToBeDownloaded(DateTimeExtensions.CreateNodaTime(2019, 3, 4, 1, 1, 1));

            Assert.Equal(3, batchNumber5);
        }
        public void ShouldReturnValidDataRange()
        {
            var service = new BatchNumberService();
            var range   = service.GetDataRange(1);

            Assert.Equal(DateTimeExtensions.CreateNodaTime(2019, 2, 10, 0, 0, 0), range.Start);
            Assert.Equal(DateTimeExtensions.CreateNodaTime(2019, 2, 16, 23, 59, 59), range.End);

            var range1 = service.GetDataRange(2);

            Assert.Equal(DateTimeExtensions.CreateNodaTime(2019, 2, 17, 0, 0, 0), range1.Start);
            Assert.Equal(DateTimeExtensions.CreateNodaTime(2019, 2, 23, 23, 59, 59), range1.End);

            var range2 = service.GetDataRange(3);

            Assert.Equal(DateTimeExtensions.CreateNodaTime(2019, 2, 24, 0, 0, 0), range2.Start);
            Assert.Equal(DateTimeExtensions.CreateNodaTime(2019, 3, 2, 23, 59, 59), range2.End);

            var range12 = service.GetDataRange(12);

            Assert.Equal(DateTimeExtensions.CreateNodaTime(2019, 4, 28, 0, 0, 0), range12.Start);
            Assert.Equal(DateTimeExtensions.CreateNodaTime(2019, 5, 4, 23, 59, 59), range12.End);
        }
Exemplo n.º 17
0
        public void ShouldInvokeExchangeWithCorrectDateRanges()
        {
            string           seriesDir       = Path.Combine(Environment.CurrentDirectory, "tests", "ShouldInvokeExchangeWithCorrectDateRanges");
            Mock <IExchange> exchangeService = new Mock <IExchange>(MockBehavior.Loose);
            var batchNumberService           = new BatchNumberService();
            var filePathService = new FilePathService();

            CancellationTokenSource    cts           = new CancellationTokenSource();
            Mock <IEventSavingService> savingService = new Mock <IEventSavingService>(MockBehavior.Loose);
            var downloader = new SingleInstrumentDownloadService(exchangeService.Object,
                                                                 batchNumberService, filePathService, savingService.Object);
            var service = new HistoricalPricesDownloadService(downloader);

            var instruments = new string[] { "BTC_USD", "ETC_USD" };

            service.DownloadAndSave(instruments, 12, seriesDir, cts.Token);

            exchangeService.Verify(
                g => g.GetHistoricalPrices("BTC_USD", 300,
                                           // batch12 start - 15 minutes
                                           DateTimeExtensions.CreateNodaTime(2019, 4, 27, 23, 45, 0),
                                           // batch12 end + 15 minutes
                                           DateTimeExtensions.CreateNodaTime(2019, 5, 5, 0, 14, 59)
                                           ),
                Times.Exactly(1)
                );
            exchangeService.Verify(
                g => g.GetHistoricalPrices("ETC_USD", 300,
                                           DateTimeExtensions.CreateNodaTime(2019, 4, 27, 23, 45, 0),
                                           DateTimeExtensions.CreateNodaTime(2019, 5, 5, 0, 14, 59)
                                           ),
                Times.Exactly(1)
                );
            exchangeService.VerifyNoOtherCalls();
            Assert.False(File.Exists(Path.Combine(seriesDir, "12", "BTC_USD.csv")));
        }