Пример #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);
                }
            }
        }
Пример #2
0
        public void TestMethod_KLine()
        {
            //SQLCEWrapper _sqlCeWrapper = new SQLCEWrapper();
            //_sqlCeWrapper.CreateDatabase();

            //_sqlCeWrapper.CreateKLineTable();
            string filePath = Path.Combine(Environment.CurrentDirectory, "TestKine.sdf");
            if(File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            DateTime startTime = new DateTime(2015, 1, 1);
            DateTime endTime = new DateTime(2015, 12, 31);
            List<IStockKLine> kLines = ExampleStockKLineMin1(startTime, endTime).ToList();
            System.Diagnostics.Debug.Print(string.Format("KLine data count is {0}", kLines.Count));

            KLineRepository repository = new KLineRepository(filePath);
            repository.AddRange(kLines);
            //_sqlCeWrapper.InsertIntoDatas(kLines);

            List<IStockKLine> getKLines = repository.Get(kLines[60].Time, kLines[120].Time).ToList();

            Assert.IsTrue(repository.Exists(kLines[0]));
            Assert.IsFalse(repository.Exists(GetUpdateData(kLines[0].Time.AddYears(1))));

            var updateData = GetUpdateData(kLines[0].Time);

            repository.UpdateRange(new IStockKLine[] { updateData });
            IList<IStockKLine> result0 = repository.GetAll().ToList();

            Assert.IsTrue(repository.Exists(updateData));

            List<IStockKLine> updateKLines = GetUpdateDatas(kLines).ToList();
            repository.UpdateRange(updateKLines);

            //string p_strSQL = "SELECT * FROM KLineTable;";
            //DataSet dataSet = _sqlCeWrapper.SelectDataSet(p_strSQL);
            IList<IStockKLine> result = repository.GetAll().ToList();

            //if (dataSet != null && dataSet.Tables.Count > 0)
            //{
            //    foreach (DataTable table in dataSet.Tables)
            //        System.Diagnostics.Debug.Print(string.Format("table Rows count is {0}", table.Rows.Count));
            //}
            if(result != null && result.Count > 0)
            {
                System.Diagnostics.Debug.Print(string.Format("KLine Data count is {0}", result.Count));
            }
            Assert.AreEqual(kLines.Count, result.Count);

            //_sqlCeWrapper.DeleteDatabase();
        }
Пример #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;
        }
Пример #4
0
        public DateTime? GetLastTradeDate(KLineType type, string stockCode)
        {
            ThrowIfTypeNotSupport(type);

            if (type == KLineType.Day)
            {
                string dbFilePath = new Day1KLineFile(stockCode).GetFilePath();
                KLineRepository repository = new KLineRepository(dbFilePath);
                return repository.GetLastTradeDate();
            }
            else
            {
                throw new NotSupportedException();
            }
        }
Пример #5
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;
            }
        }