Пример #1
0
        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);
        }
Пример #2
0
 public void SetClose(PbTick tick, double price)
 {
     if (tick.Bar == null)
     {
         tick.Bar = new BarInfo();
     }
     SetClose(tick.Bar, price);
 }
Пример #3
0
 public void SetBarSize(PbTick tick, int barSize)
 {
     if (tick.Bar == null)
     {
         tick.Bar = new BarInfo();
     }
     SetBarSize(tick.Bar, barSize);
 }
Пример #4
0
 public void SetUpperLimitPrice(PbTick tick, double price)
 {
     if (tick.Static == null)
     {
         tick.Static = new StaticInfo();
     }
     SetUpperLimitPrice(tick.Static, price);
 }
Пример #5
0
 public void SetPreSettlementPrice(PbTick tick, double price)
 {
     if (tick.Static == null)
     {
         tick.Static = new StaticInfo();
     }
     SetPreSettlementPrice(tick.Static, price);
 }
Пример #6
0
 public void SetPreOpenInterest(PbTick tick, long val)
 {
     if (tick.Static == null)
     {
         tick.Static = new StaticInfo();
     }
     SetPreOpenInterest(tick.Static, val);
 }
Пример #7
0
        public string GetSymbol(PbTick tick)
        {
            if (tick.Static == null)
            {
                return(null);
            }

            return(tick.Static.Symbol);
        }
Пример #8
0
        public string GetExchange(PbTick tick)
        {
            if (tick.Static == null)
            {
                return(null);
            }

            return(tick.Static.Exchange);
        }
Пример #9
0
        public PbTickView Data2View(PbTick tick, bool descending)
        {
            if (tick == null)
            {
                return(null);
            }
            var converter = new Int2DoubleConverter();

            return(converter.Int2Double(tick, descending));
        }
Пример #10
0
        public PbTick Write(PbTick data, Stream dest)
        {
            PbTick diff = Codec.Diff(_lastWrite, data);
            _lastWrite = data;

            diff.PrepareObjectBeforeWrite(Codec.Config);
            ProtoBuf.Serializer.SerializeWithLengthPrefix<PbTick>(dest, diff, PrefixStyle.Base128);

            return diff;
        }
Пример #11
0
 public void Write(WriterDataItem item, PbTick tick)
 {
     // 对于换交易日,应当将前一天的关闭
     if (item.TradingDay != tick.TradingDay)
     {
         item.Close();
         item.TradingDay = tick.TradingDay;
     }
     item.Tick = tick;
     item.Write();
 }
Пример #12
0
        public bool Write(PbTick tick)
        {
            WriterDataItem item;

            if (Items.TryGetValue(tick.Static.Symbol, out item))
            {
                Write(item, tick);
                return(true);
            }
            return(false);
        }
Пример #13
0
        public PbTick Write(PbTick data, Stream dest)
        {
            var diff = Codec.Diff(_lastWrite, data);

            _lastWrite = data;

            diff.PrepareObjectBeforeWrite(Codec.Config);
            ProtoBuf.Serializer.SerializeWithLengthPrefix(dest, diff, PrefixStyle.Base128);

            return(diff);
        }
Пример #14
0
        public void SetExchange(PbTick tick, string val)
        {
            if (string.IsNullOrWhiteSpace(val))
            {
                return;
            }

            if (tick.Static == null)
            {
                tick.Static = new StaticInfo();
            }

            tick.Static.Exchange = val;
        }
Пример #15
0
        public List <PbTick> Restore(IEnumerable <PbTick> list)
        {
            if (list == null)
            {
                return(null);
            }

            var _list = new List <PbTick>();

            PbTick last = null;

            foreach (var item in list)
            {
                last = Restore(last, item);
                _list.Add(last);
            }
            return(_list);
        }
Пример #16
0
        /// <summary>
        /// 可以同时得到原始的raw和解码后的数据
        /// </summary>
        /// <param name="source"></param>
        /// <param name="raw"></param>
        /// <returns></returns>
        public PbTick ReadOne(Stream source)
        {
            PbTick raw = ProtoBuf.Serializer.DeserializeWithLengthPrefix <PbTick>(source, PrefixStyle.Base128);

            if (raw == null)
            {
                return(null);
            }
            raw.PrepareObjectAfterRead(Codec.Config);

            _lastRead = Codec.Restore(_lastRead, raw);
            if (_lastRead.Config.Version != 2)
            {
                throw new ProtobufDataZeroException("only support pd0 file version 2", _lastRead.Config.Version, 2);
            }

            return(_lastRead);
        }
Пример #17
0
        public List <PbTick> Diff(IEnumerable <PbTick> list)
        {
            if (list == null)
            {
                return(null);
            }

            var _list = new List <PbTick>();

            PbTick last = null;

            foreach (var item in list)
            {
                var diff = Diff(last, item);
                last = item;
                _list.Add(diff);
            }
            return(_list);
        }
Пример #18
0
        public List <PbTick> Read(Stream stream)
        {
            PbTick resotre = null;

            List <PbTick> _list = new List <PbTick>();

            while (true)
            {
                resotre = ReadOne(stream);
                if (resotre == null)
                {
                    break;
                }

                _list.Add(resotre);
            }
            stream.Close();

            return(_list);
        }
Пример #19
0
        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);
        }
Пример #20
0
            public void Write()
            {
                lock (locker)
                {
                    if (Tick == null)
                    {
                        return;
                    }

                    if (Stream == null)
                    {
                        Stream = File.Open(FullPath, FileMode.Append, FileAccess.Write, FileShare.Read);
                        // 换天时,必须重置记录器,不然记录的数据是差分后数据,导致新文件无法解读
                        Serializer.Reset();
                    }

                    Serializer.Write(Tick, Stream);
                    Tick = null;

                    FlushInWriter();
                }
            }
Пример #21
0
            public void Write()
            {
                lock (locker)
                {
                    if (Tick == null)
                        return;

                    if (Stream == null)
                    {
                        // 换天时,必须重置记录器,不然记录的数据是差分后数据,导致新文件无法解读
                        Serializer.Reset();

                        Stream = File.Open(FullPath, FileMode.Append, FileAccess.Write, FileShare.Read);
                    }

                    Serializer.Write(Tick, Stream);
                    Tick = null;
                    //HasData = true;
                    
                    FlushInWriter();
                }
            }
Пример #22
0
 public void SetOpenInterest(PbTick tick, long val)
 {
     tick.OpenInterest = val;
 }
Пример #23
0
 public void SetTurnover(PbTick tick, double val)
 {
     tick.Turnover = (long)(val / _config.TurnoverMultiplier);
 }
Пример #24
0
 public double GetAveragePrice(PbTick tick)
 {
     return(TickToPrice(tick.AveragePrice) / _config.AveragePriceMultiplier);
 }
Пример #25
0
 public void SetAskPrice1(PbTick tick, double price)
 {
     tick.AskPrice1 = PriceToTick(price);
 }
Пример #26
0
 public void SetLastPrice(PbTick tick, double price)
 {
     tick.LastPrice = PriceToTick(price);
 }
Пример #27
0
 public double GetLastPrice(PbTick tick)
 {
     return TickToPrice(tick.LastPrice);
 }
Пример #28
0
 public void SetActionDay(PbTick tick, DateTime date)
 {
     tick.ActionDay = SetDateTime(date);
 }
Пример #29
0
 public void SetOpen(PbTick tick, double price)
 {
     if (tick.Bar == null)
         tick.Bar = new BarInfo();
     SetOpen(tick.Bar, price);
 }
Пример #30
0
 public PbTickView Data2View(PbTick tick, bool descending)
 {
     if (tick == null)
         return null;
     var converter = new Int2DoubleConverter();
     return converter.Int2Double(tick, descending);
 }
Пример #31
0
 public long GetVolume(PbTick tick)
 {
     return tick.Volume;
 }
Пример #32
0
 public void SetVolume(PbTick tick, long val)
 {
     tick.Volume = val;
 }
Пример #33
0
 public long GetOpenInterest(PbTick tick)
 {
     return tick.OpenInterest;
 }
Пример #34
0
 public void SetOpenInterest(PbTick tick, long val)
 {
     tick.OpenInterest = val;
 }
Пример #35
0
        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;
        }
Пример #36
0
 public long GetOpenInterest(PbTick tick)
 {
     return(tick.OpenInterest);
 }
Пример #37
0
 public double GetTurnover(PbTick tick)
 {
     return tick.Turnover * _config.TurnoverMultiplier;
 }
Пример #38
0
 public double GetAveragePrice(PbTick tick)
 {
     return (TickToPrice(tick.AveragePrice) / _config.AveragePriceMultiplier);
 }
Пример #39
0
 public DateTime GetActionDayDateTime(PbTick tick)
 {
     return GetDateTime(tick.ActionDay) + GetUpdateTime(tick);
 }
Пример #40
0
        /// <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;
        }
Пример #41
0
 public DateTime GetTradingDayDateTime(PbTick tick)
 {
     return GetDateTime(tick.TradingDay) + GetUpdateTime(tick);
 }
Пример #42
0
        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;
        }
Пример #43
0
 public void SetAveragePrice(PbTick tick, double price)
 {
     tick.AveragePrice = PriceToTick(price * _config.AveragePriceMultiplier);
 }
Пример #44
0
 public double GetAskPrice1(PbTick tick)
 {
     return TickToPrice(tick.AskPrice1);
 }
Пример #45
0
 public double GetAskPrice1(PbTick tick)
 {
     return(TickToPrice(tick.AskPrice1));
 }
Пример #46
0
 public void SetAskPrice1(PbTick tick, double price)
 {
     tick.AskPrice1 = PriceToTick(price);
 }
Пример #47
0
        /// <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);
        }
Пример #48
0
 public TimeSpan GetLocalTime(PbTick tick)
 {
     return GetUpdateTime(tick.Time_HHmm, tick.Time_____ssf__, tick.Time________ff, tick.LocalTime_Msec);
 }
Пример #49
0
 public double GetTurnover(PbTick tick)
 {
     return(tick.Turnover * _config.TurnoverMultiplier);
 }
Пример #50
0
 public void SetUpdateTime(PbTick tick, TimeSpan span)
 {
     SetUpdateTime(span, out tick.Time_HHmm, out tick.Time_____ssf__, out tick.Time________ff);
 }
Пример #51
0
        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);
        }
Пример #52
0
 public void SetUpdateTime(PbTick tick, int Time, int Millisec)
 {
     SetUpdateTime(Time, Millisec, out tick.Time_HHmm, out tick.Time_____ssf__, out tick.Time________ff);
 }
Пример #53
0
 public void SetVolume(PbTick tick, long val)
 {
     tick.Volume = val;
 }
Пример #54
0
 public void SetTurnover(PbTick tick, double val)
 {
     tick.Turnover = (long)(val / _config.TurnoverMultiplier);
 }
Пример #55
0
 public long GetVolume(PbTick tick)
 {
     return(tick.Volume);
 }
Пример #56
0
 public void Reset()
 {
     _lastWrite = null;
     _lastRead = null;
 }
Пример #57
0
 public void GetUpdateTime(PbTick tick, out int time, out int ms)
 {
     GetUpdateTime(tick.Time_HHmm, tick.Time_____ssf__, tick.Time________ff, out time, out ms);
 }
Пример #58
0
        /// <summary>
        /// 可以同时得到原始的raw和解码后的数据
        /// </summary>
        /// <param name="source"></param>
        /// <param name="raw"></param>
        /// <returns></returns>
        public PbTick ReadOne(Stream source)
        {
            PbTick raw = ProtoBuf.Serializer.DeserializeWithLengthPrefix<PbTick>(source, PrefixStyle.Base128);
            if (raw == null)
                return null;
            raw.PrepareObjectAfterRead(Codec.Config);

            _lastRead = Codec.Restore(_lastRead, raw);
            if (_lastRead.Config.Version != 2)
            {
                throw new ProtobufDataZeroException("only support pd0 file version 2", _lastRead.Config.Version, 2);
            }
            
            return _lastRead;
        }
Пример #59
0
        // 目前先不处理港股的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;
        }
Пример #60
0
 public TimeSpan GetUpdateTime(PbTick tick)
 {
     return GetUpdateTime(tick.Time_HHmm, tick.Time_____ssf__, tick.Time________ff, 0);
 }