public PbTickView Int2Double(PbTick tick, bool descending) { if (tick == null) { return(null); } var view = new PbTickView(); // 利用此机会设置TickSize if (Codec == null) { Codec = new PbTickCodec(); } view.Config = Int2Double(tick.Config); Codec.Config = tick.Config; view.Turnover = Codec.GetTurnover(tick); view.AveragePrice = Codec.GetAveragePrice(tick); view.LastPrice = Codec.TickToPrice(tick.LastPrice); view.AskPrice1 = Codec.TickToPrice(tick.AskPrice1); view.Volume = tick.Volume; view.OpenInterest = tick.OpenInterest; view.TradingDay = tick.TradingDay; view.ActionDay = tick.ActionDay; view.Time_HHmm = tick.Time_HHmm; view.Time_____ssf__ = tick.Time_____ssf__; view.Time________ff = tick.Time________ff; view.Bar = Int2Double(tick.Bar); view.Static = Int2Double(tick.Static); view.Split = Int2Double(tick.Split); view.LocalTime_Msec = tick.LocalTime_Msec; view.DepthList = Int2Double(tick.DepthList, descending); view.LoadQuote(descending); return(view); }
public static void WriteCsv(IEnumerable <PbTick> list, string output) { if (list == null) { return; } var Codec = new PbTickCodec(); // 将差分数据生成界面数据 IEnumerable <PbTickView> _list = Codec.Data2View(Codec.Restore(list), false); // 保存 using (TextWriter stream = new StreamWriter(output)) { var t = new PbTickView(); stream.WriteLine(PbTickView.ToCsvHeader()); foreach (var l in _list) { stream.WriteLine(l); } stream.Close(); } }
public void TestRead() { var file = new FileInfo("TickDataV1_1"); using (var stream = new MemoryStream()) using (var fileStream = file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var zip = new SevenZipExtractor(fileStream)) { zip.ExtractFile(0, stream); stream.Seek(0, SeekOrigin.Begin); var serializer = new PbTickSerializer(); var codec = new PbTickCodec(); foreach (var tick in serializer.Read(stream)) { } } }
public PbTick Double2Int(PbTickView tick, bool descending) { if (tick == null) { return(null); } var field = new PbTick(); // 利用此机会设置TickSize if (Codec == null) { Codec = new PbTickCodec(); } field.Config = Double2Int(tick.Config); Codec.Config = field.Config; Codec.SetTurnover(field, tick.Turnover); Codec.SetAveragePrice(field, tick.AveragePrice); field.LastPrice = Codec.PriceToTick(tick.LastPrice); field.AskPrice1 = Codec.PriceToTick(tick.AskPrice1); field.Volume = tick.Volume; field.OpenInterest = tick.OpenInterest; field.TradingDay = tick.TradingDay; field.ActionDay = tick.ActionDay; field.Time_HHmm = tick.Time_HHmm; field.Time_____ssf__ = tick.Time_____ssf__; field.Time________ff = tick.Time________ff; field.Bar = Double2Int(tick.Bar); field.Static = Double2Int(tick.Static); field.Split = Double2Int(tick.Split); field.LocalTime_Msec = tick.LocalTime_Msec; field.DepthList = Double2Int(tick.DepthList, descending); return(field); }
public static void WriteCsv(IEnumerable<PbTick> list, string output) { if (list == null) return; PbTickCodec Codec = new PbTickCodec(); // 将差分数据生成界面数据 IEnumerable<PbTickView> _list = Codec.Data2View(Codec.Restore(list), false); // 保存 using (TextWriter stream = new StreamWriter(output)) { PbTickView t = new PbTickView(); stream.WriteLine(t.ToCsvHeader()); foreach (var l in _list) { stream.WriteLine(l); } stream.Close(); } }
public PbTickSerializer() { Codec = new PbTickCodec(); }
public PbTickView Int2Double(PbTick tick, bool descending) { if (tick == null) return null; var view = new PbTickView(); // 利用此机会设置TickSize if (Codec == null) { Codec = new PbTickCodec(); } view.Config = Int2Double(tick.Config); Codec.Config = tick.Config; view.Turnover = Codec.GetTurnover(tick); view.AveragePrice = Codec.GetAveragePrice(tick); view.LastPrice = Codec.TickToPrice(tick.LastPrice); view.AskPrice1 = Codec.TickToPrice(tick.AskPrice1); view.Volume = tick.Volume; view.OpenInterest = tick.OpenInterest; view.TradingDay = tick.TradingDay; view.ActionDay = tick.ActionDay; view.Time_HHmm = tick.Time_HHmm; view.Time_____ssf__ = tick.Time_____ssf__; view.Time________ff = tick.Time________ff; view.Bar = Int2Double(tick.Bar); view.Static = Int2Double(tick.Static); view.Split = Int2Double(tick.Split); view.LocalTime_Msec = tick.LocalTime_Msec; view.DepthList = Int2Double(tick.DepthList, descending); view.LoadQuote(descending); return view; }
// 目前先不处理港股的tickSize变化的那种行情 PbTick CreateTick(ref DepthMarketDataNClass pDepthMarketData, PbTickCodec codec) { var tick = new PbTick(); tick.DepthList = new List<DepthItem>(); tick.Config = codec.Config; tick.TradingDay = pDepthMarketData.TradingDay; tick.ActionDay = pDepthMarketData.ActionDay; tick.Time_HHmm = pDepthMarketData.UpdateTime / 100; tick.Time_____ssf__ = pDepthMarketData.UpdateTime % 100 * 10 + pDepthMarketData.UpdateMillisec / 100; tick.Time________ff = pDepthMarketData.UpdateMillisec % 100; // 数据接收器时计算本地与交易所的行情时间差 // 1.这个地方是否保存? // 2.到底是XAPI中提供还是由接收器提供? //tick.LocalTime_Msec = (int)(DateTime.Now - codec.GetActionDayDateTime(tick)).TotalMilliseconds; codec.SetSymbol(tick, pDepthMarketData.Symbol); if (pDepthMarketData.Exchange != ExchangeType.Undefined) codec.SetExchange(tick, Enum<ExchangeType>.ToString(pDepthMarketData.Exchange)); codec.SetLowerLimitPrice(tick, pDepthMarketData.LowerLimitPrice); codec.SetUpperLimitPrice(tick, pDepthMarketData.UpperLimitPrice); codec.SetOpen(tick, pDepthMarketData.OpenPrice); codec.SetHigh(tick, pDepthMarketData.HighestPrice); codec.SetLow(tick, pDepthMarketData.LowestPrice); codec.SetClose(tick, pDepthMarketData.ClosePrice); codec.SetVolume(tick, (long)pDepthMarketData.Volume); codec.SetOpenInterest(tick, (long)pDepthMarketData.OpenInterest); codec.SetTurnover(tick, pDepthMarketData.Turnover);//一定要设置合约乘数才能最优保存 codec.SetAveragePrice(tick, pDepthMarketData.AveragePrice); codec.SetLastPrice(tick, pDepthMarketData.LastPrice); codec.SetSettlementPrice(tick, pDepthMarketData.SettlementPrice); codec.SetPreClosePrice(tick, pDepthMarketData.PreClosePrice); codec.SetPreSettlementPrice(tick, pDepthMarketData.PreSettlementPrice); codec.SetPreOpenInterest(tick, (long)pDepthMarketData.PreOpenInterest); for(int i = pDepthMarketData.Bids.Length - 1;i>=0;--i) { var bid = pDepthMarketData.Bids[i]; if (bid.Size == 0) break; // 记录卖一价 if (i == 0) { codec.SetAskPrice1(tick, bid.Price); tick.AskPrice1 += 1; } tick.DepthList.Add(new DepthItem(codec.PriceToTick(bid.Price), bid.Size, bid.Count)); } for (int i = 0; i < pDepthMarketData.Asks.Length; ++i) { var ask = pDepthMarketData.Asks[i]; if (ask.Size == 0) break; // 记录卖一价 if (i == 0) { codec.SetAskPrice1(tick, ask.Price); } tick.DepthList.Add(new DepthItem(codec.PriceToTick(ask.Price), ask.Size, ask.Count)); } return tick; }
public PbTickSerializer() { Codec = new PbTickCodec(); }
private void ViewToDataByViewType() { PbTickCodec Codec = new PbTickCodec(); this.listTickData = Codec.View2Data(this.listTickView, true); }
private void ReadFromFile(string pathChosen) { strCurrentFilePath = pathChosen; Tuple<Stream, string, double> tuple = ReadToStream(strCurrentFilePath); if (tuple == null) { return; } Stream stream = tuple.Item1; try { QuantBox.Data.Serializer.PbTickSerializer pts = new QuantBox.Data.Serializer.PbTickSerializer(); listTickData = pts.Read(stream).ToList(); 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; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
public void OutputSeries(out IDataSeries trades, out IDataSeries bids, out IDataSeries asks) { trades = new TickSeries(); bids = new TickSeries(); asks = new TickSeries(); PbTickCodec codec = new PbTickCodec(); int TradingDay = -1; int _lastTradeSize = 0; foreach (var s in Series) { if(TradingDay != s.TradingDay) { _lastTradeSize = 0; TradingDay = s.TradingDay; } var dateTime = codec.GetDateTime(s.ActionDay == 0 ? s.TradingDay : s.ActionDay).Add(codec.GetUpdateTime(s)); var tick = PbTick2DepthMarketDataNClass(codec, s); if(SubscribeExternData) { var trade = new TradeEx(dateTime, 0, _InstrumentId, tick.LastPrice, (int)tick.Volume); trade.Size -= _lastTradeSize; trade.DepthMarketData = tick; trades.Add(trade); } else { var trade = new Trade(dateTime, 0, _InstrumentId, tick.LastPrice, (int)tick.Volume); trade.Size -= _lastTradeSize; trades.Add(trade); } if (tick.Bids != null && tick.Bids.Length > 0) { var bid = new Bid(dateTime, 0, _InstrumentId, tick.Bids[0].Price, tick.Bids[0].Size); bids.Add(bid); } if (tick.Asks != null && tick.Asks.Length > 0) { var ask = new Ask(dateTime, 0, _InstrumentId, tick.Asks[0].Price, tick.Asks[0].Size); asks.Add(ask); } _lastTradeSize = (int)tick.Volume; } }
private DepthMarketDataNClass PbTick2DepthMarketDataNClass(PbTickCodec codec, PbTickView tickView) { DepthMarketDataNClass marketData = new DepthMarketDataNClass(); codec.GetUpdateTime(tickView, out marketData.UpdateTime, out marketData.UpdateMillisec); marketData.TradingDay = tickView.TradingDay; marketData.ActionDay = tickView.ActionDay; marketData.LastPrice = tickView.LastPrice; marketData.Volume = tickView.Volume; if (SubscribeExternData) { marketData.Turnover = tickView.Turnover; marketData.OpenInterest = tickView.OpenInterest; marketData.AveragePrice = tickView.AveragePrice; if (tickView.Bar != null) { marketData.OpenPrice = tickView.Bar.Open; marketData.HighestPrice = tickView.Bar.High; marketData.LowestPrice = tickView.Bar.Low; marketData.ClosePrice = tickView.Bar.Close; } if (tickView.Static != null) { marketData.LowerLimitPrice = tickView.Static.LowerLimitPrice; marketData.UpperLimitPrice = tickView.Static.UpperLimitPrice; marketData.SettlementPrice = tickView.Static.SettlementPrice; marketData.Symbol = tickView.Static.Symbol; if (!string.IsNullOrWhiteSpace(tickView.Static.Exchange)) { marketData.Exchange = Enum<ExchangeType>.Parse(tickView.Static.Exchange); } marketData.PreClosePrice = tickView.Static.PreClosePrice; marketData.PreSettlementPrice = tickView.Static.PreSettlementPrice; marketData.PreOpenInterest = tickView.Static.PreOpenInterest; } } int count = tickView.DepthList == null ? 0 : tickView.DepthList.Count; if (count > 0) { int AskPos = DepthListHelper.FindAsk1Position(tickView.DepthList, tickView.AskPrice1); int BidPos = AskPos - 1; int BidCount = BidPos + 1; int AskCount = count - AskPos; marketData.Bids = new DepthField[0]; marketData.Asks = new DepthField[0]; if(SubscribeBid) { if(BidCount>0) { marketData.Bids = new DepthField[BidCount]; int j = 0; for (int i = BidPos; i >= 0; --i) { marketData.Bids[j] = new DepthField() { Price = tickView.DepthList[i].Price, Size = tickView.DepthList[i].Size, Count = tickView.DepthList[i].Count, }; ++j; } } } if (SubscribeAsk) { if (AskCount > 0) { marketData.Asks = new DepthField[AskCount]; int j = 0; for (int i = AskPos; i < count; ++i) { marketData.Asks[j] = new DepthField() { Price = tickView.DepthList[i].Price, Size = tickView.DepthList[i].Size, Count = tickView.DepthList[i].Count, }; ++j; } } } } return marketData; }
public PbTick Double2Int(PbTickView tick, bool descending) { if (tick == null) return null; PbTick field = new PbTick(); // 利用此机会设置TickSize if (Codec == null) { Codec = new PbTickCodec(); } field.Config = Double2Int(tick.Config); Codec.Config = field.Config; Codec.SetTurnover(field, tick.Turnover); Codec.SetAveragePrice(field, tick.AveragePrice); field.LastPrice = Codec.PriceToTick(tick.LastPrice); field.AskPrice1 = Codec.PriceToTick(tick.AskPrice1); field.Volume = tick.Volume; field.OpenInterest = tick.OpenInterest; field.TradingDay = tick.TradingDay; field.ActionDay = tick.ActionDay; field.Time_HHmm = tick.Time_HHmm; field.Time_____ssf__ = tick.Time_____ssf__; field.Time________ff = tick.Time________ff; field.Bar = Double2Int(tick.Bar); field.Static = Double2Int(tick.Static); field.Split = Double2Int(tick.Split); field.LocalTime_Msec = tick.LocalTime_Msec; field.DepthList = Double2Int(tick.DepthList, descending); return field; }
private static List<PbTickView> ReadFromFile(string pathChosen) { Tuple<Stream, string, double> tuple = ReadToStream(pathChosen); IEnumerable<PbTick> listTickData; List<PbTickView> listTickView; if (tuple == null) { return null; } Stream stream = tuple.Item1; try { QuantBox.Data.Serializer.PbTickSerializer pts = new QuantBox.Data.Serializer.PbTickSerializer(); listTickData = pts.Read(stream); PbTickCodec Codec = new PbTickCodec(); listTickView = Codec.Data2View(listTickData, true); return listTickView; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return null; } }
private void ExportToList(FileInfo file_input,FileInfo file_output) { Stream _stream = new MemoryStream(); // 加载文件,支持7z解压 var fileStream = file_input.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite); { try { using (var zip = new SevenZipExtractor(fileStream)) { // 只解压了第0个,如果有多个也只解压第一个 zip.ExtractFile(0, _stream); _stream.Seek(0, SeekOrigin.Begin); } } catch { _stream = fileStream; _stream.Seek(0, SeekOrigin.Begin); } } PbTickCodec codec = new PbTickCodec(); QuantBox.Data.Serializer.PbTickSerializer Serializer = new QuantBox.Data.Serializer.PbTickSerializer(); List<PbTickView> list = Serializer.Read2View(_stream); ListToCSV(list, file_output); }