public TickReader(FileInfo file) { Serializer = new PbTickSerializer(); Codec = new QuantBox.Data.Serializer.V2.PbTickCodec(); _stream = new MemoryStream(); _originalLength = (int)file.Length; // 加载文件,支持7z解压 var fileStream = file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite); { try { using (var zip = new SevenZipExtractor(fileStream)) { zip.ExtractFile(0, _stream); _stream.Seek(0, SeekOrigin.Begin); } } catch { _stream = fileStream; _stream.Seek(0, SeekOrigin.Begin); } } }
private void Do(DirectoryInfo di, SearchOption searchOption) { foreach (var f in di.GetFiles("*", searchOption)) { // 处理 string s = f.FullName.Replace(strInput, strOutput) + ".csv"; AppendText(s + " - "); if (bSkip) { if (new FileInfo(s).Exists) { AppendText("存在" + Environment.NewLine); continue; } } try { PbTickSerializer.WriteCsv(PbTickSerializer.Read(f.FullName), s); AppendText("成功" + Environment.NewLine); } catch (Exception ex) { AppendText("失败" + Environment.NewLine); } } }
private void ReadFromFile(string pathChosen) { Tuple <Stream, string, double> tuple = ReadToStream(pathChosen); if (tuple == null) { return; } Stream stream = tuple.Item1; try { listTickData = PbTickSerializer.Read(stream); strCurrentFileName = string.Format("{0} ({1}/{2}={3})", tuple.Item2, tuple.Item3, listTickData.Count(), tuple.Item3 / listTickData.Count()); ValueChanged(false); PbTickCodec Codec = new PbTickCodec(); listTickView = Codec.Data2View(this.listTickData, true); dgvTick.DataSource = this.listTickView; SingleCheck(menuView_Diff); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
public PbTickSerializer GetSerializer(string symbol) { PbTickSerializer list; if (!dict2.TryGetValue(symbol, out list)) { list = new PbTickSerializer(); dict2.Add(symbol, list); } return(list); }
public void ReadFile(int instrumentId, string path) { Bars = new BarSeries(); Trades = new TickSeries(); Asks = new TickSeries(); Bids = new TickSeries(); PbTickSerializer pts = new PbTickSerializer(); PbTick restore = null; using (Stream stream = File.OpenRead(path)) { while (true) { restore = pts.ReadOne(stream); if (restore == null) { break; } Trade t = new Trade(); t.InstrumentId = instrumentId; t.DateTime = pts.Codec.GetActionDayDateTime(restore); t.Price = pts.Codec.GetLastPrice(restore); t.Size = (int)pts.Codec.GetVolume(restore); Trades.Add(t); Bid b = new Bid(); b.InstrumentId = instrumentId; b.DateTime = t.DateTime; b.Price = pts.Codec.GetBidPrice(restore, 1); b.Size = pts.Codec.GetBidSize(restore, 1); Bids.Add(b); Ask a = new Ask(); a.InstrumentId = instrumentId; a.DateTime = t.DateTime; a.Price = pts.Codec.GetAskPrice(restore, 1); a.Size = pts.Codec.GetAskSize(restore, 1); Asks.Add(a); } stream.Close(); } }
private void menuFile_Export_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "CSV文件|*.csv"; DialogResult result = saveFileDialog.ShowDialog(); if (result == DialogResult.OK) { string pathChosen = saveFileDialog.FileName; // 将界面数据生成差分数据 ViewToDataByViewType(); PbTickSerializer.WriteCsv(this.listTickData, pathChosen); } }
private void SaveChanges() { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Portable Data Zero files (*.pd0)|*.pd0|All files (*.*)|*.*"; DialogResult result = saveFileDialog.ShowDialog(); if (result == DialogResult.OK) { string pathChosen = saveFileDialog.FileName; ViewToDataByViewType(); PbTickSerializer.Write(this.listTickData, pathChosen); ValueChanged(false); } }
public static void _Main(string[] args) { string symbol = "AAPL"; Framework framework = Framework.Current; Instrument instrument = framework.InstrumentManager.Get(symbol); DataSeries ds = framework.DataManager.GetDataSeries(instrument, DataObjectType.Trade); PbTickSerializer pts = new PbTickSerializer(); // 最关键的部分,需要提前设置 pts.Codec.Config.SetTickSize(0.01); PbTick last = new PbTick(); using (Stream stream = File.OpenWrite(@"D:\1.data")) { for (int i = 0; i < ds.Count; ++i) { var d = ds[i]; Trade t = d as Trade; PbTick tick = new PbTick(); // 必须设置数据中的TickSize tick.Config = pts.Codec.Config; pts.Codec.SetLastPrice(tick, t.Price); pts.Codec.SetVolume(tick, t.Size); pts.Codec.SetSymbol(tick, symbol); // 时间设置也很关键 pts.Codec.SetActionDay(tick, t.DateTime.Date); pts.Codec.SetUpdateTime(tick, t.DateTime - t.DateTime.Date); // 写入流 pts.Write(tick, new Stream[] { stream }); } stream.Close(); } }
public void TestReadCsvLeve3() { FileInfo fi = new FileInfo(@"d:\wukan\Desktop\DepthDataShow\20150120.txt"); //FileInfo fo = new FileInfo(@"F:\BaiduYunDownload\DepthDataShow\20150120.pd0"); PbTickSerializer pts = new PbTickSerializer(); CCC ccc = new CCC(); CCC last_ccc = new CCC(); AAA last = new AAA(); last.buy = false; string last_symbol = "XXX"; List <AAA> list = new List <AAA>(); //using (Stream stream = File.Open(@"F:\BaiduYunDownload\DepthDataShow\20150120.pd0", FileMode.Create)) { using (StreamReader file = new StreamReader(fi.OpenRead())) { int i = 0; string str; do { ++i; str = file.ReadLine(); if (str == null) { break; } string[] arr = str.Split(','); AAA a = new AAA(); a.symbol = arr[1]; a.buy = arr[2] == "0"; a.price = double.Parse(arr[3]); a.size = int.Parse(arr[4]); if (last.buy == false && a.buy == true) { // 快照的切换点,把上次的存储都取出来,进行保存 // sell里先存的数字大的,后存的数字小的,最后的是卖一 // buy里也是先存数字大的,后存数字小的,最前的买一 // foreach (var kv in ccc.dict) { kv.Value.MakeTick(); ccc.GetSerializer(kv.Key).Write(kv.Value.tick, new Stream[] { ccc.GetStream(kv.Key) }); } ccc.dict.Clear(); } BBB bbb = ccc.Get(a.symbol); bbb.symbol = a.symbol; bbb.time = arr[0]; if (bbb.symbol == "cu1502") { int nTest = 1; } if (a.buy) { bbb.AddBid(a); } else { bbb.AddAsk(a); } last = a; last_symbol = a.symbol; //if (i < 4000) // Console.WriteLine(str); //if (i > 4000) // break; } while (str != null); file.Close(); } } }
public void TestReadCsvLeve2() { FileInfo fi = new FileInfo(@"F:\BaiduYunDownload\20141225\20141225.csv"); FileInfo fo = new FileInfo(@"F:\BaiduYunDownload\20141225\20141225.pd0"); PbTickSerializer pts = new PbTickSerializer(); using (Stream stream = File.Open(@"F:\BaiduYunDownload\20141225\20141225.pd0", FileMode.Create)) { using (StreamReader file = new StreamReader(fi.OpenRead())) { int i = 0; string str = file.ReadLine(); do { ++i; str = file.ReadLine(); if (str == null) { break; } string[] arr = str.Split(','); PbTick tick = new PbTick(); pts.Codec.SetSymbol(tick, arr[0]); tick.Config = new ConfigInfo().Default(); if (arr[0].StartsWith("TF")) { tick.Config.SetTickSize(0.002); tick.Config.ContractMultiplier = 10000; } else { tick.Config.SetTickSize(0.2); tick.Config.ContractMultiplier = 300; } tick.Config.Time_ssf_Diff = 5; pts.Codec.Config = tick.Config; pts.Codec.UseFlat(false); //if(i == 1) //{ // tick.Split = new StockSplitInfo(); // tick.Split.StockDividend = 10; //} //if(i == 2) //{ // tick.Split = new StockSplitInfo(); // tick.Split.CashDividend = 10; //} tick.ActionDay = int.Parse(arr[5]); int time = int.Parse(arr[6]); tick.Time_HHmm = time / 100000; tick.Time_____ssf__ = time % 100000 / 100; tick.Time________ff = time % 100; pts.Codec.SetLastPrice(tick, double.Parse(arr[8])); pts.Codec.SetHigh(tick, double.Parse(arr[9])); pts.Codec.SetLow(tick, double.Parse(arr[10])); pts.Codec.SetVolume(tick, int.Parse(arr[11])); pts.Codec.SetTurnover(tick, int.Parse(arr[12])); pts.Codec.SetOpenInterest(tick, int.Parse(arr[16])); pts.Codec.SetAskPrice(tick, 1, double.Parse(arr[17])); pts.Codec.SetAskPrice(tick, 2, double.Parse(arr[18])); pts.Codec.SetAskPrice(tick, 3, double.Parse(arr[19])); pts.Codec.SetAskPrice(tick, 4, double.Parse(arr[20])); pts.Codec.SetAskPrice(tick, 5, double.Parse(arr[21])); pts.Codec.SetAskSize(tick, 1, int.Parse(arr[22])); pts.Codec.SetAskSize(tick, 2, int.Parse(arr[23])); pts.Codec.SetAskSize(tick, 3, int.Parse(arr[24])); pts.Codec.SetAskSize(tick, 4, int.Parse(arr[25])); pts.Codec.SetAskSize(tick, 5, int.Parse(arr[26])); pts.Codec.SetBidPrice(tick, 1, double.Parse(arr[27])); pts.Codec.SetBidPrice(tick, 2, double.Parse(arr[28])); pts.Codec.SetBidPrice(tick, 3, double.Parse(arr[29])); pts.Codec.SetBidPrice(tick, 4, double.Parse(arr[30])); pts.Codec.SetBidPrice(tick, 5, double.Parse(arr[31])); pts.Codec.SetBidSize(tick, 1, int.Parse(arr[32])); pts.Codec.SetBidSize(tick, 2, int.Parse(arr[33])); pts.Codec.SetBidSize(tick, 3, int.Parse(arr[34])); pts.Codec.SetBidSize(tick, 4, int.Parse(arr[35])); pts.Codec.SetBidSize(tick, 5, int.Parse(arr[36])); pts.Write(tick, new Stream[] { stream }); } while (str != null); file.Close(); } } }
public void TestReadCsvLeve1() { FileInfo fi = new FileInfo(@"F:\BaiduYunDownload\IF1406\IF1406.csv"); FileInfo fo = new FileInfo(@"F:\BaiduYunDownload\IF1406\IF1406.pd0"); PbTickSerializer pts = new PbTickSerializer(); pts.Codec.Config.SetTickSize(0.2); pts.Codec.Config.Time_ssf_Diff = 5; using (Stream stream = File.Open(@"F:\BaiduYunDownload\IF1406\IF1406.pd0", FileMode.Create)) { using (StreamReader file = new StreamReader(fi.OpenRead())) { int i = 0; string str = file.ReadLine(); do { ++i; str = file.ReadLine(); if (str == null) { break; } string[] arr = str.Split(','); DateTime dt = DateTime.Parse(arr[0]).AddMilliseconds(int.Parse(arr[1])); string symbol = arr[2]; double price = double.Parse(arr[3]); int vol = int.Parse(arr[4]); int openint = int.Parse(arr[5]); double bid = double.Parse(arr[6]); int bidSize = int.Parse(arr[7]); double ask = double.Parse(arr[8]); int askSize = int.Parse(arr[9]); PbTick tick = new PbTick(); tick.Config = pts.Codec.Config; pts.Codec.SetLastPrice(tick, price); pts.Codec.SetVolume(tick, vol); pts.Codec.SetSymbol(tick, symbol); pts.Codec.SetActionDay(tick, dt.Date); pts.Codec.SetUpdateTime(tick, dt - dt.Date); pts.Codec.SetBidPrice(tick, 1, bid); pts.Codec.SetBidSize(tick, 1, bidSize); pts.Codec.SetAskPrice(tick, 1, ask); pts.Codec.SetAskSize(tick, 1, askSize); pts.Write(tick, new Stream[] { stream }); //if (i == 1000000) // break; } while (str != null); file.Close(); } } Console.WriteLine("结束了"); }
static void Main2(string[] args) { //读五档行情,然后存盘 FileInfo fi = new FileInfo(@"d:\wukan\Desktop\20141225.csv"); //FileInfo fo = new FileInfo(@"d:\wukan\Desktop\20141225.pd0"); PbTickSerializer pts = new PbTickSerializer(); using (Stream stream = File.Open(@"d:\wukan\Desktop\20141225_1.pd0", FileMode.Create)) { using (StreamReader file = new StreamReader(fi.OpenRead())) { int i = 0; string str = file.ReadLine(); do { ++i; str = file.ReadLine(); if (str == null) { break; } string[] arr = str.Split(','); PbTick tick = new PbTick(); pts.Codec.SetSymbol(tick, arr[0]); tick.Config = new ConfigInfo().Default(); if (arr[0].StartsWith("TF")) { tick.Config.SetTickSize(0.002); tick.Config.ContractMultiplier = 10000; } else { tick.Config.SetTickSize(0.2); tick.Config.ContractMultiplier = 300; } tick.Config.Time_ssf_Diff = 5; pts.Codec.Config = tick.Config; tick.ActionDay = int.Parse(arr[5]); int time = int.Parse(arr[6]); tick.Time_HHmm = time / 100000; tick.Time_____ssf__ = time % 100000 / 100; tick.Time________ff = time % 100; pts.Codec.SetLastPrice(tick, double.Parse(arr[8])); pts.Codec.SetHigh(tick, double.Parse(arr[9])); pts.Codec.SetLow(tick, double.Parse(arr[10])); pts.Codec.SetVolume(tick, int.Parse(arr[11])); pts.Codec.SetTurnover(tick, int.Parse(arr[12])); pts.Codec.SetOpenInterest(tick, int.Parse(arr[16])); tick.DepthList = new List <DepthItem>(); tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[31])), int.Parse(arr[36]), 0)); tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[30])), int.Parse(arr[35]), 0)); tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[29])), int.Parse(arr[34]), 0)); tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[28])), int.Parse(arr[33]), 0)); tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[27])), int.Parse(arr[32]), 0)); pts.Codec.SetAskPrice1(tick, double.Parse(arr[17])); tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[17])), int.Parse(arr[22]), 0)); tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[18])), int.Parse(arr[23]), 0)); tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[19])), int.Parse(arr[24]), 0)); tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[20])), int.Parse(arr[25]), 0)); tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[21])), int.Parse(arr[26]), 0)); pts.Write(tick, stream); } while (str != null); file.Close(); } } }