コード例 #1
0
        private static StockBlockRelationship ParseLine(string line, StockBlockManager manager)
        {
            if (string.IsNullOrWhiteSpace(line))
            {
                return(null);
            }

            var fields = line.Split(_splitter);

            if (fields.Length != FieldCount)
            {
                return(null);
            }

            StockBlock block = manager.GetStockBlockById(fields[2]);

            if (block == null)
            {
                return(null);
            }

            return(new StockBlockRelationship
            {
                StockCode = StockName.NormalizeCode(fields[1]),
                BlockName = block.Name
            });
        }
コード例 #2
0
        private static string NormalizeCode(string code)
        {
            StockExchangeId exchangeId = StockName.GetExchangeId(code);

            string prefix = exchangeId == StockExchangeId.ShenzhenExchange ? "sz" : "sh";

            return(prefix + code);
        }
コード例 #3
0
        public TdxBinaryBlockDataReader(string file)
        {
            if (string.IsNullOrWhiteSpace(file))
            {
                throw new ArgumentNullException();
            }

            using (BinaryReader reader = new BinaryReader(new FileStream(file, FileMode.Open, FileAccess.Read)))
            {
                // skip header
                reader.ReadBytes(HeaderSize);

                // read the number of blocks
                int blockNumber = reader.ReadInt16();
                if (blockNumber <= 0)
                {
                    throw new InvalidDataException("block number is not positive number");
                }

                for (int blockIndex = 0; blockIndex < blockNumber; ++blockIndex)
                {
                    // read block name
                    var rawBlockName = reader.ReadBytes(BlockNameSize);
                    var blockName    = ConvertRawBytesToString(rawBlockName);

                    // read # of stocks in the block
                    var stockNumber = reader.ReadInt16();

                    if (stockNumber > MaxNumberOfStockInBlock)
                    {
                        throw new InvalidDataException("stock number in block exceeds limit");
                    }

                    // skip the block level
                    reader.ReadInt16();

                    // now read stock code
                    for (int stockIndex = 0; stockIndex < stockNumber; ++stockIndex)
                    {
                        var rawCodes  = reader.ReadBytes(StockCodeSize);
                        var stockCode = ConvertRawBytesToString(rawCodes);

                        _relationships.Add(
                            new StockBlockRelationship()
                        {
                            StockCode = StockName.NormalizeCode(stockCode),
                            BlockName = blockName
                        });
                    }

                    // skip empty spaces
                    if (stockNumber < MaxNumberOfStockInBlock)
                    {
                        reader.ReadBytes(StockCodeSize * (MaxNumberOfStockInBlock - stockNumber));
                    }
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Create stock name table from file
        /// </summary>
        /// <param name="fileName">file that contains the stock names.
        /// The file must be UTF-8 encoded, and each line is formated as:
        /// code|name1|name2|...
        /// </param>
        /// <param name="onError">function to handle invalid input line in the file. default value is null.
        /// if the value is null, exception will be thrown out when invalid input line is encounted,
        /// otherwise, the invalid input line will be passed to the function.
        ///
        /// if the function returns false, no more input lines will be read from file.
        /// </param>
        public StockNameTable(string fileName, Func <string, bool> onError = null)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }

            using (StreamReader reader = new StreamReader(fileName, Encoding.UTF8))
            {
                HashSet <string> existingCodes = new HashSet <string>();

                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    if (string.IsNullOrWhiteSpace(line))
                    {
                        continue;
                    }

                    StockName stockName = null;
                    try
                    {
                        stockName = new StockName(line);

                        // avoid duplicated stock name (two stocks are treated as duplicated iff. their code are the same)
                        if (existingCodes.Add(stockName.Code))
                        {
                            _stockNames.Add(stockName);
                        }
                    }
                    catch
                    {
                        if (onError == null)
                        {
                            throw;
                        }
                        else
                        {
                            bool continueProcess = onError(line);
                            if (!continueProcess)
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }
コード例 #5
0
        /// <summary>
        /// Create stock name table from file
        /// </summary>
        /// <param name="fileName">file that contains the stock names.
        /// The file must be UTF-8 encoded, and each line is formated as:
        /// code|name1|name2|...
        /// </param>
        /// <param name="onError">function to handle invalid input line in the file. default value is null.
        /// if the value is null, exception will be thrown out when invalid input line is encounted,
        /// otherwise, the invalid input line will be passed to the function.
        ///
        /// if the function returns false, no more input lines will be read from file.
        /// </param>
        public StockNameTable(string fileName, Func <string, bool> onError = null)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }

            using (var reader = new StreamReader(fileName, Encoding.UTF8))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    if (string.IsNullOrWhiteSpace(line))
                    {
                        continue;
                    }

                    try
                    {
                        StockName stockName = StockName.Parse(line);

                        // avoid duplicated stock name (two stocks are treated as duplicated iff. their code are the same)
                        if (!ContainsStock(stockName.Code))
                        {
                            AddStock(stockName);
                        }
                    }
                    catch
                    {
                        if (onError == null)
                        {
                            throw;
                        }
                        var continueProcess = onError(line);
                        if (!continueProcess)
                        {
                            break;
                        }
                    }
                }
            }
        }
コード例 #6
0
ファイル: StockName.cs プロジェクト: wukan1986/StockAnalysis
        public static StockName Parse(string stockName)
        {
            if (string.IsNullOrWhiteSpace(stockName))
            {
                throw new ArgumentNullException("stockName");
            }

            var fields = stockName.Trim().Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            if (fields == null || fields.Length == 0)
            {
                throw new FormatException(string.Format("stock name [{0}] is invalid", stockName));
            }

            var name = new StockName
            {
                Code  = NormalizeCode(fields[0]),
                Names = fields.Length > 1 ? fields.Skip(1).ToArray() : new[] { string.Empty }
            };

            return(name);
        }
コード例 #7
0
        public static StockHistoryData LoadFromFile(
            string file,
            DateTime startDate,
            DateTime endDate,
            StockNameTable nameTable = null,
            long interval            = 86400L)
        {
            if (string.IsNullOrEmpty(file))
            {
                throw new ArgumentNullException();
            }

            var inputData = Csv.Load(file, Encoding.UTF8, ",");

            if (inputData.RowCount == 0)
            {
                return(null);
            }

            var code = StockName.NormalizeCode(inputData[0][0]);

            var name =
                nameTable != null && nameTable.ContainsStock(code)
                ? nameTable[code]
                : new StockName(code, string.Empty);

            // header is code,date,open,highest,lowest,close,volume,amount

            var data = new List <Bar>(inputData.RowCount);

            var lastInvalidBarTime = DateTime.MinValue;

            foreach (var row in inputData.Rows)
            {
                var date = DateTime.Parse(row[1]);
                if (date < startDate || date > endDate)
                {
                    continue;
                }

                try
                {
                    var dailyData = new Bar
                    {
                        Time         = DateTime.Parse(row[1]),
                        OpenPrice    = double.Parse(row[2]),
                        HighestPrice = double.Parse(row[3]),
                        LowestPrice  = double.Parse(row[4]),
                        ClosePrice   = double.Parse(row[5]),
                        Volume       = double.Parse(row[6]),
                        Amount       = double.Parse(row[7])
                    };

                    if (dailyData.OpenPrice > 0.0 &&
                        dailyData.ClosePrice > 0.0 &&
                        dailyData.HighestPrice > 0.0 &&
                        dailyData.LowestPrice > 0.0)
                    {
                        if (Math.Abs(dailyData.Volume) > 1e-6)
                        {
                            data.Add(dailyData);
                        }
                    }
                    else
                    {
                        if (dailyData.Time > lastInvalidBarTime)
                        {
                            lastInvalidBarTime = dailyData.Time;
                        }
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("Wrong format: {0} in file {1}", string.Join(",", inputData.Rows), file);
                }
            }

            // remove all data that before last invalidate bar.
            var filterData = data
                             .Where(b => b.Time > lastInvalidBarTime)
                             .OrderBy(b => b.Time)
                             .ToArray();

            return(new StockHistoryData(name, interval, filterData));
        }
コード例 #8
0
 public StockHistoryData(StockName name, long intervalInSecond, Bar[] dataOrderByTime)
 {
     _name              = name;
     _intervalInSecond  = intervalInSecond;
     _dataOrderedByTime = dataOrderByTime;
 }
コード例 #9
0
 public void AddStock(StockName stock)
 {
     _stockNames.Add(stock);
 }
コード例 #10
0
 static UntradableObject()
 {
     untradableObjects = untradableCodes.ToDictionary(s => StockName.NormalizeCode(s), s => new UntradableObject(s));
 }
コード例 #11
0
ファイル: StockName.cs プロジェクト: wukan1986/StockAnalysis
 public static string GetBoardIndex(StockBoard board)
 {
     return(StockName.NormalizeCode(StockBoardIndex.GetBoardIndex(board)));
 }
コード例 #12
0
ファイル: StockName.cs プロジェクト: wukan1986/StockAnalysis
 public string GetBoardIndex()
 {
     return(StockName.GetBoardIndex(Board));
 }
コード例 #13
0
 public StockHistoryData(StockName name, long intervalInSecond, IEnumerable <Bar> data)
 {
     _name             = name;
     _intervalInSecond = intervalInSecond;
     _data             = data.ToList();
 }