Example #1
0
        public static void Write(IEnumerable <PbTick> list, Stream stream)
        {
            if (list == null)
            {
                return;
            }

            foreach (var item in list)
            {
                PbTickSerializer.WriteOne(item, stream);
            }
        }
        //[TestMethod]
        public void TestReadCsvLeve3()
        {
            FileInfo fi = new FileInfo(@"d:\wukan\Desktop\DepthDataShow\20150120.txt");
            //FileInfo fo = new FileInfo(@"F:\BaiduYunDownload\DepthDataShow\20150120.pd0");

            PbTickSerializer pts = new PbTickSerializer();

            CCC ccc = new CCC();
            CCC last_ccc = new CCC();

            AAA last = new AAA();
            last.buy = false;

            string last_symbol = "XXX";

            List<AAA> list = new List<AAA>();

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

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

                        AAA a = new AAA();
                        a.symbol = arr[1];
                        a.buy = arr[2] == "0";
                        a.price = double.Parse(arr[3]);
                        a.size = int.Parse(arr[4]);


                        if (last.buy == false && a.buy == true)
                        {
                            // 快照的切换点,把上次的存储都取出来,进行保存
                            // sell里先存的数字大的,后存的数字小的,最后的是卖一
                            // buy里也是先存数字大的,后存数字小的,最前的买一
                            // 
                            foreach (var kv in ccc.dict)
                            {
                                kv.Value.MakeTick();

                                ccc.GetSerializer(kv.Key).Write(kv.Value.tick,
                                    new Stream[] { ccc.GetStream(kv.Key) });
                            }

                            ccc.dict.Clear();
                        }

                        BBB bbb = ccc.Get(a.symbol);
                        bbb.symbol = a.symbol;
                        bbb.time = arr[0];

                        if (bbb.symbol == "cu1502")
                        {
                            int nTest = 1;
                        }

                        if (a.buy)
                        {
                            bbb.AddBid(a);
                        }
                        else
                        {
                            bbb.AddAsk(a);
                        }


                        last = a;
                        last_symbol = a.symbol;

                        //if (i < 4000)
                        //    Console.WriteLine(str);
                        //if (i > 4000)
                        //    break;
                    } while (str != null);
                    file.Close();
                }
            }
        }
        //[TestMethod]
        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;

                        //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();
                }
            }
        }
 public PbTickSerializer GetSerializer(string symbol)
 {
     PbTickSerializer list;
     if (!dict2.TryGetValue(symbol, out list))
     {
         list = new PbTickSerializer();
         dict2.Add(symbol, list);
     }
     return list;
 }
        //[TestMethod]
        //public void TestDataFile()
        //{
        //    //var stream = new MemoryStream(File.ReadAllBytes("test.data"));
        //    //var serializer = new PbTickSerializer();
        //    //PbTick raw;
        //    //var tick = serializer.Read(stream, out raw);

        //    //Assert.AreNotEqual(null, tick);
        //}

        //[TestMethod]
        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.TickSize = pts.Codec.Config.GetTickSize();
            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("结束了");
        }