public PbTick Restore(PbTick prev, PbTick diff) { if (prev == null) { if (diff.Config == null) { throw new Exception("快照的配置不能为空"); } // 记下配置,要用到 Config = diff.Config; // 是快照,直接返回 return(diff); } var tick = new PbTick(); #region 配置数据 if (diff.Config == null) { // 使用上条的配置 tick.Config = prev.Config; } else { // 新配置 _config = diff.Config; // 是快照,直接返回 return(diff); } #endregion Config = tick.Config; tick.LastPrice = prev.LastPrice + diff.LastPrice; tick.AskPrice1 = prev.AskPrice1 + diff.AskPrice1; // 前一个是完整的,后一个是做过Size和Price的调整,如何还原 var newPrevList = new List <DepthItem>(); var newDiffList = new List <DepthItem>(); // 换成同长度 DepthListHelper.ExpandTwoListsToSameLength( prev.DepthList, diff.DepthList, int.MinValue, int.MaxValue, newPrevList, newDiffList); tick.DepthList = new List <DepthItem>(); DepthListHelper.SizeAddInTwoLists(newPrevList, newDiffList, tick.DepthList); #region 常用行情信息 tick.Volume = prev.Volume + diff.Volume; tick.OpenInterest = prev.OpenInterest + diff.OpenInterest; tick.Turnover = prev.Turnover + diff.Turnover; tick.AveragePrice = prev.AveragePrice + diff.AveragePrice; tick.TradingDay = prev.TradingDay + diff.TradingDay; tick.ActionDay = prev.ActionDay + diff.ActionDay; tick.Time_HHmm = prev.Time_HHmm + diff.Time_HHmm; tick.Time________ff = prev.Time________ff + diff.Time________ff; // 还原时间 tick.Time_____ssf__ = prev.Time_____ssf__ + diff.Time_____ssf__ + _config.Time_ssf_Diff; tick.LocalTime_Msec = prev.LocalTime_Msec + diff.LocalTime_Msec; #endregion #region Bar数据 if (prev.Bar != null || diff.Bar != null) { tick.Bar = new BarInfo(); if (prev.Bar == null) { prev.Bar = new BarInfo(); } if (diff.Bar == null) { diff.Bar = new BarInfo(); } tick.Bar.Open = prev.Bar.Open + diff.Bar.Open; tick.Bar.High = prev.Bar.High + diff.Bar.High; tick.Bar.Low = prev.Bar.Low + diff.Bar.Low; tick.Bar.Close = prev.Bar.Close + diff.Bar.Close; tick.Bar.BarSize = prev.Bar.BarSize + diff.Bar.BarSize; } #endregion #region 静态数据 if (prev.Static != null || diff.Static != null) { tick.Static = new StaticInfo(); if (prev.Static == null) { prev.Static = new StaticInfo(); } if (diff.Static == null) { diff.Static = new StaticInfo(); } tick.Static.LowerLimitPrice = prev.Static.LowerLimitPrice + diff.Static.LowerLimitPrice; tick.Static.UpperLimitPrice = prev.Static.UpperLimitPrice + diff.Static.UpperLimitPrice; tick.Static.SettlementPrice = prev.Static.SettlementPrice + diff.Static.SettlementPrice; tick.Static.Symbol = string.IsNullOrWhiteSpace(diff.Static.Symbol) ? prev.Static.Symbol : diff.Static.Symbol; tick.Static.Exchange = string.IsNullOrWhiteSpace(diff.Static.Exchange) ? prev.Static.Exchange : diff.Static.Exchange; } #endregion #region 除权除息数据 // 没有做过差分,所以直接返回 if (diff.Split != null) { tick.Split = diff.Split; } #endregion return(tick); }
/// <summary> /// 传入两个tick得到tick的差分 /// </summary> /// <param name="prev"></param> /// <param name="current"></param> /// <returns></returns> public PbTick Diff(PbTick prev, PbTick current) { if (prev == null) { if (current.Config == null) { throw new Exception("快照的配置不能为空"); } return(current); } var tick = new PbTick(); #region 配置数据 // 当前数据为空或前后相同,表示 if (current.Config == null || prev.Config.IsSame(current.Config)) { tick.Config = null; // 可以继续下去 } else { // 是新数据,返回快照 Config = current.Config; return(current); } #endregion // 先取最关键的数据,因为前一条的config总会补成有效 Config = prev.Config; tick.LastPrice = current.LastPrice - prev.LastPrice; tick.AskPrice1 = current.AskPrice1 - prev.AskPrice1; // 在这假定两个数据都是完整的 var newPrevList = new List <DepthItem>(); var newCurrList = new List <DepthItem>(); // 先将两个队列变成同长 DepthListHelper.ExpandTwoListsToSameLength( prev.DepthList, current.DepthList, int.MinValue, int.MaxValue,//这个截断工作没有做 newPrevList, newCurrList); // 然后做减法,变动Size tick.DepthList = new List <DepthItem>(); DepthListHelper.SizeMinusInTwoLists(newPrevList, newCurrList, tick.DepthList); #region 常用行情信息 tick.Volume = current.Volume - prev.Volume; tick.OpenInterest = current.OpenInterest - prev.OpenInterest; tick.Turnover = current.Turnover - prev.Turnover; tick.AveragePrice = current.AveragePrice - prev.AveragePrice; tick.TradingDay = current.TradingDay - prev.TradingDay; tick.ActionDay = current.ActionDay - prev.ActionDay; tick.Time_HHmm = current.Time_HHmm - prev.Time_HHmm; tick.Time________ff = current.Time________ff - prev.Time________ff; // 这个地方有区别要减去一个差,将时间再缩小 tick.Time_____ssf__ = current.Time_____ssf__ - prev.Time_____ssf__ - _config.Time_ssf_Diff; tick.LocalTime_Msec = current.LocalTime_Msec - prev.LocalTime_Msec; #endregion #region Bar数据 // Bar数据要进行差分计算 if (current.Bar != null || prev.Bar != null) { tick.Bar = new BarInfo(); if (current.Bar == null) { current.Bar = new BarInfo(); } if (prev.Bar == null) { prev.Bar = new BarInfo(); } tick.Bar.Open = current.Bar.Open - prev.Bar.Open; tick.Bar.High = current.Bar.High - prev.Bar.High; tick.Bar.Low = current.Bar.Low - prev.Bar.Low; tick.Bar.Close = current.Bar.Close - prev.Bar.Close; tick.Bar.BarSize = current.Bar.BarSize - prev.Bar.BarSize; if (tick.Bar.IsZero) { tick.Bar = null; } } #endregion #region 静态数据 if (current.Static != null || prev.Static != null) { tick.Static = new StaticInfo(); if (current.Static == null) { current.Static = new StaticInfo(); } if (prev.Static == null) { prev.Static = new StaticInfo(); } tick.Static.LowerLimitPrice = current.Static.LowerLimitPrice - prev.Static.LowerLimitPrice; tick.Static.UpperLimitPrice = current.Static.UpperLimitPrice - prev.Static.UpperLimitPrice; tick.Static.SettlementPrice = current.Static.SettlementPrice - prev.Static.SettlementPrice; if (!string.Equals(current.Static.Exchange, prev.Static.Exchange)) { tick.Static.Exchange = current.Static.Exchange; } if (!string.Equals(current.Static.Symbol, prev.Static.Symbol)) { tick.Static.Symbol = current.Static.Symbol; } if (tick.Static.IsZero) { tick.Static = null; } } #endregion #region 除权除息数据 // 除权除息数据本来就是稀疏矩阵,不需要做差分 if (current.Split != null) { tick.Split = current.Split; if (tick.Split.IsZero) { tick.Split = null; } } #endregion return(tick); }