Exemple #1
0
        public void TestKLineReadAndWrite()
        {
            StockKLine insertData = new StockKLine()
            {
                Amount = 123214124,
                Close = 19.96,
                Open = 19.05,
                High = 19.98,
                Low = 19.01,
                Time = DateTime.Now.Date,
                Volume = 32342352352
            };

            string fileName = "600036_2015.sdf";
            string fullPath = Path.Combine(directory, fileName);

            // Add
            using (IRepositoryContext context = ContextFactory.Create(ContextType.KLine, fullPath))
            {
                var repository = new Repository<StockKLine>(context);
                repository.Add(insertData);
                repository.UnitOfWork.Commit();
            }

            // Read
            StockKLine readData;
            using (IRepositoryContext context = ContextFactory.Create(ContextType.KLine, fullPath))
            {
                var repository = new Repository<StockKLine>(context);
                readData = repository.Get(insertData.Time);
            }
            Assert.IsNotNull(readData);
            Assert.AreEqual(insertData.Time, readData.Time);

            // update
            StockKLine updatedData = readData;
            updatedData.Open = 100;
            using (IRepositoryContext context = ContextFactory.Create(ContextType.KLine, fullPath))
            {
                var repository = new Repository<StockKLine>(context);
                repository.Update(updatedData);
                repository.UnitOfWork.Commit();
            }
            // Read
            using (IRepositoryContext context = ContextFactory.Create(ContextType.KLine, fullPath))
            {
                var repository = new Repository<StockKLine>(context);
                readData = repository.Get(insertData.Time);
            }
            Assert.IsTrue(readData.Open - 100 < 0.0000001);
        }
        public static IStockKLine Combine(this ITimeSeriesPackage<IStockKLine> self)
        {
            StockKLine outputData = new StockKLine
            {
                Time = self.Zone.EndTime,

                Open = self.Items.First().Open,
                Close = self.Items.Last().Close,

                Volume = self.Items.Sum(p => p.Volume),
                Amount = self.Items.Sum(p => p.Amount),

                High = self.Items.Max(p => p.High),
                Low = self.Items.Min(p => p.Low),
            };

            return outputData;
        }
Exemple #3
0
        public static StockKLine ToDataObject(this IStockKLine self)
        {
            StockKLine outputData = new StockKLine
            {
                //
                // 摘要:
                //     成交额
                Amount = self.Amount,
                //
                // 摘要:
                //     收盘
                Close = self.Close,
                //
                // 摘要:
                //     最高
                High = self.High,
                //
                // 摘要:
                //     最低
                Low = self.Low,
                //
                // 摘要:
                //     今开
                Open = self.Open,
                //
                // 摘要:
                //     日期与时间
                Time = self.Time,
                //
                // 摘要:
                //     成交量
                Volume = self.Volume
            };

            return outputData;
        }
Exemple #4
0
        private static StockKLine CreateRandomItem(DateTime tradingTime, double preClose, double preVolume, int digits = 2)
        {
            // 计算涨停和跌停价格
            double upLimit = PriceLimit.UpLimit(SecurityType.Sotck, preClose);
            double downLimit = PriceLimit.DownLimit(SecurityType.Sotck, preClose);

            var kLine = new StockKLine();
            kLine.Time = tradingTime;
            kLine.Volume = (int)_random.Next(preVolume * 0.5, preVolume * 1.5);

            // 随机涨跌平
            UpOrDown upOrDown = GetDirection(preClose);
            if (upOrDown == UpOrDown.Down)// 跌
            {
                // 3%的机会一字跌
                if (_random.Next(0, 100) > 96)
                {
                    kLine.Open = downLimit;
                    kLine.Close = downLimit;
                }
                else if (_random.Next(0, 100) > 90) //  10%机会跌停板
                {
                    kLine.Close = downLimit;
                    kLine.Open = _random.Next(downLimit, preClose, digits);
                }
                else
                {
                    kLine.Open = _random.Next(downLimit, preClose, digits);
                    kLine.Close = _random.Next(downLimit, preClose, digits);
                }
            }
            else if (upOrDown == UpOrDown.Up)// 涨
            {
                // 3%的机会一字涨
                if (_random.Next(0, 100) > 96)
                {
                    kLine.Open = upLimit;
                    kLine.Close = upLimit;
                }
                else if (_random.Next(0, 100) > 90) //  10%机会涨停板
                {
                    kLine.Close = upLimit;
                    kLine.Open = _random.Next(preClose, upLimit, digits);
                }
                else
                {
                    kLine.Open = _random.Next(preClose, upLimit, digits);
                    kLine.Close = _random.Next(preClose, upLimit, digits);
                }
            }
            else
            {
                kLine.Open = _random.Next(downLimit, upLimit, digits);
                kLine.Close = _random.Next(downLimit, upLimit, digits);
            }

            kLine.High = _random.Next(kLine.Open, upLimit, digits);
            kLine.Low = _random.Next(downLimit, kLine.Open, digits);

            // 随机一个均价,求出成交额
            double average = _random.Next(kLine.Low, kLine.High, digits);
            kLine.Amount = average * kLine.Volume;

            return kLine;
        }
        public IEnumerable<IStockKLine> GetAll()
        {
            List<StockKLine> result = new List<StockKLine>();
            string sql = string.Format("SELECT * FROM {0}", tableName);

            using (SqlCeConnection conn = new SqlCeConnection(ConnectionString))
            {
                conn.Open();

                using (SqlCeCommand cmd = new SqlCeCommand(sql, conn))
                {
                    SqlCeDataReader reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        StockKLine dbo = new StockKLine
                        {
                            Amount = double.Parse(reader[colAmount].ToString().Trim()),
                            Close = double.Parse(reader[colClose].ToString().Trim()),
                            High = double.Parse(reader[colHigh].ToString().Trim()),
                            Low = double.Parse(reader[colLow].ToString().Trim()),
                            Open = double.Parse(reader[colOpen].ToString().Trim()),
                            Time = DateTime.Parse(reader[colTime].ToString().Trim()),
                            Volume = double.Parse(reader[colVolume].ToString().Trim())
                        };
                        result.Add(dbo);
                    }

                    reader.Close();
                }

                conn.Close();
            }

            return result;
        }
        public IEnumerable<IStockKLine> Get(DateTime startTime, DateTime endTime)
        {
            List<StockKLine> result = new List<StockKLine>();
            string sql =
                string.Format("SELECT * FROM {0} WHERE {1}>='{2}' AND {1}<='{3}'",
                tableName,
                colTime,
                startTime.ToString("yyyy-MM-dd HH:mm:ss"),
                endTime.ToString("yyyy-MM-dd HH:mm:ss"));

            using (SqlCeConnection conn = new SqlCeConnection(ConnectionString))
            {
                conn.Open();

                using (SqlCeCommand cmd = new SqlCeCommand(sql, conn))
                {
                    SqlCeDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        StockKLine dbo = new StockKLine
                        {
                            Amount = double.Parse(reader[colAmount].ToString().Trim()),
                            Close = double.Parse(reader[colClose].ToString().Trim()),
                            High = double.Parse(reader[colHigh].ToString().Trim()),
                            Low = double.Parse(reader[colLow].ToString().Trim()),
                            Open = double.Parse(reader[colOpen].ToString().Trim()),
                            Time = DateTime.Parse(reader[colTime].ToString().Trim()),
                            Volume = double.Parse(reader[colVolume].ToString().Trim())
                        };
                        result.Add(dbo);
                    }
                    reader.Close();
                }

                conn.Close();
            }

            return result;
        }