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
            });
        }
        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));
                    }
                }
            }
        }
        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));
        }
Exemple #4
0
 static UntradableObject()
 {
     untradableObjects = untradableCodes.ToDictionary(s => StockName.NormalizeCode(s), s => new UntradableObject(s));
 }
Exemple #5
0
 public static string GetBoardIndex(StockBoard board)
 {
     return(StockName.NormalizeCode(StockBoardIndex.GetBoardIndex(board)));
 }