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__); }
// 目前先不处理港股的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); }
// 目前先不处理港股的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); }