예제 #1
0
        public void TestTickMultiplier()
        {
            PbTickCodec codec = new PbTickCodec();

            codec.Config.SetTickSize(0.2);
            codec.Config.ContractMultiplier = 50000;
            codec.Config.Time_ssf_Diff      = 5;
            codec.UseFlat(false);

            PbTick tick1 = new PbTick();

            codec.SetAveragePrice(tick1, 123.45);
            codec.SetSettlementPrice(tick1, 123.45);
            codec.SetTurnover(tick1, 1234567890123456);
            codec.SetOpenInterest(tick1, 9123456789012345678);
            codec.SetVolume(tick1, 1234567890);



            Assert.AreEqual <double>(123.45, codec.GetAveragePrice(tick1));
            Assert.AreEqual <double>(123.45, codec.GetSettlementPrice(tick1));
            Assert.AreEqual <double>(1234567890120000, codec.GetTurnover(tick1));
            Assert.AreEqual <long>(9123456789012345678, codec.GetOpenInterest(tick1));
            Assert.AreEqual <long>(1234567890, codec.GetVolume(tick1));

            codec.Config.ContractMultiplier = 5;
            codec.SetTurnover(tick1, 1234567890123456);
            Assert.AreEqual <double>(1234567890123456, codec.GetTurnover(tick1));

            codec.Config.ContractMultiplier = 0.1;
            codec.SetTurnover(tick1, 12345678901234.56);
            Assert.AreEqual <double>(12345678901234.56, codec.GetTurnover(tick1));
        }
예제 #2
0
        public void TestGetSetPrice()
        {
            PbTickCodec codec = new PbTickCodec();

            codec.Config.SetTickSize(0.2);
            codec.UseFlat(false);

            PbTick tick = new PbTick();

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



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

            codec.SetTurnover(tick, 4567.8);
            Assert.AreEqual <double>(4567.8, codec.GetTurnover(tick), "Turnover");
        }
예제 #3
0
        public void ReadFile(int instrumentId, string path)
        {
            Bars   = new BarSeries();
            Trades = new TickSeries();
            Asks   = new TickSeries();
            Bids   = new TickSeries();

            PbTickSerializer pts = new PbTickSerializer();

            PbTick restore = null;

            using (Stream stream = File.OpenRead(path))
            {
                while (true)
                {
                    restore = pts.ReadOne(stream);
                    if (restore == null)
                    {
                        break;
                    }

                    Trade t = new Trade();
                    t.InstrumentId = instrumentId;
                    t.DateTime     = pts.Codec.GetActionDayDateTime(restore);
                    t.Price        = pts.Codec.GetLastPrice(restore);
                    t.Size         = (int)pts.Codec.GetVolume(restore);

                    Trades.Add(t);

                    Bid b = new Bid();
                    b.InstrumentId = instrumentId;
                    b.DateTime     = t.DateTime;
                    b.Price        = pts.Codec.GetBidPrice(restore, 1);
                    b.Size         = pts.Codec.GetBidSize(restore, 1);

                    Bids.Add(b);

                    Ask a = new Ask();
                    a.InstrumentId = instrumentId;
                    a.DateTime     = t.DateTime;
                    a.Price        = pts.Codec.GetAskPrice(restore, 1);
                    a.Size         = pts.Codec.GetAskSize(restore, 1);

                    Asks.Add(a);
                }
                stream.Close();
            }
        }
예제 #4
0
            public void MakeTick()
            {
                tick        = new PbTick();
                tick.Config = new ConfigInfo().Default();

                CalcTickSize();

                tick.Config.SetTickSize(TickSize);
                tick.Config.Time_ssf_Diff = 10;


                Codec.Config = tick.Config;

                int HH = int.Parse(time.Substring(0, 2));
                int mm = int.Parse(time.Substring(3, 2));
                int ss = int.Parse(time.Substring(6, 2));

                tick.Time_HHmm      = HH * 100 + mm;
                tick.Time_____ssf__ = ss * 10;

                tick.TradingDay = 20150120;

                //if()
                //{

                //}
                tick.DepthList = new List <DepthItem>();

                int i = 0;

                Bids.Reverse();
                foreach (var b in Bids)
                {
                    tick.DepthList.Add(new DepthItem(Codec.PriceToTick(b.price), b.size, 0));
                }

                foreach (var a in Asks)
                {
                    tick.DepthList.Add(new DepthItem(Codec.PriceToTick(a.price), a.size, 0));
                }
                if (Asks.Count > 0)
                {
                    Codec.SetAskPrice1(tick, Asks[0].price);
                }
                Codec.SetSymbol(tick, symbol);
            }
예제 #5
0
            public void MakeTick()
            {
                tick        = new PbTick();
                tick.Config = new ConfigInfo().Default();

                CalcTickSize();

                tick.Config.SetTickSize(TickSize);
                tick.Config.Time_ssf_Diff = 10;


                Codec.Config = tick.Config;
                Codec.UseFlat(false);

                int HH = int.Parse(time.Substring(0, 2));
                int mm = int.Parse(time.Substring(3, 2));
                int ss = int.Parse(time.Substring(6, 2));

                tick.Time_HHmm      = HH * 100 + mm;
                tick.Time_____ssf__ = ss * 10;

                tick.TradingDay = 20150120;

                //if()
                //{

                //}

                int i = 0;

                foreach (var b in Bids)
                {
                    ++i;
                    Codec.SetBidPrice(tick, i, b.price);
                    Codec.SetBidSize(tick, i, b.size);
                }
                i = 0;
                foreach (var a in Asks)
                {
                    ++i;
                    Codec.SetAskPrice(tick, i, a.price);
                    Codec.SetAskSize(tick, i, a.size);
                }
                Codec.SetSymbol(tick, symbol);
            }
예제 #6
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));
        }
예제 #7
0
        public static void _Main(string[] args)
        {
            string     symbol     = "AAPL";
            Framework  framework  = Framework.Current;
            Instrument instrument = framework.InstrumentManager.Get(symbol);
            DataSeries ds         = framework.DataManager.GetDataSeries(instrument, DataObjectType.Trade);

            PbTickSerializer pts = new PbTickSerializer();

            // 最关键的部分,需要提前设置
            pts.Codec.Config.SetTickSize(0.01);

            PbTick last = new PbTick();

            using (Stream stream = File.OpenWrite(@"D:\1.data"))
            {
                for (int i = 0; i < ds.Count; ++i)
                {
                    var    d    = ds[i];
                    Trade  t    = d as Trade;
                    PbTick tick = new PbTick();

                    // 必须设置数据中的TickSize
                    tick.Config = pts.Codec.Config;

                    pts.Codec.SetLastPrice(tick, t.Price);
                    pts.Codec.SetVolume(tick, t.Size);
                    pts.Codec.SetSymbol(tick, symbol);

                    // 时间设置也很关键
                    pts.Codec.SetActionDay(tick, t.DateTime.Date);
                    pts.Codec.SetUpdateTime(tick, t.DateTime - t.DateTime.Date);

                    // 写入流
                    pts.Write(tick, new Stream[] { stream });
                }
                stream.Close();
            }
        }
        public void TestStatic()
        {
            PbTickCodec codec = new PbTickCodec();

            codec.Config.SetTickSize(0.2);


            PbTick tick1 = new PbTick();

            tick1.Config = codec.Config;

            codec.SetSymbol(tick1, "ABC");
            codec.SetExchange(tick1, "DEF");

            PbTick tick2 = new PbTick();

            codec.SetSymbol(tick2, "ABC");
            codec.SetExchange(tick2, "DEF");

            var diff = codec.Diff(tick1, tick2);

            Assert.AreEqual(null, diff.Static);
        }
예제 #9
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);
        }
        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);
        }
예제 #11
0
        public void TestReadCsvLeve2()
        {
            FileInfo fi = new FileInfo(@"F:\BaiduYunDownload\20141225\20141225.csv");
            FileInfo fo = new FileInfo(@"F:\BaiduYunDownload\20141225\20141225.pd0");

            PbTickSerializer pts = new PbTickSerializer();


            using (Stream stream = File.Open(@"F:\BaiduYunDownload\20141225\20141225.pd0", FileMode.Create))
            {
                using (StreamReader file = new StreamReader(fi.OpenRead()))
                {
                    int    i   = 0;
                    string str = file.ReadLine();
                    do
                    {
                        ++i;
                        str = file.ReadLine();
                        if (str == null)
                        {
                            break;
                        }

                        string[] arr = str.Split(',');

                        PbTick tick = new PbTick();

                        pts.Codec.SetSymbol(tick, arr[0]);

                        tick.Config = new ConfigInfo().Default();

                        if (arr[0].StartsWith("TF"))
                        {
                            tick.Config.SetTickSize(0.002);
                            tick.Config.ContractMultiplier = 10000;
                        }
                        else
                        {
                            tick.Config.SetTickSize(0.2);
                            tick.Config.ContractMultiplier = 300;
                        }
                        tick.Config.Time_ssf_Diff = 5;

                        pts.Codec.Config = tick.Config;
                        pts.Codec.UseFlat(false);

                        //if(i == 1)
                        //{
                        //    tick.Split = new StockSplitInfo();
                        //    tick.Split.StockDividend = 10;
                        //}
                        //if(i == 2)
                        //{
                        //    tick.Split = new StockSplitInfo();
                        //    tick.Split.CashDividend = 10;
                        //}

                        tick.ActionDay = int.Parse(arr[5]);
                        int time = int.Parse(arr[6]);

                        tick.Time_HHmm      = time / 100000;
                        tick.Time_____ssf__ = time % 100000 / 100;
                        tick.Time________ff = time % 100;

                        pts.Codec.SetLastPrice(tick, double.Parse(arr[8]));
                        pts.Codec.SetHigh(tick, double.Parse(arr[9]));
                        pts.Codec.SetLow(tick, double.Parse(arr[10]));
                        pts.Codec.SetVolume(tick, int.Parse(arr[11]));
                        pts.Codec.SetTurnover(tick, int.Parse(arr[12]));

                        pts.Codec.SetOpenInterest(tick, int.Parse(arr[16]));

                        pts.Codec.SetAskPrice(tick, 1, double.Parse(arr[17]));
                        pts.Codec.SetAskPrice(tick, 2, double.Parse(arr[18]));
                        pts.Codec.SetAskPrice(tick, 3, double.Parse(arr[19]));
                        pts.Codec.SetAskPrice(tick, 4, double.Parse(arr[20]));
                        pts.Codec.SetAskPrice(tick, 5, double.Parse(arr[21]));

                        pts.Codec.SetAskSize(tick, 1, int.Parse(arr[22]));
                        pts.Codec.SetAskSize(tick, 2, int.Parse(arr[23]));
                        pts.Codec.SetAskSize(tick, 3, int.Parse(arr[24]));
                        pts.Codec.SetAskSize(tick, 4, int.Parse(arr[25]));
                        pts.Codec.SetAskSize(tick, 5, int.Parse(arr[26]));

                        pts.Codec.SetBidPrice(tick, 1, double.Parse(arr[27]));
                        pts.Codec.SetBidPrice(tick, 2, double.Parse(arr[28]));
                        pts.Codec.SetBidPrice(tick, 3, double.Parse(arr[29]));
                        pts.Codec.SetBidPrice(tick, 4, double.Parse(arr[30]));
                        pts.Codec.SetBidPrice(tick, 5, double.Parse(arr[31]));

                        pts.Codec.SetBidSize(tick, 1, int.Parse(arr[32]));
                        pts.Codec.SetBidSize(tick, 2, int.Parse(arr[33]));
                        pts.Codec.SetBidSize(tick, 3, int.Parse(arr[34]));
                        pts.Codec.SetBidSize(tick, 4, int.Parse(arr[35]));
                        pts.Codec.SetBidSize(tick, 5, int.Parse(arr[36]));

                        pts.Write(tick, new Stream[] { stream });
                    } while (str != null);
                    file.Close();
                }
            }
        }
예제 #12
0
        public void TestReadCsvLeve1()
        {
            FileInfo fi = new FileInfo(@"F:\BaiduYunDownload\IF1406\IF1406.csv");
            FileInfo fo = new FileInfo(@"F:\BaiduYunDownload\IF1406\IF1406.pd0");

            PbTickSerializer pts = new PbTickSerializer();

            pts.Codec.Config.SetTickSize(0.2);
            pts.Codec.Config.Time_ssf_Diff = 5;

            using (Stream stream = File.Open(@"F:\BaiduYunDownload\IF1406\IF1406.pd0", FileMode.Create))
            {
                using (StreamReader file = new StreamReader(fi.OpenRead()))
                {
                    int    i   = 0;
                    string str = file.ReadLine();
                    do
                    {
                        ++i;
                        str = file.ReadLine();
                        if (str == null)
                        {
                            break;
                        }

                        string[] arr = str.Split(',');

                        DateTime dt      = DateTime.Parse(arr[0]).AddMilliseconds(int.Parse(arr[1]));
                        string   symbol  = arr[2];
                        double   price   = double.Parse(arr[3]);
                        int      vol     = int.Parse(arr[4]);
                        int      openint = int.Parse(arr[5]);
                        double   bid     = double.Parse(arr[6]);
                        int      bidSize = int.Parse(arr[7]);
                        double   ask     = double.Parse(arr[8]);
                        int      askSize = int.Parse(arr[9]);

                        PbTick tick = new PbTick();

                        tick.Config = pts.Codec.Config;

                        pts.Codec.SetLastPrice(tick, price);
                        pts.Codec.SetVolume(tick, vol);
                        pts.Codec.SetSymbol(tick, symbol);

                        pts.Codec.SetActionDay(tick, dt.Date);
                        pts.Codec.SetUpdateTime(tick, dt - dt.Date);

                        pts.Codec.SetBidPrice(tick, 1, bid);
                        pts.Codec.SetBidSize(tick, 1, bidSize);
                        pts.Codec.SetAskPrice(tick, 1, ask);
                        pts.Codec.SetAskSize(tick, 1, askSize);

                        pts.Write(tick, new Stream[] { stream });

                        //if (i == 1000000)
                        //    break;
                    } while (str != null);
                    file.Close();
                }
            }
            Console.WriteLine("结束了");
        }
예제 #13
0
        public void TestTickDiff()
        {
            PbTickCodec codec = new PbTickCodec();

            codec.Config.SetTickSize(0.2);
            codec.Config.Time_ssf_Diff = 5;
            codec.UseFlat(false);

            PbTick tick1 = new PbTick();

            tick1.Config = codec.Config;

            codec.SetLastPrice(tick1, 1234);
            codec.SetVolume(tick1, 1);
            codec.SetActionDay(tick1, DateTime.Today);
            codec.SetTradingDay(tick1, DateTime.Today.AddDays(-1));
            //codec.Set
            codec.SetAskPrice(tick1, 1, 1234.2);
            codec.SetAskSize(tick1, 1, 1);
            //codec.SetAskPrice(tick1, 2, 1234.4);
            //codec.SetAskSize(tick1, 2, 1);
            //codec.SetAskPrice(tick1, 3, 1234.6);
            //codec.SetAskSize(tick1, 3, 3);
            //codec.SetAskPrice(tick1, 4, 1234.8);
            //codec.SetAskSize(tick1, 4, 4);
            //codec.SetAskPrice(tick1, 5, 1235.0);
            //codec.SetAskSize(tick1, 5, 5);
            //codec.SetAskPrice(tick1, 6, 1235.2);
            //codec.SetAskSize(tick1, 6, 6);

            codec.SetBidPrice(tick1, 1, 1234);
            codec.SetBidSize(tick1, 1, 1);
            //codec.SetBidPrice(tick1, 2, 1233.8);
            //codec.SetBidSize(tick1, 2, 2);
            //codec.SetBidPrice(tick1, 3, 1233.6);
            //codec.SetBidSize(tick1, 3, 3);
            //codec.SetBidPrice(tick1, 4, 1233.4);
            //codec.SetBidSize(tick1, 4, 4);
            //codec.SetBidPrice(tick1, 5, 1233.2);
            //codec.SetBidSize(tick1, 5, 5);
            //codec.SetBidPrice(tick1, 6, 1232.0);
            //codec.SetBidSize(tick1, 6, 6);

            codec.SetLowerLimitPrice(tick1, 123.4);
            codec.SetUpperLimitPrice(tick1, 567.8);
            codec.SetSettlementPrice(tick1, 123.4);

            codec.SetOpen(tick1, 10);
            codec.SetHigh(tick1, 10);
            codec.SetLow(tick1, 10);
            codec.SetClose(tick1, 10);
            codec.SetBarSize(tick1, 10);

            tick1.Time_HHmm      = 1234;
            tick1.Time_____ssf__ = 567;
            tick1.Time________ff = 0;

            PbTick tick2 = new PbTick();

            codec.SetLastPrice(tick2, 1234.2);
            codec.SetVolume(tick2, 2);

            codec.SetActionDay(tick2, DateTime.Today);
            codec.SetTradingDay(tick2, DateTime.Today.AddDays(-1));
            codec.SetAskPrice(tick2, 1, 1234.2);
            codec.SetAskSize(tick2, 1, 1);
            codec.SetAskPrice(tick2, 2, 1234.4);
            codec.SetAskSize(tick2, 2, 2);
            codec.SetAskPrice(tick2, 3, 1234.6);
            codec.SetAskSize(tick2, 3, 3);

            codec.SetBidPrice(tick2, 1, 1234);
            codec.SetBidSize(tick2, 1, 1);
            codec.SetBidPrice(tick2, 2, 1233.8);
            codec.SetBidSize(tick2, 2, 2);
            codec.SetBidPrice(tick2, 3, 1233.6);
            codec.SetBidSize(tick2, 3, 3);

            codec.SetLowerLimitPrice(tick2, 123.4);
            codec.SetUpperLimitPrice(tick2, 567.8);
            codec.SetSettlementPrice(tick2, 123.4);

            codec.SetOpen(tick2, 10);
            codec.SetHigh(tick2, 10);
            codec.SetLow(tick2, 10);
            codec.SetClose(tick2, 10);
            codec.SetBarSize(tick2, 10);

            tick2.Time_HHmm      = 1234;
            tick2.Time_____ssf__ = 572;
            tick2.Time________ff = 0;

            var diff = codec.Diff(tick1, tick2);

            Assert.AreEqual <int>(0, diff.ActionDay);
            Assert.AreEqual <int>(0, diff.TradingDay);
            Assert.AreEqual(null, diff.Static);
            Assert.AreEqual(null, diff.Bar);
            Assert.AreEqual <int>(0, diff.Depth1_3.AskPrice1, "AskPrice1");
            Assert.AreEqual <int>(0, diff.Depth1_3.AskSize1, "AskSize1");
            Assert.AreEqual <int>(0, diff.Depth1_3.AskPrice2, "AskPrice2");
            Assert.AreEqual <int>(2, diff.Depth1_3.AskSize2, "AskSize2");
            Assert.AreEqual <int>(0, diff.Depth1_3.AskPrice3, "AskPrice3");
            Assert.AreEqual <int>(3, diff.Depth1_3.AskSize3, "AskSize3");

            Assert.AreEqual <int>(0, diff.Time_____ssf__);


            var tick3 = codec.Restore(tick1, diff);

            Assert.AreEqual <int>(tick2.Depth1_3.AskPrice1, tick3.Depth1_3.AskPrice1);
            Assert.AreEqual <int>(tick2.Depth1_3.AskPrice2, tick3.Depth1_3.AskPrice2);
            Assert.AreEqual <int>(tick2.Depth1_3.AskPrice3, tick3.Depth1_3.AskPrice3);
            Assert.AreEqual <int>(tick2.Depth1_3.AskSize1, tick3.Depth1_3.AskSize1);
            Assert.AreEqual <int>(tick2.Depth1_3.AskSize2, tick3.Depth1_3.AskSize2);
            Assert.AreEqual <int>(tick2.Depth1_3.AskSize3, tick3.Depth1_3.AskSize3);

            Assert.AreEqual <int>(tick2.Depth1_3.BidPrice1, tick3.Depth1_3.BidPrice1);
            Assert.AreEqual <int>(tick2.Depth1_3.BidPrice2, tick3.Depth1_3.BidPrice2);
            Assert.AreEqual <int>(tick2.Depth1_3.BidPrice3, tick3.Depth1_3.BidPrice3);
            Assert.AreEqual <int>(tick2.Depth1_3.BidSize1, tick3.Depth1_3.BidSize1);
            Assert.AreEqual <int>(tick2.Depth1_3.BidSize2, tick3.Depth1_3.BidSize2);
            Assert.AreEqual <int>(tick2.Depth1_3.BidSize3, tick3.Depth1_3.BidSize3);

            Assert.AreEqual <double>(codec.GetLowerLimitPrice(tick1), codec.GetLowerLimitPrice(tick3));
            Assert.AreEqual <double>(codec.GetUpperLimitPrice(tick1), codec.GetUpperLimitPrice(tick3));
            Assert.AreEqual <double>(codec.GetSettlementPrice(tick1), codec.GetSettlementPrice(tick3));

            Assert.AreEqual <double>(codec.GetOpen(tick1), codec.GetOpen(tick3));
            Assert.AreEqual <double>(codec.GetHigh(tick1), codec.GetHigh(tick3));
            Assert.AreEqual <double>(codec.GetLow(tick1), codec.GetLow(tick3));
            Assert.AreEqual <double>(codec.GetClose(tick1), codec.GetClose(tick3));

            Assert.AreEqual <int>(572, tick3.Time_____ssf__);
        }
예제 #14
0
        static void Main2(string[] args)
        {
            //读五档行情,然后存盘
            FileInfo fi = new FileInfo(@"d:\wukan\Desktop\20141225.csv");
            //FileInfo fo = new FileInfo(@"d:\wukan\Desktop\20141225.pd0");

            PbTickSerializer pts = new PbTickSerializer();


            using (Stream stream = File.Open(@"d:\wukan\Desktop\20141225_1.pd0", FileMode.Create))
            {
                using (StreamReader file = new StreamReader(fi.OpenRead()))
                {
                    int    i   = 0;
                    string str = file.ReadLine();
                    do
                    {
                        ++i;
                        str = file.ReadLine();
                        if (str == null)
                        {
                            break;
                        }

                        string[] arr = str.Split(',');

                        PbTick tick = new PbTick();

                        pts.Codec.SetSymbol(tick, arr[0]);

                        tick.Config = new ConfigInfo().Default();

                        if (arr[0].StartsWith("TF"))
                        {
                            tick.Config.SetTickSize(0.002);
                            tick.Config.ContractMultiplier = 10000;
                        }
                        else
                        {
                            tick.Config.SetTickSize(0.2);
                            tick.Config.ContractMultiplier = 300;
                        }
                        tick.Config.Time_ssf_Diff = 5;

                        pts.Codec.Config = tick.Config;


                        tick.ActionDay = int.Parse(arr[5]);
                        int time = int.Parse(arr[6]);

                        tick.Time_HHmm      = time / 100000;
                        tick.Time_____ssf__ = time % 100000 / 100;
                        tick.Time________ff = time % 100;

                        pts.Codec.SetLastPrice(tick, double.Parse(arr[8]));
                        pts.Codec.SetHigh(tick, double.Parse(arr[9]));
                        pts.Codec.SetLow(tick, double.Parse(arr[10]));
                        pts.Codec.SetVolume(tick, int.Parse(arr[11]));
                        pts.Codec.SetTurnover(tick, int.Parse(arr[12]));

                        pts.Codec.SetOpenInterest(tick, int.Parse(arr[16]));


                        tick.DepthList = new List <DepthItem>();

                        tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[31])), int.Parse(arr[36]), 0));
                        tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[30])), int.Parse(arr[35]), 0));
                        tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[29])), int.Parse(arr[34]), 0));
                        tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[28])), int.Parse(arr[33]), 0));
                        tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[27])), int.Parse(arr[32]), 0));

                        pts.Codec.SetAskPrice1(tick, double.Parse(arr[17]));

                        tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[17])), int.Parse(arr[22]), 0));
                        tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[18])), int.Parse(arr[23]), 0));
                        tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[19])), int.Parse(arr[24]), 0));
                        tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[20])), int.Parse(arr[25]), 0));
                        tick.DepthList.Add(new DepthItem(pts.Codec.PriceToTick(double.Parse(arr[21])), int.Parse(arr[26]), 0));


                        pts.Write(tick, stream);
                    } while (str != null);
                    file.Close();
                }
            }
        }
예제 #15
0
        // 目前先不处理港股的tickSize变化的那种行情
        PbTick CreateTick(ref DepthMarketDataField 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);

            do
            {
                if (pDepthMarketData.BidVolume1 == 0)
                {
                    break;
                }
                tick.DepthList.Insert(0, new DepthItem(codec.PriceToTick(pDepthMarketData.BidPrice1), pDepthMarketData.BidVolume1, 0));

                // 先记录一个假的,防止只有买价没有卖价的情况
                codec.SetAskPrice1(tick, pDepthMarketData.BidPrice1);
                tick.AskPrice1 += 1;

                if (pDepthMarketData.BidVolume2 == 0)
                {
                    break;
                }
                tick.DepthList.Insert(0, new DepthItem(codec.PriceToTick(pDepthMarketData.BidPrice2), pDepthMarketData.BidVolume2, 0));

                if (pDepthMarketData.BidVolume3 == 0)
                {
                    break;
                }
                tick.DepthList.Insert(0, new DepthItem(codec.PriceToTick(pDepthMarketData.BidPrice3), pDepthMarketData.BidVolume3, 0));

                if (pDepthMarketData.BidVolume4 == 0)
                {
                    break;
                }
                tick.DepthList.Insert(0, new DepthItem(codec.PriceToTick(pDepthMarketData.BidPrice4), pDepthMarketData.BidVolume4, 0));

                if (pDepthMarketData.BidVolume5 == 0)
                {
                    break;
                }
                tick.DepthList.Insert(0, new DepthItem(codec.PriceToTick(pDepthMarketData.BidPrice5), pDepthMarketData.BidVolume5, 0));
            } while (false);

            do
            {
                if (pDepthMarketData.AskVolume1 == 0)
                {
                    break;
                }
                tick.DepthList.Add(new DepthItem(codec.PriceToTick(pDepthMarketData.AskPrice1), pDepthMarketData.AskVolume1, 0));
                // 记录卖一价
                codec.SetAskPrice1(tick, pDepthMarketData.AskPrice1);

                if (pDepthMarketData.AskVolume2 == 0)
                {
                    break;
                }
                tick.DepthList.Add(new DepthItem(codec.PriceToTick(pDepthMarketData.AskPrice2), pDepthMarketData.AskVolume2, 0));

                if (pDepthMarketData.AskVolume3 == 0)
                {
                    break;
                }
                tick.DepthList.Add(new DepthItem(codec.PriceToTick(pDepthMarketData.AskPrice3), pDepthMarketData.AskVolume3, 0));

                if (pDepthMarketData.AskVolume4 == 0)
                {
                    break;
                }
                tick.DepthList.Add(new DepthItem(codec.PriceToTick(pDepthMarketData.AskPrice4), pDepthMarketData.AskVolume4, 0));

                if (pDepthMarketData.AskVolume5 == 0)
                {
                    break;
                }
                tick.DepthList.Add(new DepthItem(codec.PriceToTick(pDepthMarketData.AskPrice5), pDepthMarketData.AskVolume5, 0));
            } while (false);

            return(tick);
        }