Beispiel #1
0
        public static void WriteCsv(IEnumerable <PbTick> list, string output)
        {
            if (list == null)
            {
                return;
            }

            var Codec = new PbTickCodec();

            // 将差分数据生成界面数据
            IEnumerable <PbTickView> _list = Codec.Data2View(Codec.Restore(list), false);

            // 保存
            using (TextWriter stream = new StreamWriter(output))
            {
                var t = new PbTickView();
                stream.WriteLine(t.ToCsvHeader());

                foreach (var l in _list)
                {
                    stream.WriteLine(l);
                }
                stream.Close();
            }
        }
        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");
        }
        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));
        }
Beispiel #4
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);
        }
Beispiel #5
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);
        }
Beispiel #6
0
 public PbTickSerializer()
 {
     Codec = new PbTickCodec();
 }
        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);
        }
        public void TestGcdTickSize()
        {
            PbTickCodec codec = new PbTickCodec();

            Assert.AreEqual(0.00001, codec.gcd());
            Assert.AreEqual(0.3, codec.gcd(100.3f - 100.0f));
            Assert.AreEqual(0.2, codec.gcd(100.3f - 100.1f));
            Assert.AreEqual(0.1, codec.gcd(100.3f - 100.0f, 100.2f - 100.0f));
            Assert.AreEqual(0.1, codec.gcd(100.3f - 100.0f, 100.2f - 100.0f, 100.1f - 100.0f));
            Assert.AreEqual(0.05, codec.gcd(100.3f - 100.0f, 100.2f - 100.0f, 100.1f - 100.0f, 100.1f - 100.05f));

            Assert.AreEqual(0.1, codec.gcd(100.0f - 100.3f, 100.0f - 100.2f));
            Assert.AreEqual(0.05, codec.gcd(100.0f - 100.3f, 100.0f - 100.2f, 100.0f - 100.1f, 100.05f - 100.1f));
            Assert.AreEqual(10, codec.gcd(1000, 41430));
        }
        public void TestTickMultiplier()
        {
            PbTickCodec codec = new PbTickCodec();

            codec.Config.SetTickSize(0.2);
            codec.TickSize = codec.Config.GetTickSize();
            codec.Config.ContractMultiplier = 50000;
            codec.Config.Time_ssf_Diff = 5;

            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(123.45, codec.GetAveragePrice(tick1));
            Assert.AreEqual(123.45, codec.GetSettlementPrice(tick1));
            Assert.AreEqual(1234567890120000, codec.GetTurnover(tick1));
            Assert.AreEqual(9123456789012345678, codec.GetOpenInterest(tick1));
            Assert.AreEqual(1234567890, codec.GetVolume(tick1));

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

            codec.Config.ContractMultiplier = 0.1;
            codec.SetTurnover(tick1, 12345678901234.56);
            Assert.AreEqual(12345678901234.56, codec.GetTurnover(tick1));
        }
        public void TestTickDiff()
        {
            PbTickCodec codec = new PbTickCodec();

            codec.Config.SetTickSize(0.2);
            codec.TickSize = codec.Config.GetTickSize();
            codec.Config.Time_ssf_Diff = 5;

            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(0, diff.ActionDay);
            Assert.AreEqual(0, diff.TradingDay);
            Assert.AreEqual(null, diff.Static);
            Assert.AreEqual(null, diff.Bar);
            Assert.AreEqual(0, diff.Depth1_3.AskPrice1, "AskPrice1");
            Assert.AreEqual(0, diff.Depth1_3.AskSize1, "AskSize1");
            Assert.AreEqual(0, diff.Depth1_3.AskPrice2, "AskPrice2");
            Assert.AreEqual(2, diff.Depth1_3.AskSize2, "AskSize2");
            Assert.AreEqual(0, diff.Depth1_3.AskPrice3, "AskPrice3");
            Assert.AreEqual(3, diff.Depth1_3.AskSize3, "AskSize3");

            Assert.AreEqual(0, diff.Time_____ssf__);


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

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

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

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

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

            Assert.AreEqual(572, tick3.Time_____ssf__);
        }
        public void TestTick2Price()
        {
            PbTickCodec ptc = new PbTickCodec();

            ptc.Config.SetTickSize(0.2);
            ptc.TickSize = ptc.Config.GetTickSize();

            Assert.AreEqual(5, ptc.PriceToTick(1));
            Assert.AreEqual(6, ptc.PriceToTick(1.2));
            Assert.AreEqual(7, ptc.PriceToTick(1.4));
            Assert.AreEqual(8, ptc.PriceToTick(1.6));
            Assert.AreEqual(9, ptc.PriceToTick(1.8));
            Assert.AreEqual(10, ptc.PriceToTick(2.0));

            Assert.AreEqual(-5, ptc.PriceToTick(-1));
            Assert.AreEqual(-6, ptc.PriceToTick(-1.2));
            Assert.AreEqual(-7, ptc.PriceToTick(-1.4));
            Assert.AreEqual(-8, ptc.PriceToTick(-1.6));
            Assert.AreEqual(-9, ptc.PriceToTick(-1.8));
            Assert.AreEqual(-10, ptc.PriceToTick(-2.0));


            Assert.AreEqual(0.2, ptc.TickToPrice(1));
            Assert.AreEqual(0.4, ptc.TickToPrice(2));

            Assert.AreEqual(-0.2, ptc.TickToPrice(-1));
            Assert.AreEqual(-0.4, ptc.TickToPrice(-2));
            Assert.AreEqual(-2, ptc.TickToPrice(-10));
        }
        public static void WriteCsv(IEnumerable<PbTick> list, string output)
        {
            if (list == null)
                return;

            PbTickCodec Codec = new PbTickCodec();

            // 将差分数据生成界面数据
            IEnumerable<PbTickView> _list = Codec.Data2View(Codec.Restore(list), false);

            // 保存
            using (TextWriter stream = new StreamWriter(output))
            {
                PbTickView t = new PbTickView();
                stream.WriteLine(t.ToCsvHeader());

                foreach (var l in _list)
                {
                    stream.WriteLine(l);
                }
                stream.Close();
            }
        }
 public PbTickSerializer()
 {
     Codec = new PbTickCodec();
 }
        public PbTick Double2Int(PbTickView tick)
        {
            if (tick == null)
                return null;

            PbTick 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;
        }
        public PbTickView Int2Double(PbTick tick)
        {
            if (tick == null)
                return null;

            PbTickView 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;
        }