public static string PrintEntryData(EntryData e,bool includeHeader=false) { StringBuilder sb = new StringBuilder(); if(includeHeader)sb.Append("time,price,change,share,money,type"); sb.Append(String.Format("{0},{1},{2},{3},{4},{5}",e.time,e.price,e.money,e.change,e.share,e.type)); #if DEBUG Console.WriteLine(sb.ToString()); #endif return sb.ToString(); }
/// <summary> /// 分析某一天的数据 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static DailyData ReadCsv(string fileName,bool isDownload) { DailyData data = new DailyData(); data.entryList = new List<EntryData>(); FileInfo fi = new FileInfo(fileName); string fName = fi.Name; //data.stock = StockUtil.RetrieveStock(fName); string date = StockUtil.RetrieveDate(fName); data.Date = DateTime.Parse(date); // open the file "data.csv" which is a CSV file with headers using (CsvReader csv = new CsvReader( new StreamReader(fileName), true)) { int index = 0; int fieldCount = csv.FieldCount; string[] headers = csv.GetFieldHeaders(); decimal current; //颠倒记录 IEnumerable<string[]> hi= csv.Reverse<string[]>(); //foreach(String[] a in hi) //{ // Console.WriteLine(a[0]); //} //for(int i=0;i<hi.;i++) //{ // Console.WriteLine(csv[i,0]); //} //while (csv.ReadNextRecord()) //{ //Console.WriteLine(hi.Count<string[]>()); try { foreach (String[] record in hi) { EntryData dd = new EntryData(); current = Decimal.Parse(record[1].Replace("\0", "")); if (index == 0) { data.ClosePrice = current; data.HighestPrice = current; data.LowestPrice = current; } if (data.HighestPrice < current) { data.HighestPrice = current; data.TimeWhenHighest = DateTime.Parse(date + " " + record[0]); } if (data.LowestPrice > current) { data.LowestPrice = current; data.TimeWhenLowest = DateTime.Parse(date + " " + record[0]); } dd.time = DateTime.Parse(date + " " + record[0]); dd.price = current; dd.change = Decimal.Parse(record[2]); dd.share = decimal.Parse(record[3]); dd.money = decimal.Parse(record[4]); dd.type = record[5]; data.entryList.Add(dd); data.OpenPrice = current; index++; } } catch { ///TODO 吞掉异常 /// StockLog.Log.Info("Can't parse at line " + index + " " + fileName); } //} //LOG.Info(fileName+" "+fieldCount + " " + index); } return data; }