Esempio n. 1
0
        public void Add(KLineType type, string stockCode, IEnumerable<IStockKLine> kLines)
        {
            ThrowIfTypeNotSupport(type);

            if(type == KLineType.Day)
            {
                string dbFilePath = new Day1KLineFile(stockCode).GetFilePath();
                KLineRepository repository = new KLineRepository(dbFilePath);
                repository.AddRange(kLines);
            }
            else
            {
                Year1KLineFile file = null;
                if(type == KLineType.Min1)
                {
                    file = new Min1KLineFile(stockCode);
                }
                else
                {
                    file = new Min5KLineFile(stockCode);
                }
                var packages = file.SplitToPackages(kLines);

                // 插入所有数据
                foreach (var package in packages)
                {
                    // 获取数据文件路径
                    string dbFilePath = file.GetFilePath(package);

                    KLineRepository repository = new KLineRepository(dbFilePath);
                    repository.AddRange(package.Items);
                }
            }
        }
Esempio n. 2
0
        public void TestMethod_Min5()
        {
            string stockCode_600036 = "600036";
            string stockCode_000400 = "000400";
            DateTime startTime = new DateTime(2015, 1, 1);
            DateTime endTime = new DateTime(2015, 1, 1);
            List<IStockKLine> insertDatas = ExampleStockKLineMin5(startTime, endTime).ToList();

            //var kLineFileInfos_600036 = DataFiles.GetKLineFileInfo(KLineType.Min5, stockCode_600036, startTime, endTime);
            //List<string> dataFiles_600036 = kLineFileInfos_600036.Select(p => p.FullPath).ToList();
            List<string> dataFiles_600036 = new Min5KLineFile(stockCode_600036).GetFilePath(startTime, endTime).ToList();
            CleanupFiles(dataFiles_600036);

            //var kLineFileInfos_000400 = DataFiles.GetKLineFileInfo(KLineType.Min5, stockCode_000400, startTime, endTime);
            //List<string> dataFiles_000400 = kLineFileInfos_000400.Select(p => p.FullPath).ToList();
            List<string> dataFiles_000400 = new Min5KLineFile(stockCode_000400).GetFilePath(startTime, endTime).ToList();
            CleanupFiles(dataFiles_000400);

            var appService = new KLineAppService();

            #region// 测试插入数据
            appService.Add(KLineType.Min5, stockCode_600036, insertDatas);
            Assert.IsTrue(appService.Exists(KLineType.Min5, stockCode_600036, insertDatas[insertDatas.Count - 1]));

            int count = insertDatas.Count/2;
            for (int i = 0; i < count; i++)
            {
                appService.Add(KLineType.Min5, stockCode_000400, insertDatas[i]);
            }
            Assert.IsTrue(appService.Exists(KLineType.Min5, stockCode_000400, insertDatas[count - 1]));
            #endregion

            #region// 测试更新数据
            List<StockKLineDto> kLineUpdates = GetKLineUpdates(insertDatas);
            //此种方式实在慢的可以
            //appService.Update(KLineType.Min5, stockCode_600036, kLineUpdates);
            //Assert.IsTrue(appService.Exists(KLineType.Min5, stockCode_600036, kLineUpdates[kLineUpdates.Count - 1]));
            appService.Update(KLineType.Min5, stockCode_600036, kLineUpdates[0]);
            Assert.IsTrue(appService.Exists(KLineType.Min5, stockCode_600036, kLineUpdates[kLineUpdates.Count - 1]));

            StockKLineDto kLineUpdate = kLineUpdates[0];
            kLineUpdate.Open = 0;
            appService.Update(KLineType.Min5, stockCode_000400, kLineUpdate);
            kLineUpdate.Close = 100;
            appService.Update(KLineType.Min5, stockCode_000400, kLineUpdate);
            Assert.IsTrue(appService.Exists(KLineType.Min5, stockCode_000400, kLineUpdate));
            #endregion

            #region// 测试读取数据
            var securities_600036 = appService.Get(KLineType.Min5, stockCode_600036, insertDatas[0].Time, insertDatas[insertDatas.Count - 1].Time).ToList();
            Assert.IsNotNull(securities_600036);
            Assert.IsTrue(securities_600036.Count == insertDatas.Count);
            Assert.AreEqual(kLineUpdates[0].Low, securities_600036[0].Low);

            var securities_000400 = appService.Get(KLineType.Min5, stockCode_000400, insertDatas[0].Time, insertDatas[count - 1].Time).ToList();
            Assert.IsNotNull(securities_000400);
            Assert.IsTrue(securities_000400.Count == count);
            Assert.AreEqual(kLineUpdate.Open, securities_000400[0].Open);
            Assert.AreEqual(kLineUpdate.Close, securities_000400[0].Close);
            #endregion

            #region // 测试删除数据
            appService.Delete(KLineType.Min5, stockCode_600036, insertDatas);
            var securities_600036_afterDeleted = appService.Get(KLineType.Min5, stockCode_600036, insertDatas[0].Time, insertDatas[insertDatas.Count - 1].Time).ToList();
            Assert.IsNotNull(securities_600036_afterDeleted);
            Assert.IsTrue(securities_600036_afterDeleted.Count == 0);
            #endregion
        }
Esempio n. 3
0
        public IEnumerable<IStockKLine> Get(
            KLineType type, string stockCode, 
            DateTime startTime, DateTime endTime)
        {
            ThrowIfTypeNotSupport(type);

            /*
            注意需要考虑到:
            1:如果查询的时间跨度很长,记录可能存放于不同的文件中,需要进行查询结果的拼接。
            2:同理在获取Context,以及FilePath的时候,也需要考虑时间跨度导致的多文件处理。
            */
            List<IStockKLine> result = new List<IStockKLine>();

            if (type == KLineType.Day)
            {
                string dbFilePath = new Day1KLineFile(stockCode).GetFilePath();
                KLineRepository repository = new KLineRepository(dbFilePath);
                result.AddRange(repository.Get(startTime, endTime));
            }
            else
            {
                Year1KLineFile file = null;
                if (type == KLineType.Min1)
                {
                    file = new Min1KLineFile(stockCode);
                }
                else
                {
                    file = new Min5KLineFile(stockCode);
                }
                var files = file.GetFilePath(startTime, endTime);

                // 插入所有数据
                foreach (var dbFilePath in files)
                {
                    // 获取数据文件路径
                    KLineRepository repository = new KLineRepository(dbFilePath);
                    result.AddRange(repository.Get(startTime, endTime));
                }
            }

            return result;
        }
Esempio n. 4
0
        public bool Exists(KLineType type, string stockCode, IStockKLine kLine)
        {
            ThrowIfTypeNotSupport(type);

            // 获取数据文件路径
            string dbFilePath = null;
            if(type == KLineType.Day)
            {
                dbFilePath = new Day1KLineFile(stockCode).GetFilePath();
            }
            else if(type == KLineType.Min1)
            {
                dbFilePath = new Min1KLineFile(stockCode).GetFilePath(kLine.Time);
            }
            else if(type == KLineType.Min5)
            {
                dbFilePath = new Min5KLineFile(stockCode).GetFilePath(kLine.Time);
            }

            if (!string.IsNullOrEmpty(dbFilePath))
            {
                KLineRepository repository = new KLineRepository(dbFilePath);
                return repository.Exists(kLine);
            }
            else
            {
                return false;
            }
        }