/// <summary> /// 将列表中的数据平铺到结构体中 /// 只在保存到文件时操作,其它时候都不要调用 /// 以后用户要使用数据时直接操作列表即可 /// </summary> public void PrepareObjectBeforeWrite(ConfigInfo _Config) { if (DepthList == null || DepthList.Count == 0) { Depth = null; return; } List <DepthItem> tempList = new List <DepthItem>(); DepthListHelper.PriceMinusInOneList(DepthList, tempList); // 还是使用新的,因为不知道以前是否已经有数据,导致混淆 Depth = new DepthTick(); //if(tempList.Count>0) // tempList[0].Price = LastPrice - tempList[0].Price; DepthListHelper.ListToStruct(tempList, _Config.MarketType, Depth); }
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); }
static void Main(string[] args) { List<DepthItem> oldPrevList = new List<DepthItem>(); List<DepthItem> oldCurrList = new List<DepthItem>(); List<DepthItem> newPrevList = new List<DepthItem>(); List<DepthItem> newCurrList = new List<DepthItem>(); DepthListHelper test = new DepthListHelper(); // 处理老的多一条 oldPrevList.Add(new DepthItem(1, 100, 0)); oldPrevList.Add(new DepthItem(7, 100, 0)); oldPrevList.Add(new DepthItem(12, 100, 0)); int j11 = DepthListHelper.FindAsk1Position(oldPrevList, 0); int j12 = DepthListHelper.FindAsk1Position(oldPrevList, 1); int j13 = DepthListHelper.FindAsk1Position(oldPrevList, 12); int j14 = DepthListHelper.FindAsk1Position(oldPrevList, 15); List<DepthItemView> oldPrevListV = new List<DepthItemView>(); oldPrevListV.Add(new DepthItemView() { Price = 10}); oldPrevListV.Add(new DepthItemView() { Price = 5 }); oldPrevListV.Add(new DepthItemView() { Price = 1 }); int j21 = DepthListHelper.FindAsk1PositionDescending(oldPrevListV, 12); int j22 = DepthListHelper.FindAsk1PositionDescending(oldPrevListV, 10); int j23 = DepthListHelper.FindAsk1PositionDescending(oldPrevListV, 1); int j24 = DepthListHelper.FindAsk1PositionDescending(oldPrevListV, 0); //oldCurrList.Add(new DepthItem(5, 100, 0)); //oldCurrList.Add(new DepthItem(8, 100, 0)); //oldCurrList.Add(new DepthItem(13, 100, 0)); DepthListHelper.ExpandTwoListsToSameLength(oldPrevList, oldCurrList, 5, 13, newPrevList, newCurrList); List<DepthItem> newList = new List<DepthItem>(); DepthListHelper.SizeMinusInTwoLists(newPrevList, newCurrList, newList); List<DepthItem> newList2 = new List<DepthItem>(); DepthListHelper.SizeAddInTwoLists(newPrevList, newList, newList2); List<DepthItem> newList3 = new List<DepthItem>(); DepthListHelper.PriceMinusInOneList(newList2, newList3); List<DepthItem> newList4 = new List<DepthItem>(); DepthListHelper.PriceAddInOneList(newList3, newList4); DepthTick tick = new DepthTick(); DepthListHelper.SetDepthTick(tick, 4, 100); DepthListHelper.SetDepthTick(tick, 10, 200); DepthListHelper.SetDepthTick(tick, 15, 300); DepthListHelper.SetDepthTick(tick, 28, 400); DepthListHelper.SetDepthTick(tick, 30, 500); int i1 = DepthListHelper.GetDepthTick(tick, 4); int i2 = DepthListHelper.GetDepthTick(tick, 10); int i3 = DepthListHelper.GetDepthTick(tick, 15); int i4 = DepthListHelper.GetDepthTick(tick, 28); int i5 = DepthListHelper.GetDepthTick(tick, 30); int i6 = DepthListHelper.GetDepthTick(tick, 45); DepthTick tick2 = new DepthTick(); DepthListHelper.ListToStruct(newPrevList, 2, tick2); List<DepthItem> newList5 = new List<DepthItem>(); DepthListHelper.StructToList(tick2, 2, newList5); //int j1 = test.FindAsk1Position(oldCurrList, 5); //int j2 = test.FindAsk1Position(oldCurrList, 8); //int j3 = test.FindAsk1Position(oldCurrList, 12); //int j4 = test.FindAsk1Position(oldCurrList, 13); //int j5 = test.FindAsk1Position(oldCurrList, 14); //int j6 = test.FindAsk1Position(oldCurrList, 15); //int j7 = test.FindAsk1Position(oldCurrList, 4); //int j8 = test.FindAsk1Position(oldCurrList, 7); //oldPrevList = new List<DepthItem>(); //oldPrevList.Add(new DepthItem(3, 100, 0)); //oldPrevList.Add(new DepthItem(7, 100, 0)); //oldPrevList.Add(new DepthItem(12, 100, 0)); //int x1 = test.FindAsk1Position(oldPrevList, 3); //int x2 = test.FindAsk1Position(oldPrevList, 7); //int x3 = test.FindAsk1Position(oldPrevList, 12); //int x4 = test.FindAsk1Position(oldPrevList, 5); //int x5 = test.FindAsk1Position(oldPrevList, 13); //int x6 = test.FindAsk1Position(oldPrevList, 2); // 处理新的多一条 //oldPrevList.Add(new DepthItem(1, 100, 0)); //oldCurrList.Add(new DepthItem(1, 100, 0)); //oldCurrList.Add(new DepthItem(5, 100, 0)); //test.Do(oldPrevList, oldCurrList, newPrevList, newCurrList); //test. }