Esempio n. 1
0
        public DepthTick GetLevelObject(PbTick tick, int level)
        {
            if (level <= 0)
            {
                throw new InvalidOperationException("the level must be gt zero");
            }

            DepthTick from_last = null;
            DepthTick from_next = null;

            from_last = null;
            from_next = tick.Depth1_3;

            while (level > 0)
            {
                if (from_next == null)
                {
                    return(null);
                }

                from_last = from_next;
                from_next = from_next.Next;

                level -= 3;
            }

            return(from_last);
        }
Esempio n. 2
0
 public void SetClose(PbTick tick, double price)
 {
     if (tick.Bar == null)
     {
         tick.Bar = new BarInfo();
     }
     SetClose(tick.Bar, price);
 }
Esempio n. 3
0
 public void SetBarSize(PbTick tick, int barSize)
 {
     if (tick.Bar == null)
     {
         tick.Bar = new BarInfo();
     }
     SetBarSize(tick.Bar, barSize);
 }
Esempio n. 4
0
 public void SetSettlementPrice(PbTick tick, double price)
 {
     if (tick.Static == null)
     {
         tick.Static = new StaticInfo();
     }
     SetSettlementPrice(tick.Static, price);
 }
Esempio n. 5
0
 public void SetUpperLimitPrice(PbTick tick, double price)
 {
     if (tick.Static == null)
     {
         tick.Static = new StaticInfo();
     }
     SetUpperLimitPrice(tick.Static, price);
 }
Esempio n. 6
0
        public string GetSymbol(PbTick tick)
        {
            if (tick.Static == null)
            {
                return(null);
            }

            return(tick.Static.Symbol);
        }
Esempio n. 7
0
        public string GetExchange(PbTick tick)
        {
            if (tick.Static == null)
            {
                return(null);
            }

            return(tick.Static.Exchange);
        }
Esempio n. 8
0
 /// <summary>
 /// 支持同时写向多个Stream
 /// 这样,只要有Tick来了,可以将转码后的小数据同时放在FileStream和MemoryStream.
 /// MemoryStream可以用来网络传输
 /// </summary>
 /// <param name="data"></param>
 /// <param name="dest"></param>
 public PbTick Write(PbTick data, params Stream[] dest)
 {
     PbTick diff = Codec.Diff(_lastWrite, data);
     _lastWrite = data;
     foreach (var d in dest)
     {
         ProtoBuf.Serializer.SerializeWithLengthPrefix<PbTick>(d, diff, PrefixStyle.Base128);
     }
     return diff;
 }
Esempio n. 9
0
        /// <summary>
        /// 支持同时写向多个Stream
        /// 这样,只要有Tick来了,可以将转码后的小数据同时放在FileStream和MemoryStream.
        /// MemoryStream可以用来网络传输
        /// </summary>
        /// <param name="data"></param>
        /// <param name="dest"></param>
        public PbTick Write(PbTick data, params Stream[] dest)
        {
            var diff = Codec.Diff(_lastWrite, data);

            _lastWrite = data;
            foreach (var d in dest)
            {
                ProtoBuf.Serializer.SerializeWithLengthPrefix <PbTick>(d, diff, PrefixStyle.Base128);
            }
            return(diff);
        }
Esempio n. 10
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;
     _lastRead = Codec.Restore(_lastRead, raw);
     if (_lastRead.Config.Version != 1)
     {
         throw new ProtobufDataZeroException("only support pd0 file version 1", _lastRead.Config.Version, 1);
     }
     return _lastRead;
 }
Esempio n. 11
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;
        }
Esempio n. 12
0
        /// <summary>
        /// 可以同时得到原始的raw和解码后的数据
        /// </summary>
        /// <param name="source"></param>
        /// <param name="raw"></param>
        /// <returns></returns>
        public PbTick ReadOne(Stream source, bool unpackDepth = true)
        {
            var raw = ProtoBuf.Serializer.DeserializeWithLengthPrefix <PbTick>(source, PrefixStyle.Base128);

            if (raw == null)
            {
                return(null);
            }
            _lastRead = Codec.Restore(_lastRead, raw, unpackDepth);
            if (_lastRead.Config.Version != 1)
            {
                throw new ProtobufDataZeroException("only support pd0 file version 1", _lastRead.Config.Version, 1);
            }
            return(_lastRead);
        }
Esempio n. 13
0
        public void TestGetSetPrice()
        {
            var codec = new PbTickCodec();
            codec.Config.SetTickSize(0.2);

            PbTick tick = new PbTick();

            Assert.AreEqual(0, codec.GetBidPrice(tick, 1));
            Assert.AreEqual(0, codec.GetBidPrice(tick, 4));
            Assert.AreEqual(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(1, codec.GetBidPrice(tick, 1));
            Assert.AreEqual(2.4, codec.GetBidPrice(tick, 4));
            Assert.AreEqual(5.8, codec.GetBidPrice(tick, 10));



            Assert.AreEqual(0, codec.GetAskPrice(tick, 1));
            Assert.AreEqual(0, codec.GetAskPrice(tick, 4));
            Assert.AreEqual(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(-1.0, codec.GetAskPrice(tick, 1));
            Assert.AreEqual(2.4, codec.GetAskPrice(tick, 4));
            Assert.AreEqual(-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(1234.56, codec.GetSettlementPrice(tick), "SettlementPrice");

            codec.SetTurnover(tick, 4567.8);
            Assert.AreEqual(4567.8, codec.GetTurnover(tick), "Turnover");
        }
Esempio n. 14
0
        public List <PbTick> Restore(IEnumerable <PbTick> list, bool unpackDepth = true)
        {
            if (list == null)
            {
                return(null);
            }

            var _list = new List <PbTick>();

            PbTick last = null;

            foreach (var item in list)
            {
                last = Restore(last, item, unpackDepth);
                _list.Add(last);
            }
            return(_list);
        }
Esempio n. 15
0
        public List <PbTick> Restore(IEnumerable <PbTick> list)
        {
            if (list == null)
            {
                return(null);
            }

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

            PbTick last = null;

            foreach (var item in list)
            {
                last = Restore(last, item);
                _list.Add(last);
            }
            return(_list);
        }
Esempio n. 16
0
        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(1234, hhmm_____);
            Assert.AreEqual(567, ____ssf__);
            Assert.AreEqual(89, _______ff);

            codec.SetUpdateTime(time, ms, out hhmm_____, out ____ssf__, out _______ff);
            Assert.AreEqual(1234, hhmm_____);
            Assert.AreEqual(567, ____ssf__);
            Assert.AreEqual(89, _______ff);

            codec.GetUpdateTime(hhmm_____, ____ssf__, _______ff, out time, out ms);
            Assert.AreEqual(123456, time);
            Assert.AreEqual(789, ms);

            span = codec.GetUpdateTime(hhmm_____, ____ssf__, _______ff);
            Assert.AreEqual(0, span.Days);
            Assert.AreEqual(12, span.Hours);
            Assert.AreEqual(34, span.Minutes);
            Assert.AreEqual(56, span.Seconds);
            Assert.AreEqual(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(date1, tick.ActionDay);
            Assert.AreEqual(date2, tick.TradingDay);
            Assert.AreEqual(new DateTime(2014, 11, 4), codec.GetDateTime(tick.ActionDay));
            Assert.AreEqual(new DateTime(2014, 11, 5), codec.GetDateTime(tick.TradingDay));
        }
Esempio n. 17
0
        public List <PbTick> Diff(IEnumerable <PbTick> list)
        {
            if (list == null)
            {
                return(null);
            }

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

            PbTick last = null;

            foreach (var item in list)
            {
                PbTick diff = Diff(last, item);
                last = item;
                _list.Add(diff);
            }
            return(_list);
        }
Esempio n. 18
0
        public void SetAskPrice(PbTick tick, int level, double price)
        {
            var next = CreateLevelObject(tick, level);

            switch (level % 3)
            {
            case 1:
                next.AskPrice1 = PriceToTick(price);
                break;

            case 2:
                next.AskPrice2 = PriceToTick(price);
                break;

            case 0:
                next.AskPrice3 = PriceToTick(price);
                break;
            }
        }
Esempio n. 19
0
        public void SetAskSize(PbTick tick, int level, int size)
        {
            var next = CreateLevelObject(tick, level);

            switch (level % 3)
            {
            case 1:
                next.AskSize1 = size;
                break;

            case 2:
                next.AskSize2 = size;
                break;

            case 0:
                next.AskSize3 = size;
                break;
            }
        }
Esempio n. 20
0
        public void SetAskCount(PbTick tick, int level, int count)
        {
            var next = CreateLevelObject(tick, level);

            switch (level % 3)
            {
            case 1:
                next.AskCount1 = count;
                break;

            case 2:
                next.AskCount2 = count;
                break;

            case 0:
                next.AskCount3 = count;
                break;
            }
        }
Esempio n. 21
0
        public PbTick Double2Int(PbTickView tick)
        {
            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.Depth1_3     = Double2Int(tick.Depth1_3);
            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);


            return(field);
        }
Esempio n. 22
0
        public int GetAskCount(PbTick tick, int level)
        {
            var next = GetLevelObject(tick, level);

            if (next == null)
            {
                return(0);
            }

            switch (level % 3)
            {
            case 1:
                return(next.AskCount1);

            case 2:
                return(next.AskCount2);

            case 0:
                return(next.AskCount3);
            }
            return(0);
        }
Esempio n. 23
0
        public int GetBidSize(PbTick tick, int level)
        {
            var next = GetLevelObject(tick, level);

            if (next == null)
            {
                return(0);
            }

            switch (level % 3)
            {
            case 1:
                return(next.BidSize1);

            case 2:
                return(next.BidSize2);

            case 0:
                return(next.BidSize3);
            }
            return(0);
        }
Esempio n. 24
0
        public PbTickView Int2Double(PbTick tick)
        {
            if (tick == null)
            {
                return(null);
            }

            var field = new PbTickView();

            // 利用此机会设置TickSize
            if (Codec == null)
            {
                Codec = new PbTickCodec();
            }
            field.Config = Int2Double(tick.Config);

            Codec.Config = tick.Config;

            field.Turnover     = Codec.GetTurnover(tick);
            field.AveragePrice = Codec.GetAveragePrice(tick);

            field.LastPrice = Codec.TickToPrice(tick.LastPrice);

            field.Depth1_3     = Int2Double(tick.Depth1_3);
            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    = Int2Double(tick.Bar);
            field.Static = Int2Double(tick.Static);
            field.Split  = Int2Double(tick.Split);

            return(field);
        }
Esempio n. 25
0
        public double GetAskPrice(PbTick tick, int level)
        {
            var next = GetLevelObject(tick, level);

            if (next == null)
            {
                return(0);
            }

            switch (level % 3)
            {
            case 1:
                return(TickToPrice(next.AskPrice1));

            case 2:
                return(TickToPrice(next.AskPrice2));

            case 0:
                return(TickToPrice(next.AskPrice3));
            }
            return(0);
        }
Esempio n. 26
0
 public void SetAveragePrice(PbTick tick, double price)
 {
     tick.AveragePrice = PriceToTick(price * _config.AveragePriceMultiplier);
 }
Esempio n. 27
0
 public void SetTurnover(PbTick tick, double val)
 {
     tick.Turnover = (long)(val / _config.TurnoverMultiplier);
 }
Esempio n. 28
0
 public long GetVolume(PbTick tick)
 {
     return(tick.Volume);
 }
Esempio n. 29
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);
 }
Esempio n. 30
0
 public void SetLastPrice(PbTick tick, double price)
 {
     tick.LastPrice = PriceToTick(price);
 }
Esempio n. 31
0
 public DateTime GetTradingDayDateTime(PbTick tick)
 {
     return GetDateTime(tick.TradingDay) + GetUpdateTime(tick);
 }
Esempio n. 32
0
        public double GetAskPrice(PbTick tick, int level)
        {
            var next = GetLevelObject(tick, level);
            if (next == null)
                return 0;

            switch (level % 3)
            {
                case 1:
                    return TickToPrice(next.AskPrice1);
                case 2:
                    return TickToPrice(next.AskPrice2);
                case 0:
                    return TickToPrice(next.AskPrice3);
            }
            return 0;
        }
Esempio n. 33
0
        public int GetAskCount(PbTick tick, int level)
        {
            var next = GetLevelObject(tick, level);
            if (next == null)
                return 0;

            switch (level % 3)
            {
                case 1:
                    return next.AskCount1;
                case 2:
                    return next.AskCount2;
                case 0:
                    return next.AskCount3;
            }
            return 0;
        }
Esempio n. 34
0
 public long GetOpenInterest(PbTick tick)
 {
     return tick.OpenInterest;
 }
Esempio n. 35
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;
            }

            PbTick tick = new PbTick();

            #region 配置数据
            // 当前数据为空或前后相同,表示
            if(current.Config == null || prev.Config.IsSame(current.Config))
            {
                tick.Config = null;
                // 可以继续下去
            }
            else
            {
                // 是新数据,返回快照
                _config = current.Config;
                TickSize = _config.GetTickSize();

                return current;
            }
            #endregion

            // 先取最关键的数据,因为前一条的config总会补成有效
            _config = prev.Config;
            TickSize = _config.GetTickSize();

            tick.LastPrice = current.LastPrice - prev.LastPrice;

            DepthTick from_last = null;
            DepthTick from_next = null;
            DepthTick to_last = null;
            DepthTick to_next = null;

            DepthTick last_last = null;
            DepthTick last_next = null;

            #region 买1到买N
            from_last = null;
            from_next = current.Depth1_3;
            to_last = null;
            to_next = tick.Depth1_3;

            last_last = null;
            last_next = prev.Depth1_3;

            while (true)
            {
                // 如果下一步没有数据,直接跳过
                if (from_next == null)
                    break;

                // 每节的循环
                if (from_next.BidSize1 == 0)
                    break;

                // 如果没有上一笔,这个地方就比较特殊
                if (from_last == null)
                {
                    // 需要考察目标是否有数据
                    if (to_next == null)
                    {
                        to_next = new DepthTick();
                        tick.Depth1_3 = to_next;
                    }
                    to_next.BidPrice1 = current.LastPrice - from_next.BidPrice1;
                }
                else
                {
                    // 需要考察目标是否有数据
                    if (to_next == null)
                    {
                        to_next = new DepthTick();
                        to_last.Next = to_next;
                    }
                    to_next.BidPrice1 = from_last.BidPrice3 - from_next.BidPrice1 - 1;
                }
                if (last_next == null)
                {
                    to_next.BidSize1 = from_next.BidSize1;
                    to_next.BidCount1 = from_next.BidCount1;
                }
                else
                {
                    to_next.BidSize1 = from_next.BidSize1 - last_next.BidSize1;
                    to_next.BidCount1 = from_next.BidCount1 - last_next.BidCount1;
                }


                if (from_next.BidSize2 == 0)
                    break;
                to_next.BidPrice2 = from_next.BidPrice1 - from_next.BidPrice2 - 1;
                if (last_next == null)
                {
                    to_next.BidSize2 = from_next.BidSize2;
                    to_next.BidCount2 = from_next.BidCount2;
                }
                else
                {
                    to_next.BidSize2 = from_next.BidSize2 - last_next.BidSize2;
                    to_next.BidCount2 = from_next.BidCount2 - last_next.BidCount2;
                }


                if (from_next.BidSize3 == 0)
                    break;

                to_next.BidPrice3 = from_next.BidPrice2 - from_next.BidPrice3 - 1;
                if (last_next == null)
                {
                    to_next.BidSize3 = from_next.BidSize3;
                    to_next.BidCount3 = from_next.BidCount3;
                }
                else
                {
                    to_next.BidSize3 = from_next.BidSize3 - last_next.BidSize3;
                    to_next.BidCount3 = from_next.BidCount3 - last_next.BidCount3;
                }

                // 移动到下一个数据块
                from_last = from_next;
                from_next = from_next.Next;

                to_last = to_next;
                to_next = to_next.Next;

                last_last = last_next;
                if (last_next != null)
                    last_next = last_next.Next;
            }
            #endregion

            #region 卖1到卖N
            from_last = null;
            from_next = current.Depth1_3;
            to_last = null;
            to_next = tick.Depth1_3;

            last_last = null;
            last_next = prev.Depth1_3;

            while (true)
            {
                // 如果下一步没有数据,直接跳过
                if (from_next == null)
                    break;

                // 每节的循环
                if (from_next.AskSize1 == 0)
                    break;

                // 如果没有上一笔,这个地方就比较特殊
                if (from_last == null)
                {
                    // 需要考察目标是否有数据
                    if (to_next == null)
                    {
                        to_next = new DepthTick();
                        tick.Depth1_3 = to_next;
                    }
                    to_next.AskPrice1 = from_next.AskPrice1 - current.LastPrice;
                }
                else
                {
                    // 需要考察目标是否有数据
                    if (to_next == null)
                    {
                        to_next = new DepthTick();
                        to_last.Next = to_next;
                    }
                    to_next.AskPrice1 = from_next.AskPrice1 - from_last.AskPrice3 - 1;
                }
                if (last_next == null)
                {
                    to_next.AskSize1 = from_next.AskSize1;
                    to_next.AskCount1 = from_next.AskCount1;
                }
                else
                {
                    to_next.AskSize1 = from_next.AskSize1 - last_next.AskSize1;
                    to_next.AskCount1 = from_next.AskCount1 - last_next.AskCount1;
                }

                if (from_next.AskSize2 == 0)
                    break;
                to_next.AskPrice2 = from_next.AskPrice2 - from_next.AskPrice1 - 1;
                if (last_next == null)
                {
                    to_next.AskSize2 = from_next.AskSize2;
                    to_next.AskCount2 = from_next.AskCount2;
                }
                else
                {
                    to_next.AskSize2 = from_next.AskSize2 - last_next.AskSize2;
                    to_next.AskCount2 = from_next.AskCount2 - last_next.AskCount2;
                }


                if (from_next.AskSize3 == 0)
                    break;
                to_next.AskPrice3 = from_next.AskPrice3 - from_next.AskPrice2 - 1;
                if (last_next == null)
                {
                    to_next.AskSize3 = from_next.AskSize3;
                    to_next.AskCount3 = from_next.AskCount3;
                }
                else
                {
                    to_next.AskSize3 = from_next.AskSize3 - last_next.AskSize3;
                    to_next.AskCount3 = from_next.AskCount3 - last_next.AskCount3;
                }

                // 移动到下一个数据块
                from_last = from_next;
                from_next = from_next.Next;

                to_last = to_next;
                to_next = to_next.Next;

                last_last = last_next;
                if (last_next != null)
                    last_next = last_next.Next;
            }
            #endregion

            #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;
            #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;
        }
Esempio n. 36
0
 public void SetOpenInterest(PbTick tick, long val)
 {
     tick.OpenInterest = val;
 }
Esempio n. 37
0
 public double GetLastPrice(PbTick tick)
 {
     return TickToPrice(tick.LastPrice);
 }
Esempio n. 38
0
        public void SetAskSize(PbTick tick, int level, int size)
        {
            var next = CreateLevelObject(tick, level);

            switch (level % 3)
            {
                case 1:
                    next.AskSize1 = size;
                    break;
                case 2:
                    next.AskSize2 = size;
                    break;
                case 0:
                    next.AskSize3 = size;
                    break;
            }
        }
Esempio n. 39
0
        public int GetBidSize(PbTick tick, int level)
        {
            var next = GetLevelObject(tick, level);
            if (next == null)
                return 0;

            switch (level % 3)
            {
                case 1:
                    return next.BidSize1;
                case 2:
                    return next.BidSize2;
                case 0:
                    return next.BidSize3;
            }
            return 0;
        }
Esempio n. 40
0
 public void SetOpen(PbTick tick, double price)
 {
     if (tick.Bar == null)
         tick.Bar = new BarInfo();
     SetOpen(tick.Bar, price);
 }
Esempio n. 41
0
        public void SetAskPrice(PbTick tick, int level, double price)
        {
            var next = CreateLevelObject(tick, level);

            switch (level % 3)
            {
                case 1:
                    next.AskPrice1 = PriceToTick(price);
                    break;
                case 2:
                    next.AskPrice2 = PriceToTick(price);
                    break;
                case 0:
                    next.AskPrice3 = PriceToTick(price);
                    break;
            }
        }
Esempio n. 42
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);
 }
Esempio n. 43
0
 public void SetUpdateTime(PbTick tick, TimeSpan span)
 {
     SetUpdateTime(span, out tick.Time_HHmm, out tick.Time_____ssf__, out tick.Time________ff);
 }
Esempio n. 44
0
 public TimeSpan GetUpdateTime(PbTick tick)
 {
     return GetUpdateTime(tick.Time_HHmm, tick.Time_____ssf__, tick.Time________ff);
 }
Esempio n. 45
0
 public TimeSpan GetUpdateTime(PbTick tick)
 {
     return(GetUpdateTime(tick.Time_HHmm, tick.Time_____ssf__, tick.Time________ff));
 }
Esempio n. 46
0
 public void SetActionDay(PbTick tick, DateTime date)
 {
     tick.ActionDay = SetDateTime(date);
 }
Esempio n. 47
0
 public long GetOpenInterest(PbTick tick)
 {
     return(tick.OpenInterest);
 }
Esempio n. 48
0
 public double GetTurnover(PbTick tick)
 {
     return tick.Turnover * _config.TurnoverMultiplier;
 }
Esempio n. 49
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);
 }
Esempio n. 50
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);
            }

            PbTick tick = new PbTick();

            #region 配置数据
            // 当前数据为空或前后相同,表示
            if (current.Config == null || prev.Config.IsSame(current.Config))
            {
                tick.Config = null;
                // 可以继续下去
            }
            else
            {
                // 是新数据,返回快照
                _config  = current.Config;
                TickSize = _config.GetTickSize();

                return(current);
            }
            #endregion

            // 先取最关键的数据,因为前一条的config总会补成有效
            _config  = prev.Config;
            TickSize = _config.GetTickSize();

            tick.LastPrice = current.LastPrice - prev.LastPrice;

            DepthTick from_last = null;
            DepthTick from_next = null;
            DepthTick to_last   = null;
            DepthTick to_next   = null;

            DepthTick last_last = null;
            DepthTick last_next = null;

            #region 买1到买N
            from_last = null;
            from_next = current.Depth1_3;
            to_last   = null;
            to_next   = tick.Depth1_3;

            last_last = null;
            last_next = prev.Depth1_3;

            while (true)
            {
                // 如果下一步没有数据,直接跳过
                if (from_next == null)
                {
                    break;
                }

                // 每节的循环
                if (from_next.BidSize1 == 0)
                {
                    break;
                }

                // 如果没有上一笔,这个地方就比较特殊
                if (from_last == null)
                {
                    // 需要考察目标是否有数据
                    if (to_next == null)
                    {
                        to_next       = new DepthTick();
                        tick.Depth1_3 = to_next;
                    }
                    to_next.BidPrice1 = current.LastPrice - from_next.BidPrice1;
                }
                else
                {
                    // 需要考察目标是否有数据
                    if (to_next == null)
                    {
                        to_next      = new DepthTick();
                        to_last.Next = to_next;
                    }
                    to_next.BidPrice1 = from_last.BidPrice3 - from_next.BidPrice1 - 1;
                }
                if (last_next == null)
                {
                    to_next.BidSize1  = from_next.BidSize1;
                    to_next.BidCount1 = from_next.BidCount1;
                }
                else
                {
                    to_next.BidSize1  = from_next.BidSize1 - last_next.BidSize1;
                    to_next.BidCount1 = from_next.BidCount1 - last_next.BidCount1;
                }


                if (from_next.BidSize2 == 0)
                {
                    break;
                }
                to_next.BidPrice2 = from_next.BidPrice1 - from_next.BidPrice2 - 1;
                if (last_next == null)
                {
                    to_next.BidSize2  = from_next.BidSize2;
                    to_next.BidCount2 = from_next.BidCount2;
                }
                else
                {
                    to_next.BidSize2  = from_next.BidSize2 - last_next.BidSize2;
                    to_next.BidCount2 = from_next.BidCount2 - last_next.BidCount2;
                }


                if (from_next.BidSize3 == 0)
                {
                    break;
                }

                to_next.BidPrice3 = from_next.BidPrice2 - from_next.BidPrice3 - 1;
                if (last_next == null)
                {
                    to_next.BidSize3  = from_next.BidSize3;
                    to_next.BidCount3 = from_next.BidCount3;
                }
                else
                {
                    to_next.BidSize3  = from_next.BidSize3 - last_next.BidSize3;
                    to_next.BidCount3 = from_next.BidCount3 - last_next.BidCount3;
                }

                // 移动到下一个数据块
                from_last = from_next;
                from_next = from_next.Next;

                to_last = to_next;
                to_next = to_next.Next;

                last_last = last_next;
                if (last_next != null)
                {
                    last_next = last_next.Next;
                }
            }
            #endregion

            #region 卖1到卖N
            from_last = null;
            from_next = current.Depth1_3;
            to_last   = null;
            to_next   = tick.Depth1_3;

            last_last = null;
            last_next = prev.Depth1_3;

            while (true)
            {
                // 如果下一步没有数据,直接跳过
                if (from_next == null)
                {
                    break;
                }

                // 每节的循环
                if (from_next.AskSize1 == 0)
                {
                    break;
                }

                // 如果没有上一笔,这个地方就比较特殊
                if (from_last == null)
                {
                    // 需要考察目标是否有数据
                    if (to_next == null)
                    {
                        to_next       = new DepthTick();
                        tick.Depth1_3 = to_next;
                    }
                    to_next.AskPrice1 = from_next.AskPrice1 - current.LastPrice;
                }
                else
                {
                    // 需要考察目标是否有数据
                    if (to_next == null)
                    {
                        to_next      = new DepthTick();
                        to_last.Next = to_next;
                    }
                    to_next.AskPrice1 = from_next.AskPrice1 - from_last.AskPrice3 - 1;
                }
                if (last_next == null)
                {
                    to_next.AskSize1  = from_next.AskSize1;
                    to_next.AskCount1 = from_next.AskCount1;
                }
                else
                {
                    to_next.AskSize1  = from_next.AskSize1 - last_next.AskSize1;
                    to_next.AskCount1 = from_next.AskCount1 - last_next.AskCount1;
                }

                if (from_next.AskSize2 == 0)
                {
                    break;
                }
                to_next.AskPrice2 = from_next.AskPrice2 - from_next.AskPrice1 - 1;
                if (last_next == null)
                {
                    to_next.AskSize2  = from_next.AskSize2;
                    to_next.AskCount2 = from_next.AskCount2;
                }
                else
                {
                    to_next.AskSize2  = from_next.AskSize2 - last_next.AskSize2;
                    to_next.AskCount2 = from_next.AskCount2 - last_next.AskCount2;
                }


                if (from_next.AskSize3 == 0)
                {
                    break;
                }
                to_next.AskPrice3 = from_next.AskPrice3 - from_next.AskPrice2 - 1;
                if (last_next == null)
                {
                    to_next.AskSize3  = from_next.AskSize3;
                    to_next.AskCount3 = from_next.AskCount3;
                }
                else
                {
                    to_next.AskSize3  = from_next.AskSize3 - last_next.AskSize3;
                    to_next.AskCount3 = from_next.AskCount3 - last_next.AskCount3;
                }

                // 移动到下一个数据块
                from_last = from_next;
                from_next = from_next.Next;

                to_last = to_next;
                to_next = to_next.Next;

                last_last = last_next;
                if (last_next != null)
                {
                    last_next = last_next.Next;
                }
            }
            #endregion

            #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;
            #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);
        }
Esempio n. 51
0
 public void SetLastPrice(PbTick tick, double price)
 {
     tick.LastPrice = PriceToTick(price);
 }
Esempio n. 52
0
 public DateTime GetActionDayDateTime(PbTick tick)
 {
     return GetDateTime(tick.ActionDay) + GetUpdateTime(tick);
 }
Esempio n. 53
0
 public double GetLastPrice(PbTick tick)
 {
     return(TickToPrice(tick.LastPrice));
 }
Esempio n. 54
0
 public double GetAveragePrice(PbTick tick)
 {
     return (TickToPrice(tick.AveragePrice) / _config.AveragePriceMultiplier);
 }
Esempio n. 55
0
 public void SetVolume(PbTick tick, long val)
 {
     tick.Volume = val;
 }
Esempio n. 56
0
        public void SetAskCount(PbTick tick, int level, int count)
        {
            var next = CreateLevelObject(tick, level);

            switch (level % 3)
            {
                case 1:
                    next.AskCount1 = count;
                    break;
                case 2:
                    next.AskCount2 = count;
                    break;
                case 0:
                    next.AskCount3 = count;
                    break;
            }
        }
Esempio n. 57
0
 public void SetVolume(PbTick tick, long val)
 {
     tick.Volume = val;
 }
Esempio n. 58
0
 public void SetTradingDay(PbTick tick, DateTime date)
 {
     tick.TradingDay = SetDateTime(date);
 }
Esempio n. 59
0
 public static void WriteOne(PbTick tick, Stream stream)
 {
     ProtoBuf.Serializer.SerializeWithLengthPrefix <PbTick>(stream, tick, PrefixStyle.Base128);
 }
Esempio n. 60
0
 public long GetVolume(PbTick tick)
 {
     return tick.Volume;
 }