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); } }
public void TestTickMultiplier() { PbTickCodec codec = new PbTickCodec(); codec.Config.SetTickSize(0.2); codec.Config.ContractMultiplier = 50000; codec.Config.Time_ssf_Diff = 5; codec.UseFlat(false); PbTick tick1 = new PbTick(); codec.SetAveragePrice(tick1, 123.45); codec.SetSettlementPrice(tick1, 123.45); codec.SetTurnover(tick1, 1234567890123456); codec.SetOpenInterest(tick1, 9123456789012345678); codec.SetVolume(tick1, 1234567890); Assert.AreEqual <double>(123.45, codec.GetAveragePrice(tick1)); Assert.AreEqual <double>(123.45, codec.GetSettlementPrice(tick1)); Assert.AreEqual <double>(1234567890120000, codec.GetTurnover(tick1)); Assert.AreEqual <long>(9123456789012345678, codec.GetOpenInterest(tick1)); Assert.AreEqual <long>(1234567890, codec.GetVolume(tick1)); codec.Config.ContractMultiplier = 5; codec.SetTurnover(tick1, 1234567890123456); Assert.AreEqual <double>(1234567890123456, codec.GetTurnover(tick1)); codec.Config.ContractMultiplier = 0.1; codec.SetTurnover(tick1, 12345678901234.56); Assert.AreEqual <double>(12345678901234.56, codec.GetTurnover(tick1)); }
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 TestTick2Price() { PbTickCodec ptc = new PbTickCodec(); ptc.Config.SetTickSize(0.2); ptc.TickSize = ptc.Config.GetTickSize(); Assert.AreEqual(5, ptc.PriceToTick(1)); Assert.AreEqual(6, ptc.PriceToTick(1.2)); Assert.AreEqual(7, ptc.PriceToTick(1.4)); Assert.AreEqual(8, ptc.PriceToTick(1.6)); Assert.AreEqual(9, ptc.PriceToTick(1.8)); Assert.AreEqual(10, ptc.PriceToTick(2.0)); Assert.AreEqual(-5, ptc.PriceToTick(-1)); Assert.AreEqual(-6, ptc.PriceToTick(-1.2)); Assert.AreEqual(-7, ptc.PriceToTick(-1.4)); Assert.AreEqual(-8, ptc.PriceToTick(-1.6)); Assert.AreEqual(-9, ptc.PriceToTick(-1.8)); Assert.AreEqual(-10, ptc.PriceToTick(-2.0)); Assert.AreEqual(0.2, ptc.TickToPrice(1)); Assert.AreEqual(0.4, ptc.TickToPrice(2)); Assert.AreEqual(-0.2, ptc.TickToPrice(-1)); Assert.AreEqual(-0.4, ptc.TickToPrice(-2)); Assert.AreEqual(-2, ptc.TickToPrice(-10)); }
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); }
public void TestTick2Price() { PbTickCodec ptc = new PbTickCodec(); ptc.Config.SetTickSize(0.2); ptc.UseFlat(false); Assert.AreEqual <int>(5, ptc.PriceToTick(1)); Assert.AreEqual <int>(6, ptc.PriceToTick(1.2)); Assert.AreEqual <int>(7, ptc.PriceToTick(1.4)); Assert.AreEqual <int>(8, ptc.PriceToTick(1.6)); Assert.AreEqual <int>(9, ptc.PriceToTick(1.8)); Assert.AreEqual <int>(10, ptc.PriceToTick(2.0)); Assert.AreEqual <int>(-5, ptc.PriceToTick(-1)); Assert.AreEqual <int>(-6, ptc.PriceToTick(-1.2)); Assert.AreEqual <int>(-7, ptc.PriceToTick(-1.4)); Assert.AreEqual <int>(-8, ptc.PriceToTick(-1.6)); Assert.AreEqual <int>(-9, ptc.PriceToTick(-1.8)); Assert.AreEqual <int>(-10, ptc.PriceToTick(-2.0)); Assert.AreEqual <double>(0.2, ptc.TickToPrice(1)); Assert.AreEqual <double>(0.4, ptc.TickToPrice(2)); Assert.AreEqual <double>(-0.2, ptc.TickToPrice(-1)); Assert.AreEqual <double>(-0.4, ptc.TickToPrice(-2)); Assert.AreEqual <double>(-2, ptc.TickToPrice(-10)); }
public void CalcTickSize() { if (Bids.Count > 1 && Asks.Count > 1) { double ts3 = Asks[0].price - Bids[Bids.Count - 1].price; if (ts3 > 0) { TickSize = PbTickCodec.gcd(TickSize, ts3); } } }
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; } }
public void TestGetSetPrice() { PbTickCodec codec = new PbTickCodec(); codec.Config.SetTickSize(0.2); codec.UseFlat(false); PbTick tick = new PbTick(); Assert.AreEqual <double>(0, codec.GetBidPrice(tick, 1)); Assert.AreEqual <double>(0, codec.GetBidPrice(tick, 4)); Assert.AreEqual <double>(0, codec.GetBidPrice(tick, 10)); codec.SetBidPrice(tick, 1, 1.0); codec.SetBidPrice(tick, 4, 2.4); codec.SetBidPrice(tick, 10, 5.8); Assert.AreEqual <double>(1, codec.GetBidPrice(tick, 1)); Assert.AreEqual <double>(2.4, codec.GetBidPrice(tick, 4)); Assert.AreEqual <double>(5.8, codec.GetBidPrice(tick, 10)); Assert.AreEqual <double>(0, codec.GetAskPrice(tick, 1)); Assert.AreEqual <double>(0, codec.GetAskPrice(tick, 4)); Assert.AreEqual <double>(0, codec.GetAskPrice(tick, 10)); codec.SetAskPrice(tick, 1, -1.0); codec.SetAskPrice(tick, 4, 2.4); codec.SetAskPrice(tick, 10, -5.8); Assert.AreEqual <double>(-1.0, codec.GetAskPrice(tick, 1)); Assert.AreEqual <double>(2.4, codec.GetAskPrice(tick, 4)); Assert.AreEqual <double>(-5.8, codec.GetAskPrice(tick, 10)); codec.SetAskCount(tick, 1, 4); codec.SetAskCount(tick, 4, 5); codec.SetAskCount(tick, 10, -9); Assert.AreEqual <double>(4, codec.GetAskCount(tick, 1)); Assert.AreEqual <double>(5, codec.GetAskCount(tick, 4)); Assert.AreEqual <double>(-9, codec.GetAskCount(tick, 10)); codec.SetSettlementPrice(tick, 1234.56); Assert.AreEqual <double>(1234.56, codec.GetSettlementPrice(tick), "SettlementPrice"); codec.SetTurnover(tick, 4567.8); Assert.AreEqual <double>(4567.8, codec.GetTurnover(tick), "Turnover"); }
public void TestGcdTickSize() { PbTickCodec codec = new PbTickCodec(); Assert.AreEqual <double>(0.00001, codec.gcd()); Assert.AreEqual <double>(0.3, codec.gcd(100.3f - 100.0f)); Assert.AreEqual <double>(0.2, codec.gcd(100.3f - 100.1f)); Assert.AreEqual <double>(0.1, codec.gcd(100.3f - 100.0f, 100.2f - 100.0f)); Assert.AreEqual <double>(0.1, codec.gcd(100.3f - 100.0f, 100.2f - 100.0f, 100.1f - 100.0f)); Assert.AreEqual <double>(0.05, codec.gcd(100.3f - 100.0f, 100.2f - 100.0f, 100.1f - 100.0f, 100.1f - 100.05f)); Assert.AreEqual <double>(0.1, codec.gcd(100.0f - 100.3f, 100.0f - 100.2f)); Assert.AreEqual <double>(0.05, codec.gcd(100.0f - 100.3f, 100.0f - 100.2f, 100.0f - 100.1f, 100.05f - 100.1f)); Assert.AreEqual <double>(10, codec.gcd(1000, 41430)); }
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 void AddBid(AAA b) { // 由大到小 if (Bids.Count > 1) { double diff = Bids[Bids.Count - 1].price - b.price; if (diff > 0) { TickSize = PbTickCodec.gcd(TickSize, diff); } } else { TickSize = PbTickCodec.gcd(TickSize, b.price); } Bids.Add(b); }
public void AddAsk(AAA a) { // 由小到大 if (Asks.Count > 1) { double diff = a.price - Asks[Asks.Count - 1].price; if (diff > 0) { TickSize = PbTickCodec.gcd(TickSize, diff); } } else { TickSize = PbTickCodec.gcd(TickSize, a.price); } Asks.Add(a); }
private void ExportToList(FileInfo file_input, FileInfo file_output, bool toCsv, bool toKdb, bool saveQuote) { // 都没选,跳过 if (toCsv || toKdb) { } else { return; } 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); if (toCsv) { ListToCSV(list, file_output); } // 得加入kdb+支持 if (toKdb) { ListToKdb(list, saveQuote); } }
public void TestConvertDateTime() { var span = new TimeSpan(0, 12, 34, 56, 789); int time = 123456; int ms = 789; int hhmm_____ = 0; int ____ssf__ = 0; int _______ff = 0; var codec = new PbTickCodec(); codec.SetUpdateTime(span, out hhmm_____, out ____ssf__, out _______ff); Assert.AreEqual <int>(1234, hhmm_____); Assert.AreEqual <int>(567, ____ssf__); Assert.AreEqual <int>(89, _______ff); codec.SetUpdateTime(time, ms, out hhmm_____, out ____ssf__, out _______ff); Assert.AreEqual <int>(1234, hhmm_____); Assert.AreEqual <int>(567, ____ssf__); Assert.AreEqual <int>(89, _______ff); codec.GetUpdateTime(hhmm_____, ____ssf__, _______ff, out time, out ms); Assert.AreEqual <int>(123456, time); Assert.AreEqual <int>(789, ms); span = codec.GetUpdateTime(hhmm_____, ____ssf__, _______ff); Assert.AreEqual <int>(0, span.Days); Assert.AreEqual <int>(12, span.Hours); Assert.AreEqual <int>(34, span.Minutes); Assert.AreEqual <int>(56, span.Seconds); Assert.AreEqual <int>(789, span.Milliseconds); var date1 = 20141104; var date2 = 20141105; var tick = new PbTick(); codec.SetActionDay(tick, new DateTime(2014, 11, 4)); codec.SetTradingDay(tick, new DateTime(2014, 11, 5)); Assert.AreEqual <int>(date1, tick.ActionDay); Assert.AreEqual <int>(date2, tick.TradingDay); Assert.AreEqual <DateTime>(new DateTime(2014, 11, 4), codec.GetDateTime(tick.ActionDay)); Assert.AreEqual <DateTime>(new DateTime(2014, 11, 5), codec.GetDateTime(tick.TradingDay)); }
private void menuView_Convert_Click(object sender, EventArgs e) { ViewToDataByViewType(); SingleCheck(sender); PbTickCodec Codec = new PbTickCodec(); listTickView = Codec.Data2View(Codec.Restore(this.listTickData), false); dgvTick.DataSource = listTickView; dgvTick.CurrentCell = dgvTick.Rows[RowIndex].Cells[ColumnIndex]; if (Selected) { dgvTick.CurrentRow.Selected = Selected; } dgvTick.FirstDisplayedScrollingRowIndex = FirstDisplayedScrollingRowIndex; dgvTick.HorizontalScrollingOffset = HorizontalScrollingOffset; }
private void ViewToDataByViewType() { PbTickCodec Codec = new PbTickCodec(); List <PbTick> tempList; switch (eViewType) { case ViewType.Diff: this.listTickData = Codec.View2Data(this.listTickView, true); break; case ViewType.Restore: tempList = Codec.View2Data(this.listTickView, true); this.listTickData = Codec.Diff(tempList); break; case ViewType.Convert: tempList = Codec.View2Data(this.listTickView, false); this.listTickData = Codec.Diff(tempList); break; } }
public void TestStatic() { PbTickCodec codec = new PbTickCodec(); codec.Config.SetTickSize(0.2); PbTick tick1 = new PbTick(); tick1.Config = codec.Config; codec.SetSymbol(tick1, "ABC"); codec.SetExchange(tick1, "DEF"); PbTick tick2 = new PbTick(); codec.SetSymbol(tick2, "ABC"); codec.SetExchange(tick2, "DEF"); var diff = codec.Diff(tick1, tick2); Assert.AreEqual(null, diff.Static); }
// 目前先不处理港股的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); }
private void ViewToDataByViewType() { PbTickCodec Codec = new PbTickCodec(); this.listTickData = Codec.View2Data(this.listTickView, true); }
private DepthMarketDataField PbTick2DepthMarketDataField(PbTickCodec codec, PbTick tick) { PbTickView tickView = codec.Data2View(tick, false); DepthMarketDataField marketData = default(DepthMarketDataField); codec.GetUpdateTime(tick, out marketData.UpdateTime, out marketData.UpdateMillisec); marketData.TradingDay = tickView.TradingDay; marketData.ActionDay = tickView.ActionDay; marketData.LastPrice = tickView.LastPrice; marketData.Volume = tickView.Volume; 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); } } 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 _BidPos = BidPos; if (_BidPos >= 0) { marketData.BidPrice1 = tickView.DepthList[_BidPos].Price; marketData.BidVolume1 = tickView.DepthList[_BidPos].Size; --_BidPos; if (_BidPos >= 0) { marketData.BidPrice2 = tickView.DepthList[_BidPos].Price; marketData.BidVolume2 = tickView.DepthList[_BidPos].Size; --_BidPos; if (_BidPos >= 0) { marketData.BidPrice3 = tickView.DepthList[_BidPos].Price; marketData.BidVolume3 = tickView.DepthList[_BidPos].Size; --_BidPos; if (_BidPos >= 0) { marketData.BidPrice4 = tickView.DepthList[_BidPos].Price; marketData.BidVolume4 = tickView.DepthList[_BidPos].Size; --_BidPos; if (_BidPos >= 0) { marketData.BidPrice5 = tickView.DepthList[_BidPos].Price; marketData.BidVolume5 = tickView.DepthList[_BidPos].Size; } } } } } int _AskPos = AskPos; if (_AskPos < count) { marketData.AskPrice1 = tickView.DepthList[_AskPos].Price; marketData.AskVolume1 = tickView.DepthList[_AskPos].Size; ++_AskPos; if (_AskPos < count) { marketData.AskPrice2 = tickView.DepthList[_AskPos].Price; marketData.AskVolume2 = tickView.DepthList[_AskPos].Size; ++_AskPos; if (_AskPos < count) { marketData.AskPrice3 = tickView.DepthList[_AskPos].Price; marketData.AskVolume3 = tickView.DepthList[_AskPos].Size; ++_AskPos; if (_AskPos < count) { marketData.AskPrice4 = tickView.DepthList[_AskPos].Price; marketData.AskVolume4 = tickView.DepthList[_AskPos].Size; ++_AskPos; if (_AskPos < count) { marketData.AskPrice5 = tickView.DepthList[_AskPos].Price; marketData.AskVolume5 = tickView.DepthList[_AskPos].Size; } } } } } } return(marketData); }
// 目前先不处理港股的tickSize变化的那种行情 PbTick CreateTick(ref DepthMarketDataField 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); do { if (pDepthMarketData.BidVolume1 == 0) { break; } tick.DepthList.Insert(0, new DepthItem(codec.PriceToTick(pDepthMarketData.BidPrice1), pDepthMarketData.BidVolume1, 0)); // 先记录一个假的,防止只有买价没有卖价的情况 codec.SetAskPrice1(tick, pDepthMarketData.BidPrice1); tick.AskPrice1 += 1; if (pDepthMarketData.BidVolume2 == 0) { break; } tick.DepthList.Insert(0, new DepthItem(codec.PriceToTick(pDepthMarketData.BidPrice2), pDepthMarketData.BidVolume2, 0)); if (pDepthMarketData.BidVolume3 == 0) { break; } tick.DepthList.Insert(0, new DepthItem(codec.PriceToTick(pDepthMarketData.BidPrice3), pDepthMarketData.BidVolume3, 0)); if (pDepthMarketData.BidVolume4 == 0) { break; } tick.DepthList.Insert(0, new DepthItem(codec.PriceToTick(pDepthMarketData.BidPrice4), pDepthMarketData.BidVolume4, 0)); if (pDepthMarketData.BidVolume5 == 0) { break; } tick.DepthList.Insert(0, new DepthItem(codec.PriceToTick(pDepthMarketData.BidPrice5), pDepthMarketData.BidVolume5, 0)); } while (false); do { if (pDepthMarketData.AskVolume1 == 0) { break; } tick.DepthList.Add(new DepthItem(codec.PriceToTick(pDepthMarketData.AskPrice1), pDepthMarketData.AskVolume1, 0)); // 记录卖一价 codec.SetAskPrice1(tick, pDepthMarketData.AskPrice1); if (pDepthMarketData.AskVolume2 == 0) { break; } tick.DepthList.Add(new DepthItem(codec.PriceToTick(pDepthMarketData.AskPrice2), pDepthMarketData.AskVolume2, 0)); if (pDepthMarketData.AskVolume3 == 0) { break; } tick.DepthList.Add(new DepthItem(codec.PriceToTick(pDepthMarketData.AskPrice3), pDepthMarketData.AskVolume3, 0)); if (pDepthMarketData.AskVolume4 == 0) { break; } tick.DepthList.Add(new DepthItem(codec.PriceToTick(pDepthMarketData.AskPrice4), pDepthMarketData.AskVolume4, 0)); if (pDepthMarketData.AskVolume5 == 0) { break; } tick.DepthList.Add(new DepthItem(codec.PriceToTick(pDepthMarketData.AskPrice5), pDepthMarketData.AskVolume5, 0)); } while (false); return(tick); }
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 void TestTickDiff() { PbTickCodec codec = new PbTickCodec(); codec.Config.SetTickSize(0.2); codec.Config.Time_ssf_Diff = 5; codec.UseFlat(false); PbTick tick1 = new PbTick(); tick1.Config = codec.Config; codec.SetLastPrice(tick1, 1234); codec.SetVolume(tick1, 1); codec.SetActionDay(tick1, DateTime.Today); codec.SetTradingDay(tick1, DateTime.Today.AddDays(-1)); //codec.Set codec.SetAskPrice(tick1, 1, 1234.2); codec.SetAskSize(tick1, 1, 1); //codec.SetAskPrice(tick1, 2, 1234.4); //codec.SetAskSize(tick1, 2, 1); //codec.SetAskPrice(tick1, 3, 1234.6); //codec.SetAskSize(tick1, 3, 3); //codec.SetAskPrice(tick1, 4, 1234.8); //codec.SetAskSize(tick1, 4, 4); //codec.SetAskPrice(tick1, 5, 1235.0); //codec.SetAskSize(tick1, 5, 5); //codec.SetAskPrice(tick1, 6, 1235.2); //codec.SetAskSize(tick1, 6, 6); codec.SetBidPrice(tick1, 1, 1234); codec.SetBidSize(tick1, 1, 1); //codec.SetBidPrice(tick1, 2, 1233.8); //codec.SetBidSize(tick1, 2, 2); //codec.SetBidPrice(tick1, 3, 1233.6); //codec.SetBidSize(tick1, 3, 3); //codec.SetBidPrice(tick1, 4, 1233.4); //codec.SetBidSize(tick1, 4, 4); //codec.SetBidPrice(tick1, 5, 1233.2); //codec.SetBidSize(tick1, 5, 5); //codec.SetBidPrice(tick1, 6, 1232.0); //codec.SetBidSize(tick1, 6, 6); codec.SetLowerLimitPrice(tick1, 123.4); codec.SetUpperLimitPrice(tick1, 567.8); codec.SetSettlementPrice(tick1, 123.4); codec.SetOpen(tick1, 10); codec.SetHigh(tick1, 10); codec.SetLow(tick1, 10); codec.SetClose(tick1, 10); codec.SetBarSize(tick1, 10); tick1.Time_HHmm = 1234; tick1.Time_____ssf__ = 567; tick1.Time________ff = 0; PbTick tick2 = new PbTick(); codec.SetLastPrice(tick2, 1234.2); codec.SetVolume(tick2, 2); codec.SetActionDay(tick2, DateTime.Today); codec.SetTradingDay(tick2, DateTime.Today.AddDays(-1)); codec.SetAskPrice(tick2, 1, 1234.2); codec.SetAskSize(tick2, 1, 1); codec.SetAskPrice(tick2, 2, 1234.4); codec.SetAskSize(tick2, 2, 2); codec.SetAskPrice(tick2, 3, 1234.6); codec.SetAskSize(tick2, 3, 3); codec.SetBidPrice(tick2, 1, 1234); codec.SetBidSize(tick2, 1, 1); codec.SetBidPrice(tick2, 2, 1233.8); codec.SetBidSize(tick2, 2, 2); codec.SetBidPrice(tick2, 3, 1233.6); codec.SetBidSize(tick2, 3, 3); codec.SetLowerLimitPrice(tick2, 123.4); codec.SetUpperLimitPrice(tick2, 567.8); codec.SetSettlementPrice(tick2, 123.4); codec.SetOpen(tick2, 10); codec.SetHigh(tick2, 10); codec.SetLow(tick2, 10); codec.SetClose(tick2, 10); codec.SetBarSize(tick2, 10); tick2.Time_HHmm = 1234; tick2.Time_____ssf__ = 572; tick2.Time________ff = 0; var diff = codec.Diff(tick1, tick2); Assert.AreEqual <int>(0, diff.ActionDay); Assert.AreEqual <int>(0, diff.TradingDay); Assert.AreEqual(null, diff.Static); Assert.AreEqual(null, diff.Bar); Assert.AreEqual <int>(0, diff.Depth1_3.AskPrice1, "AskPrice1"); Assert.AreEqual <int>(0, diff.Depth1_3.AskSize1, "AskSize1"); Assert.AreEqual <int>(0, diff.Depth1_3.AskPrice2, "AskPrice2"); Assert.AreEqual <int>(2, diff.Depth1_3.AskSize2, "AskSize2"); Assert.AreEqual <int>(0, diff.Depth1_3.AskPrice3, "AskPrice3"); Assert.AreEqual <int>(3, diff.Depth1_3.AskSize3, "AskSize3"); Assert.AreEqual <int>(0, diff.Time_____ssf__); var tick3 = codec.Restore(tick1, diff); Assert.AreEqual <int>(tick2.Depth1_3.AskPrice1, tick3.Depth1_3.AskPrice1); Assert.AreEqual <int>(tick2.Depth1_3.AskPrice2, tick3.Depth1_3.AskPrice2); Assert.AreEqual <int>(tick2.Depth1_3.AskPrice3, tick3.Depth1_3.AskPrice3); Assert.AreEqual <int>(tick2.Depth1_3.AskSize1, tick3.Depth1_3.AskSize1); Assert.AreEqual <int>(tick2.Depth1_3.AskSize2, tick3.Depth1_3.AskSize2); Assert.AreEqual <int>(tick2.Depth1_3.AskSize3, tick3.Depth1_3.AskSize3); Assert.AreEqual <int>(tick2.Depth1_3.BidPrice1, tick3.Depth1_3.BidPrice1); Assert.AreEqual <int>(tick2.Depth1_3.BidPrice2, tick3.Depth1_3.BidPrice2); Assert.AreEqual <int>(tick2.Depth1_3.BidPrice3, tick3.Depth1_3.BidPrice3); Assert.AreEqual <int>(tick2.Depth1_3.BidSize1, tick3.Depth1_3.BidSize1); Assert.AreEqual <int>(tick2.Depth1_3.BidSize2, tick3.Depth1_3.BidSize2); Assert.AreEqual <int>(tick2.Depth1_3.BidSize3, tick3.Depth1_3.BidSize3); Assert.AreEqual <double>(codec.GetLowerLimitPrice(tick1), codec.GetLowerLimitPrice(tick3)); Assert.AreEqual <double>(codec.GetUpperLimitPrice(tick1), codec.GetUpperLimitPrice(tick3)); Assert.AreEqual <double>(codec.GetSettlementPrice(tick1), codec.GetSettlementPrice(tick3)); Assert.AreEqual <double>(codec.GetOpen(tick1), codec.GetOpen(tick3)); Assert.AreEqual <double>(codec.GetHigh(tick1), codec.GetHigh(tick3)); Assert.AreEqual <double>(codec.GetLow(tick1), codec.GetLow(tick3)); Assert.AreEqual <double>(codec.GetClose(tick1), codec.GetClose(tick3)); Assert.AreEqual <int>(572, tick3.Time_____ssf__); }