Beispiel #1
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 <int>(1234, hhmm_____);
            Assert.AreEqual <int>(567, ____ssf__);
            Assert.AreEqual <int>(89, _______ff);

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

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

            span = codec.GetUpdateTime(hhmm_____, ____ssf__, _______ff);
            Assert.AreEqual <int>(0, span.Days);
            Assert.AreEqual <int>(12, span.Hours);
            Assert.AreEqual <int>(34, span.Minutes);
            Assert.AreEqual <int>(56, span.Seconds);
            Assert.AreEqual <int>(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 <int>(date1, tick.ActionDay);
            Assert.AreEqual <int>(date2, tick.TradingDay);
            Assert.AreEqual <DateTime>(new DateTime(2014, 11, 4), codec.GetDateTime(tick.ActionDay));
            Assert.AreEqual <DateTime>(new DateTime(2014, 11, 5), codec.GetDateTime(tick.TradingDay));
        }
        private DepthMarketDataField PbTick2DepthMarketDataField(PbTickCodec codec, PbTick tick)
        {
            PbTickView tickView = codec.Data2View(tick, false);

            DepthMarketDataField marketData = default(DepthMarketDataField);

            codec.GetUpdateTime(tick, out marketData.UpdateTime, out marketData.UpdateMillisec);

            marketData.TradingDay   = tickView.TradingDay;
            marketData.ActionDay    = tickView.ActionDay;
            marketData.LastPrice    = tickView.LastPrice;
            marketData.Volume       = tickView.Volume;
            marketData.Turnover     = tickView.Turnover;
            marketData.OpenInterest = tickView.OpenInterest;
            marketData.AveragePrice = tickView.AveragePrice;
            if (tickView.Bar != null)
            {
                marketData.OpenPrice    = tickView.Bar.Open;
                marketData.HighestPrice = tickView.Bar.High;
                marketData.LowestPrice  = tickView.Bar.Low;
                marketData.ClosePrice   = tickView.Bar.Close;
            }
            if (tickView.Static != null)
            {
                marketData.LowerLimitPrice = tickView.Static.LowerLimitPrice;
                marketData.UpperLimitPrice = tickView.Static.UpperLimitPrice;
                marketData.SettlementPrice = tickView.Static.SettlementPrice;
                marketData.Symbol          = tickView.Static.Symbol;
                if (!string.IsNullOrWhiteSpace(tickView.Static.Exchange))
                {
                    marketData.Exchange = Enum <ExchangeType> .Parse(tickView.Static.Exchange);
                }
            }

            int count = tickView.DepthList == null ? 0 : tickView.DepthList.Count;

            if (count > 0)
            {
                int AskPos  = DepthListHelper.FindAsk1Position(tickView.DepthList, tickView.AskPrice1);
                int BidPos  = AskPos - 1;
                int _BidPos = BidPos;
                if (_BidPos >= 0)
                {
                    marketData.BidPrice1  = tickView.DepthList[_BidPos].Price;
                    marketData.BidVolume1 = tickView.DepthList[_BidPos].Size;
                    --_BidPos;
                    if (_BidPos >= 0)
                    {
                        marketData.BidPrice2  = tickView.DepthList[_BidPos].Price;
                        marketData.BidVolume2 = tickView.DepthList[_BidPos].Size;
                        --_BidPos;
                        if (_BidPos >= 0)
                        {
                            marketData.BidPrice3  = tickView.DepthList[_BidPos].Price;
                            marketData.BidVolume3 = tickView.DepthList[_BidPos].Size;
                            --_BidPos;
                            if (_BidPos >= 0)
                            {
                                marketData.BidPrice4  = tickView.DepthList[_BidPos].Price;
                                marketData.BidVolume4 = tickView.DepthList[_BidPos].Size;
                                --_BidPos;
                                if (_BidPos >= 0)
                                {
                                    marketData.BidPrice5  = tickView.DepthList[_BidPos].Price;
                                    marketData.BidVolume5 = tickView.DepthList[_BidPos].Size;
                                }
                            }
                        }
                    }
                }

                int _AskPos = AskPos;
                if (_AskPos < count)
                {
                    marketData.AskPrice1  = tickView.DepthList[_AskPos].Price;
                    marketData.AskVolume1 = tickView.DepthList[_AskPos].Size;
                    ++_AskPos;
                    if (_AskPos < count)
                    {
                        marketData.AskPrice2  = tickView.DepthList[_AskPos].Price;
                        marketData.AskVolume2 = tickView.DepthList[_AskPos].Size;
                        ++_AskPos;
                        if (_AskPos < count)
                        {
                            marketData.AskPrice3  = tickView.DepthList[_AskPos].Price;
                            marketData.AskVolume3 = tickView.DepthList[_AskPos].Size;
                            ++_AskPos;
                            if (_AskPos < count)
                            {
                                marketData.AskPrice4  = tickView.DepthList[_AskPos].Price;
                                marketData.AskVolume4 = tickView.DepthList[_AskPos].Size;
                                ++_AskPos;
                                if (_AskPos < count)
                                {
                                    marketData.AskPrice5  = tickView.DepthList[_AskPos].Price;
                                    marketData.AskVolume5 = tickView.DepthList[_AskPos].Size;
                                }
                            }
                        }
                    }
                }
            }
            return(marketData);
        }
        public static PbTickStruct5 toStruct(PbTickView input)
        {
            var o = default(PbTickStruct5);

            codec.GetUpdateTime(input, out o.UpdateTime, out o.UpdateMillisec);

            o.TradingDay = input.TradingDay;
            o.ActionDay  = input.ActionDay;
            o.LastPrice  = (float)input.LastPrice;
            o.Volume     = input.Volume;

            o.Turnover     = (float)input.Turnover;
            o.OpenInterest = (float)input.OpenInterest;
            o.AveragePrice = (float)input.AveragePrice;

            if (input.Bar != null)
            {
                o.OpenPrice    = (float)input.Bar.Open;
                o.HighestPrice = (float)input.Bar.High;
                o.LowestPrice  = (float)input.Bar.Low;
                o.ClosePrice   = (float)input.Bar.Close;
            }

            if (input.Static != null)
            {
                o.LowerLimitPrice = (float)input.Static.LowerLimitPrice;
                o.UpperLimitPrice = (float)input.Static.UpperLimitPrice;
                o.SettlementPrice = (float)input.Static.SettlementPrice;
                o.Symbol          = input.Static.Symbol;
                o.Exchange        = input.Static.Exchange;
                //o.Symbol = "IF";
                //o.Exchange = "CFFEX";
                o.PreClosePrice      = (float)input.Static.PreClosePrice;
                o.PreSettlementPrice = (float)input.Static.PreSettlementPrice;
                o.PreOpenInterest    = input.Static.PreOpenInterest;
            }

            o.Price = new float[10];
            o.Size  = new int[10];

            int count = input.DepthList == null ? 0 : input.DepthList.Count;

            if (count > 0)
            {
                int AskPos   = DepthListHelper.FindAsk1Position(input.DepthList, input.AskPrice1); // 卖一位置
                int BidPos   = AskPos - 1;                                                         // 买一位置,在数组中的位置
                int BidCount = BidPos + 1;
                int AskCount = count - AskPos;


                if (BidCount > 0)
                {
                    int j = 0;
                    for (int i = BidPos; i >= 0; --i)
                    {
                        o.Price[4 - j] = (float)input.DepthList[i].Price;
                        o.Size[4 - j]  = input.DepthList[i].Size;

                        ++j;
                    }
                }

                if (AskCount > 0)
                {
                    int j = 0;
                    for (int i = AskPos; i < count; ++i)
                    {
                        o.Price[5 + j] = (float)input.DepthList[i].Price;
                        o.Size[5 + j]  = input.DepthList[i].Size;

                        ++j;
                    }
                }
            }

            return(o);
        }
Beispiel #4
0
        public void OutputSeries(out IDataSeries trades, out IDataSeries bids, out IDataSeries asks)
        {
            trades = new TickSeries();
            bids   = new TickSeries();
            asks   = new TickSeries();

            PbTickCodec codec          = new PbTickCodec();
            int         TradingDay     = -1;
            int         _lastTradeSize = 0;

            foreach (var s in Series)
            {
                if (TradingDay != s.TradingDay)
                {
                    _lastTradeSize = 0;
                    TradingDay     = s.TradingDay;
                }
                var dateTime = codec.GetDateTime(s.ActionDay == 0 ? s.TradingDay : s.ActionDay).Add(codec.GetUpdateTime(s));
                var tick     = PbTick2DepthMarketDataNClass(codec, s);

                if (SubscribeExternData)
                {
                    var trade = new TradeEx(dateTime, 0, _InstrumentId, tick.LastPrice, (int)tick.Volume);
                    trade.Size           -= _lastTradeSize;
                    trade.DepthMarketData = tick;
                    trades.Add(trade);
                }
                else
                {
                    var trade = new Trade(dateTime, 0, _InstrumentId, tick.LastPrice, (int)tick.Volume);
                    trade.Size -= _lastTradeSize;
                    trades.Add(trade);
                }


                if (tick.Bids != null && tick.Bids.Length > 0)
                {
                    var bid = new Bid(dateTime, 0, _InstrumentId, tick.Bids[0].Price, tick.Bids[0].Size);
                    bids.Add(bid);
                }
                if (tick.Asks != null && tick.Asks.Length > 0)
                {
                    var ask = new Ask(dateTime, 0, _InstrumentId, tick.Asks[0].Price, tick.Asks[0].Size);
                    asks.Add(ask);
                }

                _lastTradeSize = (int)tick.Volume;
            }
        }
Beispiel #5
0
        private DepthMarketDataNClass PbTick2DepthMarketDataNClass(PbTickCodec codec, PbTickView tickView)
        {
            DepthMarketDataNClass marketData = new DepthMarketDataNClass();

            codec.GetUpdateTime(tickView, out marketData.UpdateTime, out marketData.UpdateMillisec);

            marketData.TradingDay = tickView.TradingDay;
            marketData.ActionDay  = tickView.ActionDay;
            marketData.LastPrice  = tickView.LastPrice;
            marketData.Volume     = tickView.Volume;
            if (SubscribeExternData)
            {
                marketData.Turnover     = tickView.Turnover;
                marketData.OpenInterest = tickView.OpenInterest;
                marketData.AveragePrice = tickView.AveragePrice;
                if (tickView.Bar != null)
                {
                    marketData.OpenPrice    = tickView.Bar.Open;
                    marketData.HighestPrice = tickView.Bar.High;
                    marketData.LowestPrice  = tickView.Bar.Low;
                    marketData.ClosePrice   = tickView.Bar.Close;
                }
                if (tickView.Static != null)
                {
                    marketData.LowerLimitPrice = tickView.Static.LowerLimitPrice;
                    marketData.UpperLimitPrice = tickView.Static.UpperLimitPrice;
                    marketData.SettlementPrice = tickView.Static.SettlementPrice;
                    marketData.Symbol          = tickView.Static.Symbol;
                    if (!string.IsNullOrWhiteSpace(tickView.Static.Exchange))
                    {
                        marketData.Exchange = Enum <ExchangeType> .Parse(tickView.Static.Exchange);
                    }
                    marketData.PreClosePrice      = tickView.Static.PreClosePrice;
                    marketData.PreSettlementPrice = tickView.Static.PreSettlementPrice;
                    marketData.PreOpenInterest    = tickView.Static.PreOpenInterest;
                }
            }

            int count = tickView.DepthList == null ? 0 : tickView.DepthList.Count;

            if (count > 0)
            {
                int AskPos   = DepthListHelper.FindAsk1Position(tickView.DepthList, tickView.AskPrice1);
                int BidPos   = AskPos - 1;
                int BidCount = BidPos + 1;
                int AskCount = count - AskPos;

                marketData.Bids = new DepthField[0];
                marketData.Asks = new DepthField[0];

                if (SubscribeBid)
                {
                    if (BidCount > 0)
                    {
                        marketData.Bids = new DepthField[BidCount];
                        int j = 0;
                        for (int i = BidPos; i >= 0; --i)
                        {
                            marketData.Bids[j] = new DepthField()
                            {
                                Price = tickView.DepthList[i].Price,
                                Size  = tickView.DepthList[i].Size,
                                Count = tickView.DepthList[i].Count,
                            };
                            ++j;
                        }
                    }
                }
                if (SubscribeAsk)
                {
                    if (AskCount > 0)
                    {
                        marketData.Asks = new DepthField[AskCount];

                        int j = 0;
                        for (int i = AskPos; i < count; ++i)
                        {
                            marketData.Asks[j] = new DepthField()
                            {
                                Price = tickView.DepthList[i].Price,
                                Size  = tickView.DepthList[i].Size,
                                Count = tickView.DepthList[i].Count,
                            };
                            ++j;
                        }
                    }
                }
            }
            return(marketData);
        }