Пример #1
0
        public StoreData(List <optionDataFormat> myData, int start = 0, int end = 0)
        {
            if (start == 0)
            {
                start = 0;
                end   = myData.Count() - 1;
            }
            if (start != 0 && start < myData.Count() && end == 0)
            {
                end = myData.Count() - 1;
            }
            DataTable dt = new DataTable();

            dt.Columns.Add("代码", Type.GetType("System.String"));
            dt.Columns.Add("日期", Type.GetType("System.String"));
            dt.Columns.Add("时间", Type.GetType("System.Double"));
            dt.Columns.Add("成交价", Type.GetType("System.Double"));
            dt.Columns.Add("Ask1", Type.GetType("System.Double"));
            dt.Columns.Add("Ask2", Type.GetType("System.Double"));
            dt.Columns.Add("Ask3", Type.GetType("System.Double"));
            dt.Columns.Add("Ask4", Type.GetType("System.Double"));
            dt.Columns.Add("Ask5", Type.GetType("System.Double"));
            dt.Columns.Add("Askv1", Type.GetType("System.Double"));
            dt.Columns.Add("Askv2", Type.GetType("System.Double"));
            dt.Columns.Add("Askv3", Type.GetType("System.Double"));
            dt.Columns.Add("Askv4", Type.GetType("System.Double"));
            dt.Columns.Add("Askv5", Type.GetType("System.Double"));
            dt.Columns.Add("bid1", Type.GetType("System.Double"));
            dt.Columns.Add("bid2", Type.GetType("System.Double"));
            dt.Columns.Add("bid3", Type.GetType("System.Double"));
            dt.Columns.Add("bid4", Type.GetType("System.Double"));
            dt.Columns.Add("bid5", Type.GetType("System.Double"));
            dt.Columns.Add("bidv1", Type.GetType("System.Double"));
            dt.Columns.Add("bidv2", Type.GetType("System.Double"));
            dt.Columns.Add("bidv3", Type.GetType("System.Double"));
            dt.Columns.Add("bidv4", Type.GetType("System.Double"));
            dt.Columns.Add("bidv5", Type.GetType("System.Double"));
            dt.Columns.Add("high", Type.GetType("System.Double"));
            dt.Columns.Add("low", Type.GetType("System.Double"));
            dt.Columns.Add("成交量", Type.GetType("System.Double"));
            dt.Columns.Add("成交额", Type.GetType("System.Double"));
            dt.Columns.Add("持仓量", Type.GetType("System.Double"));
            dt.Columns.Add("成交笔数", Type.GetType("System.Double"));
            dt.Columns.Add("状态", Type.GetType("System.String"));
            string str = DateTime.Now.ToString("yyyyMMdd");

            lock (myData)
            {
                for (int i = start; i <= end; i++)
                {
                    optionDataFormat data = myData[i];
                    dt.Rows.Add(new object[] { data.code, str, data.time, data.last, data.ask[0], data.ask[1], data.ask[2], data.ask[3], data.ask[4], data.askv[0], data.askv[1], data.askv[2], data.askv[3], data.askv[4], data.bid[0], data.bid[1], data.bid[2], data.bid[3], data.bid[4], data.bidv[0], data.bidv[1], data.bidv[2], data.bidv[3], data.bidv[4], data.high, data.low, data.volume, data.turnover, data.openInterest, data.count, data.status });
                }
            }
            CsvApplication.SaveCSV(dt, "optiondata_" + str + ".csv", "append");
        }
Пример #2
0
        static void Main(string[] args)
        {
            //Thread.Sleep(15000);
            List <optionDataFormat> myData = new List <optionDataFormat>();
            Dictionary <string, List <optionDataFormat> > optionData = new Dictionary <string, List <optionDataFormat> >();
            int total = 0;
            int today = Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd"));
            OptionInformation myOption = new OptionInformation(today);

            int[]  optionList   = myOption.GetOptionNameByDate(today);
            string time         = "";
            int    recordStart  = 0;
            int    recordEnd    = 0;
            string optionString = "";

            for (int i = 0; i < optionList.Length; i++)
            {
                optionString += optionList[i].ToString() + ".SH,";
            }
            int thisMonth = Convert.ToInt32(DateTime.Now.ToString("yyMM"));

            for (int i = 0; i <= 9; i++)
            {
                string month = DateTime.Now.AddMonths(i).ToString("yyMM");
                optionString += "IH" + month + ".CF,";
            }
            optionString += "510050.SH";

            bool bRet = SetConsoleCtrlHandler(commandLineDelegate, true);

            var baseAddress = ConfigurationManager.AppSettings["OrientWebGWAddress"];

            var userName     = ConfigurationManager.AppSettings["UserName"];
            var userPassword = ConfigurationManager.AppSettings["UserPassword"];

            GetPublicKey().Wait();

            var accessToken = GetAccessToken(userName, userPassword).Result;

            Console.WriteLine("GetToken:{0}", accessToken);

            if (string.IsNullOrEmpty(accessToken))
            {
                Console.WriteLine("用户名密码错误");
                Console.ReadLine();
                return;
            }


            using (var hubConnection = new HubConnection(baseAddress))
            {
                hubConnection.Headers.Add("Authorization", "Bearer " + accessToken);

                IHubProxy proxy = hubConnection.CreateHubProxy("DataCenterHub");

                proxy.On <dynamic, string>("onPushDynamicData", (data, stkcd) =>
                {
                    //DateTime currentTime = System.DateTime.Now;
                    // Console.WriteLine(currentTime+currentTime.Millisecond.ToString());
                    optionDataFormat data0 = new optionDataFormat();
                    data0.code             = data["c"];
                    data0.ask  = new int[5];
                    data0.askv = new int[5];
                    data0.bid  = new int[5];
                    data0.bidv = new int[5];
                    for (int i = 0; i < 5; i++)
                    {
                        data0.ask[i]  = data["a"][i];
                        data0.askv[i] = data["av"][i];
                        data0.bid[i]  = data["b"][i];
                        data0.bidv[i] = data["bv"][i];
                    }
                    data0.high         = data["h"];
                    data0.low          = data["l"];
                    data0.last         = data["p"];
                    data0.openInterest = data["oi"] == null?0:data["oi"];
                    data0.status       = data["s"];
                    data0.time         = data["t"];
                    data0.turnover     = data["tt"];
                    data0.volume       = data["deal"]["v"];
                    data0.count        = data["deal"]["c"];
                    //if (optionData.ContainsKey(data0.code) == false)
                    //{
                    //    List<optionDataFormat> myData0 = new List<optionDataFormat>();
                    //    myData0.Add(data0);
                    //    optionData.Add(data0.code, myData0);
                    //}
                    //else
                    //{
                    //    optionData[data0.code].Add(data0);
                    //}
                    myData.Add(data0);
                    total += 1;
                });

                proxy.On("onPushStaticData", data =>
                {
                    Console.WriteLine(data["c"] + " " + data["pc"]);
                });

                proxy.On("onPushBasisData", data =>
                {
                    Console.WriteLine(data["c"] + " " + data["cn"]);
                });

                proxy.On("onPushOrderQueueData", data =>
                {
                    Console.WriteLine(data["c"] + " " + data["t"] + " " + data["a"].ToString());
                });


                DateTime firstStamp = DateTime.MinValue;
                DateTime firstNow   = DateTime.MinValue;

                proxy.On("onPushTickData", data =>
                {
                });


                proxy.On("OnNotify", data =>
                {
                    Console.WriteLine(data["c"] + " " + data["cn"]);
                });

                proxy.On("OnRequest", data =>
                {
                    Console.WriteLine(data);
                });

                hubConnection.Start().ContinueWith(c =>
                {
                    if (c.IsFaulted)
                    {
                        //连接失败,暂时无法在客户端区分错误类别,只能识别出500内部错误
                    }
                    else
                    {
                        proxy.Invoke("RequestSubscribe", optionString);

                        //proxy.Invoke("RequestSubscribe", "002341.SZ", "OQ,TI,SO");

                        //proxy.Invoke("RequestGetBasisData", "11000021.SH");

                        //proxy.Invoke("RequestGetStaticData", "11000021.SH");

                        //proxy.Invoke("RequestGetSecurityList", "1100");

                        //proxy.Invoke("RequestGetMinuteData", "11000021.SH");
                    }
                });

                while (true)
                {
                    Thread.Sleep(3 * 1000);
                    time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    Console.WriteLine(total);
                    recordEnd = total - 1;
                    if (recordEnd > recordStart)
                    {
                        StoreData store = new StoreData(myData, recordStart, recordEnd);
                        Console.WriteLine("Time: {2}, record data! form {0} to {1}", recordStart, recordEnd, time);
                    }
                    recordStart = recordEnd + 1;
                }
            }
        }