public static List <Quote> MinQuotesFromTr3(string tr2FileName, Dictionary <int, TimeSpan> tr2TimeIndex) { if (!tr2FileName.EndsWith("tr3") || !File.Exists(tr2FileName)) { return(null); } FileInfo fileInfo = new FileInfo(tr2FileName); DateTime date = fileInfo.LastWriteTime.Date; if ((fileInfo.Length - 16) % PoboTr3Structure.Size != 0) { Console.WriteLine("File length is not expected: " + fileInfo.Length.ToString()); return(null); } else if ((fileInfo.Length - 16) / PoboTr3Structure.Size > tr2TimeIndex.Count + 1) { Console.WriteLine("The time index contains less items than the actual tr2 file."); return(null); } List <Quote> result = new List <Quote>(); using (FileStream fs = new FileStream(tr2FileName, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[fs.Length - 16]; fs.Read(buffer, 0, 16); int readed = fs.Read(buffer, 0, buffer.Length); fs.Close(); if (readed != buffer.Length) { throw new IOException("Failed to read all data!"); } PoboTr3Structure[] rawRecords = PoboDataImporter.BytesToStructures <PoboTr3Structure>(buffer); for (int i = 1; i < rawRecords.Length; i++) { if (rawRecords[i].High == 0 || rawRecords[i].Low == 0) { continue; } Quote minQuote = new Quote(RecordType.MinuteRecord, date + tr2TimeIndex[i - 1], rawRecords[i].Open, rawRecords[i].High, rawRecords[i].Low, rawRecords[i].Close); result.Add(minQuote); } return(result); } }
public PoboDayFileSummary(string name, string fileName) { Name = name; FileInfo info = new FileInfo(fileName); int fileSize = (int)info.Length; if (fileSize % PoboBarStructure.Size != 0) { Console.WriteLine("Wrong file size of " + fileSize.ToString()); return; } byte[] buffer1 = new byte[PoboBarStructure.Size]; byte[] buffer2 = new byte[PoboBarStructure.Size]; FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); buffer1 = br.ReadBytes(PoboBarStructure.Size); fs.Seek(-PoboBarStructure.Size, SeekOrigin.End); buffer2 = br.ReadBytes(PoboBarStructure.Size); br.Close(); fs.Close(); PoboBarStructure first = PoboDataImporter.BytesToStructures <PoboBarStructure>(buffer1)[0]; PoboBarStructure last = PoboDataImporter.BytesToStructures <PoboBarStructure>(buffer2)[0]; Since = first.Time.Date; Until = last.Time.Date; ItemsCount = fileSize / PoboBarStructure.Size; TotalDays = (int)(Until - Since).TotalDays + 1; IsAlive = true; }
public static Quote LastDayQuote(string dayFileName) { string tr3FileName = dayFileName.Replace("da1", "tr3").Replace("Day", "Tick"); if (!File.Exists(tr3FileName)) { return(null); } FileInfo fileInfo = new FileInfo(tr3FileName); DateTime date = fileInfo.LastAccessTime.Date; if ((fileInfo.Length - 16) % PoboTr3Structure.Size != 0) { Console.WriteLine("File length is not expected: " + fileInfo.Length.ToString()); return(null); } double open = -1, low = -1, high = -1, close = -1; using (FileStream fs = new FileStream(tr3FileName, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[fs.Length - 16]; fs.Read(buffer, 0, 16); int readed = fs.Read(buffer, 0, buffer.Length); fs.Close(); if (readed != buffer.Length) { throw new IOException("Failed to read all data!"); } PoboTr3Structure[] rawRecords = PoboDataImporter.BytesToStructures <PoboTr3Structure>(buffer); //int next = 0; //if (rawRecords[0].Open != 0) //{ // open = rawRecords[0].Open; // low = rawRecords[0].Low != 0 ? rawRecords[0].Low : open; // high = rawRecords[0].High != 0 ? rawRecords[0].High : open; // close = rawRecords[0].Close; // next = 1; //} //else //{ // open = rawRecords[1].Open; // low = rawRecords[1].Low != 0 ? rawRecords[1].Low : open; // high = rawRecords[1].High != 0 ? rawRecords[1].High : open; // close = rawRecords[1].Close != 0 ? rawRecords[1].Close : open; // next = 2; //} for (int i = 0; i < rawRecords.Length; i++) { if (rawRecords[i].Low == 0 || (rawRecords[i].Open == 0)) { continue; } if (open == -1 && rawRecords[i].Open != 0) { open = rawRecords[i].Open; } if (low == -1) { low = rawRecords[i].Low; } else { low = Math.Min(low, rawRecords[i].Low); } if (rawRecords[i].High != 0) { high = Math.Max(high, rawRecords[i].High); } if (rawRecords[i].Close != 0) { close = rawRecords[i].Close; } } //PoboTr2Structure last = rawRecords[rawRecords.Length - 1]; //if (last.Close != 0) //{ // close = last.Close; //} return(new Quote(date, open, high, low, close)); } }