Exemple #1
0
        public Dictionary <string, Tick> Current(IEnumerable <string> symbols)
        {
            Dictionary <string, Tick> ret = new Dictionary <string, Tick>();
            string        baseUrl = "http://api.money.126.net/data/feed/";
            int           i = 0, si = 0;
            string        symbolsString = "";
            List <string> tickStrings   = new List <string>();

            try
            {
                foreach (string symbol in symbols)
                {
                    string neteasySymbol = this.getNeteasySymbol(symbol);
                    symbolsString += neteasySymbol + ",";
                    i++; si++;
                    if (i >= this.batchSize || si >= symbols.Count())
                    {
                        Stream       stream = this.webClient.OpenRead(baseUrl + symbolsString);
                        StreamReader reader = new StreamReader(stream);
                        string       tickString;
                        while ((tickString = reader.ReadLine()) != null)
                        {
                            tickStrings.Add(tickString);
                        }
                        stream.Close();
                        symbolsString = "";
                        i             = 0;
                    }
                }
                string pattern = @"^_ntes_quote_callback\((.+)\);$";
                foreach (string tickString in tickStrings)
                {
                    Match mat = Regex.Match(tickString, pattern);
                    if (mat.Groups.Count > 1)
                    {
                        string  json = mat.Groups[1].Value;
                        JObject data = (JObject)JsonConvert.DeserializeObject(json);
                        foreach (string symbol in symbols)
                        {
                            if (!data.ContainsKey(this.dictGMToNeteasy[symbol]))
                            {
                                continue;
                            }
                            JObject record = (JObject)data[this.dictGMToNeteasy[symbol]];
                            Tick    aTick  = new Tick
                            {
                                Price     = Utils.ParseFloat((string)record["price"]),
                                LastClose = Utils.ParseFloat((string)record["yestclose"]),
                                Open      = Utils.ParseFloat((string)record["open"]),
                                High      = Utils.ParseFloat((string)record["high"]),
                                Low       = Utils.ParseFloat((string)record["low"]),
                                // UpperLimit = float.Parse(data[47]),
                                // LowerLimit = float.Parse(data[48]),
                                DateTime = Utils.StringToDateTime(record["time"].ToString(), "NETEASY")
                            };

                            for (int k = 0; k < 5; k++)
                            {
                                aTick.Quotes[k] = new Quote
                                {
                                    BidPrice  = Utils.ParseFloat((string)record[String.Format("bid{0}", k + 1)]),
                                    BidVolume = Utils.ParseLong((string)record[String.Format("bidvol{0}", k + 1)]),
                                    AskPrice  = Utils.ParseFloat((string)record[String.Format("ask{0}", k + 1)]),
                                    AskVolume = Utils.ParseLong((string)record[String.Format("askvol{0}", k + 1)])
                                };
                            }
                            aTick.CumVolume = Utils.ParseDouble((string)record["volume"]);
                            aTick.CumAmount = Utils.ParseDouble((string)record["turnover"]);
                            aTick.Source    = "Neteasy";
                            ret.Add(symbol, aTick);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0}的{1}发生错误:{2}", ex.Source, ex.TargetSite.Name, ex.Message);
            }
            return(ret);
        }
Exemple #2
0
        public Dictionary <string, Tick> Current(IEnumerable <string> symbols)
        {
            Dictionary <string, Tick> ret = new Dictionary <string, Tick>();
            string        url             = "http://nuff.eastmoney.com/EM_Finance2015TradeInterface/JS.ashx?id=";
            List <string> tickStrings     = new List <string>();

            //只能一个个的请求,无法成批发送请求
            try
            {
                foreach (string symbol in symbols)
                {
                    string       eastMoneySymbol = this.getEastMoneySymbol(symbol);
                    Stream       stream          = this.webClient.OpenRead(url + eastMoneySymbol);
                    StreamReader reader          = new StreamReader(stream);
                    string       tickString;
                    if ((tickString = reader.ReadLine()) != null)
                    {
                        tickStrings.Add(tickString);
                    }
                    stream.Close();
                }
                int i = 0;
                foreach (string symbol in symbols)
                {
                    if (i >= tickStrings.Count)
                    {
                        break;
                    }
                    string pattern = @"^callback\((.+)\)$";
                    Match  mat     = Regex.Match(tickStrings[i], pattern);
                    if (mat.Groups.Count > 1)
                    {
                        string json = mat.Groups[1].Value;
                        json.Replace("-", "");
                        JObject obj  = (JObject)JsonConvert.DeserializeObject(json);
                        JArray  data = (JArray)obj["Value"];
                        if (data.Count >= 30) //保证有数据
                        {
                            if ((string)data[1] != symbol.Substring(5))
                            {
                                continue;
                            }
                            Tick aTick = new Tick
                            {
                                UpperLimit = Utils.ParseFloat((string)data[23]),
                                LowerLimit = Utils.ParseFloat((string)data[24]),
                                Price      = Utils.ParseFloat((string)data[25]),
                                Open       = Utils.ParseFloat((string)data[28]),
                                High       = Utils.ParseFloat((string)data[30]),
                                CumVolume  = Utils.ParseDouble((string)data[31]) * 100,
                                Low        = Utils.ParseFloat((string)data[32]),
                                Volume     = Utils.ParseInt((string)data[33]) * 100,
                                LastClose  = Utils.ParseFloat((string)data[34]),
                                CumAmount  = Utils.ParseDouble(((string)data[35]).Replace('万', ' ')) * 10000,
                            };
                            for (int k = 0; k < 5; k++)
                            {
                                aTick.Quotes[k] = new Quote
                                {
                                    BidPrice  = Utils.ParseFloat((string)data[3 + k]),
                                    BidVolume = Utils.ParseLong((string)data[13 + k]) * 100,
                                    AskPrice  = Utils.ParseFloat((string)data[8 + k]),
                                    AskVolume = Utils.ParseLong((string)data[18 + k]) * 100
                                };
                            }
                            aTick.DateTime = Utils.StringToDateTime((string)data[49], "EASTMONEY");
                            this.dictLastTradeDate[symbol] = aTick.DateTime.Date;//当前交易日期
                            aTick.Source = "EastMoney";
                            ret.Add(symbol, aTick);
                        }
                    }
                    i++;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0}的{1}发生错误:{2}", ex.Source, ex.TargetSite.Name, ex.Message);
            }

            return(ret);
        }