private string url = "http://hq.sinajs.cn/list=";//api地址 public override NET_ERROR StockGetWithCheck(ref List<string> id, out Dictionary<string, StockInfoEntity> dict) { dict = new Dictionary<string, StockInfoEntity>(); string stock = ""; foreach (string s in id) { if (s[0] == '0') stock += "sh" + s.Substring(1) + ','; else stock += "sz" + s.Substring(1) + ','; } WebClient client = new WebClient(); client.Headers.Add("user-agent", "Mozilla/4.0"); Stream data; try { data = client.OpenRead(url + stock);//构建批量获取地址 } catch (WebException) { UpdateLog(stock, NET_ERROR.NET_CANT_CONNECT); return NET_ERROR.NET_CANT_CONNECT;//网络错误 无法连接 } StreamReader reader = new StreamReader(data);//读取所有结果 string str; StockInfoEntity SIE = new StockInfoEntity(); while ((str = reader.ReadLine()) != null) { string backid = str.Substring(12, 7); if (backid[0] == 'h') backid = backid.Replace('h', '0'); else if (backid[0] == 'z') backid = backid.Replace('z', '1'); else continue; str = str.Substring(str.IndexOf("\"") + 1); string[] sArray = str.Split(','); if(sArray.Length < 33) continue; SIE.name = sArray[0]; SIE.open = sArray[1]; SIE.yestclose = sArray[2]; SIE.price = sArray[3]; SIE.high = sArray[4]; SIE.low = sArray[5]; SIE.volume = sArray[8]; SIE.turnover = sArray[9]; double dvalue = Convert.ToDouble(sArray[3]) - Convert.ToDouble(sArray[2]); SIE.updown = dvalue.ToString(); double per = dvalue / Convert.ToDouble(sArray[2]); SIE.percent = per > 0 ? per.ToString() : (-per).ToString(); SIE.arrow = per > 0 ? "↑" : "↓"; SIE.time = sArray[30] + " " + sArray[31]; dict.Add(backid, SIE); } return NET_ERROR.NET_REQ_OK; }
public void UpdataSync(StockInfoEntity SIE) { Action <TextBox, String> updateAction = new Action <TextBox, string>(pricesync); price.Dispatcher.BeginInvoke(updateAction, price, SIE.price); Action <String> up = new Action <string>(updownsync); this.Dispatcher.BeginInvoke(up, SIE.updown); }
private void Proc_RTM(SGCommand command) { var code = (command.Parameters.ContainsKey("code")) ? command.Parameters["code"].ToString() : null; if (StockInfoEntity.IsValidCode(code)) { var plug = this.FindPlug <PlugIn>(code); if (plug != null && plug.DataCount <= 0) { this.PutData <SGCommand>(code, command); } } }
private void UpdataSync(StockInfoEntity SIE) { Action <TextBlock, String> updateAction = new Action <TextBlock, string>(TextBoxSync); arrow.Dispatcher.BeginInvoke(updateAction, arrow, SIE.arrow); high.Dispatcher.BeginInvoke(updateAction, high, SIE.high); low.Dispatcher.BeginInvoke(updateAction, low, SIE.low); open.Dispatcher.BeginInvoke(updateAction, open, SIE.open); percent.Dispatcher.BeginInvoke(updateAction, percent, Adapter.DataAdapter.RealTwo(Convert.ToDouble(SIE.percent) * 100) + "%"); price.Dispatcher.BeginInvoke(updateAction, price, SIE.price); time.Dispatcher.BeginInvoke(updateAction, time, SIE.time); turnover.Dispatcher.BeginInvoke(updateAction, turnover, Adapter.DataAdapter.RealTwo(Convert.ToDouble(SIE.turnover) / 100) + "手"); updown.Dispatcher.BeginInvoke(updateAction, updown, Adapter.DataAdapter.RealTwo(Convert.ToDouble(SIE.updown))); volume.Dispatcher.BeginInvoke(updateAction, volume, Adapter.DataAdapter.RealTwo(Convert.ToDouble(SIE.volume) / 10000) + "万"); yestclose.Dispatcher.BeginInvoke(updateAction, yestclose, SIE.yestclose); }
public static bool IsValidCode(string code, CODETYPE type) { var valid = false; switch (type) { case CODETYPE.index: valid = IndexInfoEntity.TellMarket(code) != MARKET.none; break; case CODETYPE.stock: valid = StockInfoEntity.TellMarket(code) != MARKET.none; break; } return(valid); }
public T FetchData <T>(string code) where T : BasicInfoEntity { var market = StockInfoEntity.TellMarket(code); if (market == MARKET.none) { return(null); } var ncode = market.ToString() + code; var parameters = new Dictionary <string, string>(); parameters.Add("list", ncode); var script = WebRequestor.Send(url, parameters, WebRequestor.RequestMethod.get, Encoding.GetEncoding("GB2312")); StockInfoEntity info = null; TryParse(script, out info); object result = info; return((T)result); }
private bool ClearMinute(string code) { if (StockInfoEntity.TellMarket(code) == MARKET.none) { return(false); } if (CheckTable <DBTStkMinuteEntity>() && Config.Instance.INFO.ScriptSetting.Scripts.ContainsKey("DEL_MINUTE_BY_CD")) { var scripts = Config.Instance.INFO.ScriptSetting.Scripts; var param = accessor.CreateParameter("CODE", code); var cmd = accessor.CreateCommand(scripts["DEL_MINUTE_BY_CD"], new List <DbParameter>() { param }); if (accessor.ExecuteSQLCommand(cmd) < 0) { logger.Write(TYPE.ERROR, string.Format("delete minute data failed.({0})", code)); logger.Write(TYPE.ERROR, accessor.LastError); return(false); } } return(true); }
protected override RESULT Process(StringBuilder messager) { var working = Config.Instance.INFO.BizTime.IsWorkingTime; //working = 0; if (working > 0) { // Retrieve Code Data From DB var stocks = new List <string>(); if (CheckTable <DBTStkFavoriteEntity>()) { accessor.RetrieveEntity <DBTStkFavoriteEntity>().ForEach(data => stocks.Add(StockInfoEntity.FormatedCode(data.Code))); } if (stocks.Count > 0) { service.FetchData <TengxunStockInfoEntity>(stocks).ForEach(info => { if (info.IsTodayData && info.IsValid) { this.PutData <TengxunStockInfoEntity>(info.Code, info); } }); } } else { Config.Instance.INFO.BizTime.Clear(); tables.Clear(); } return(RESULT.OK); }
private string url = "http://hq.sinajs.cn/list=";//api地址 public override NET_ERROR StockGetWithCheck(ref List <string> id, out Dictionary <string, StockInfoEntity> dict) { dict = new Dictionary <string, StockInfoEntity>(); string stock = ""; foreach (string s in id) { if (s[0] == '0') { stock += "sh" + s.Substring(1) + ','; } else { stock += "sz" + s.Substring(1) + ','; } } WebClient client = new WebClient(); client.Headers.Add("user-agent", "Mozilla/4.0"); Stream data; try { data = client.OpenRead(url + stock);//构建批量获取地址 } catch (WebException) { UpdateLog(stock, NET_ERROR.NET_CANT_CONNECT); return(NET_ERROR.NET_CANT_CONNECT); //网络错误 无法连接 } StreamReader reader = new StreamReader(data); //读取所有结果 string str; StockInfoEntity SIE = new StockInfoEntity(); while ((str = reader.ReadLine()) != null) { string backid = str.Substring(12, 7); if (backid[0] == 'h') { backid = backid.Replace('h', '0'); } else if (backid[0] == 'z') { backid = backid.Replace('z', '1'); } else { continue; } str = str.Substring(str.IndexOf("\"") + 1); string[] sArray = str.Split(','); if (sArray.Length < 33) { continue; } SIE.name = sArray[0]; SIE.open = sArray[1]; SIE.yestclose = sArray[2]; SIE.price = sArray[3]; SIE.high = sArray[4]; SIE.low = sArray[5]; SIE.volume = sArray[8]; SIE.turnover = sArray[9]; double dvalue = Convert.ToDouble(sArray[3]) - Convert.ToDouble(sArray[2]); SIE.updown = dvalue.ToString(); double per = dvalue / Convert.ToDouble(sArray[2]); SIE.percent = per > 0 ? per.ToString() : (-per).ToString(); SIE.arrow = per > 0 ? "↑" : "↓"; SIE.time = sArray[30] + " " + sArray[31]; dict.Add(backid, SIE); } return(NET_ERROR.NET_REQ_OK); }
protected override RESULT Process(StringBuilder messager) { var working = Config.Instance.INFO.TimeSetting.IsWorkingTime; if (working > 0) { // Retrieve Code Data From DB var stocks = new List <string>(); if (CheckTable <DBTStkFavoriteEntity>()) { accessor.RetrieveEntity <DBTStkFavoriteEntity>().ForEach(data => stocks.Add(StockInfoEntity.FormatedCode(data.Code))); } if (stocks.Count > 0) { var list = service.FetchData <TengxunStockInfoEntity>(stocks); foreach (TengxunStockInfoEntity info in list) { ProcessStockInfo(info); } } } else { RecordDailyInfo(); LastDayEntities.Clear(); Config.Instance.INFO.TimeSetting.Clear(); tables.Clear(); filter4idx.Clear(); filter4stk.Clear(); lines.Values.ToList().ForEach(line => line.TriggerSwitch()); lines.Values.ToList().ForEach(line => line.Clear()); } return(RESULT.OK); }
private bool TryParse(string script, out StockInfoEntity entity) { entity = null; if (string.IsNullOrWhiteSpace(script)) { return(false); } var match = Regex.Match(script, "^var [a-zA-Z0-9/_]+[=]{1}\"(?<data>.*)\";$", RegexOptions.IgnoreCase | RegexOptions.Singleline); var result = match.Success; if (result) { var data = match.Groups["data"].Value; if (string.IsNullOrWhiteSpace(data)) { return(false); } string[] temp = data.Split(','); if (temp.Length < 32) { return(false); } int index = 0; entity = new StockInfoEntity(); entity.Name = temp[index]; entity.TodayOpen = decimal.Parse(temp[++index]); entity.YesterdayClose = decimal.Parse(temp[++index]); entity.Current = decimal.Parse(temp[++index]); entity.High = decimal.Parse(temp[++index]); entity.Low = decimal.Parse(temp[++index]); ++index; ++index; //entity.Buy = decimal.Parse(temp[++index]); //entity.Sell = decimal.Parse(temp[++index]); entity.GVolAmount = int.Parse(temp[++index]); entity.GVolMoney = decimal.Parse(temp[++index]); for (int i = 0; i < 5; i++) { entity.BuyList.Add(new OrderItemEntity() { Amount = Convert.ToDecimal(temp[++index]), Price = Convert.ToDecimal(temp[++index]), Flag = TRADE.buy }); } for (int i = 0; i < 5; i++) { entity.SellList.Add(new OrderItemEntity() { Amount = Convert.ToDecimal(temp[++index]), Price = Convert.ToDecimal(temp[++index]), Flag = TRADE.sale }); } //entity.Time = Convert.ToDateTime(string.Format("{0} {1}", temp[30].Replace('/', '-'), temp[31])); entity.Time = Convert.ToDateTime(string.Format("{0} {1}", temp[++index].Replace('/', '-'), temp[++index])); } return(result); }
private string url = "http://api.money.126.net/data/feed/";//api地址 public override NET_ERROR StockGetWithCheck(ref List <string> id, out Dictionary <string, StockInfoEntity> dict) { dict = new Dictionary <string, StockInfoEntity>(); string stock = ""; foreach (string s in id) { stock += s + ','; } WebClient client = new WebClient(); client.Headers.Add("user-agent", "Mozilla/4.0"); Stream data; try { data = client.OpenRead(url + stock);//构建批量获取地址 } catch (WebException) { UpdateLog(stock, NET_ERROR.NET_CANT_CONNECT); return(NET_ERROR.NET_CANT_CONNECT); //网络错误 无法连接 } StreamReader reader = new StreamReader(data); //读取所有结果 string str = reader.ReadToEnd(); string json; try { string pattern = @"\(.*?\)"; Regex regex = new Regex(pattern, RegexOptions.IgnoreCase); json = regex.Match(str).Value.Trim('(', ')');//正则获取json } catch (Exception) { UpdateLog(stock, NET_ERROR.NET_DATA_ERROR); return(NET_ERROR.NET_DATA_ERROR);//获取json失败 } JObject jo; try { jo = (JObject)JsonConvert.DeserializeObject(json);//解析json } catch (JsonException) { UpdateLog(stock, NET_ERROR.NET_JSON_NOT_EXISTS); return(NET_ERROR.NET_JSON_NOT_EXISTS);//解析失败 } StockInfoEntity SIE = new StockInfoEntity(); if (jo.Count == 0) { UpdateLog(stock, NET_ERROR.NET_REQ_ERROR); return(NET_ERROR.NET_REQ_ERROR); } foreach (string s in id) { if (jo.Property(s) == null) { UpdateLog(s, NET_ERROR.NET_REQ_ERROR);//股票返回存在性检测 } else { try//尝试json读取股票数据 { JToken j = jo[s]; SIE.name = j["name"].ToString(); SIE.arrow = j["arrow"].ToString(); SIE.high = j["high"].ToString(); SIE.low = j["low"].ToString(); SIE.open = j["open"].ToString(); SIE.percent = j["percent"].ToString(); SIE.price = j["price"].ToString(); SIE.time = j["time"].ToString(); SIE.turnover = j["turnover"].ToString(); SIE.updown = j["updown"].ToString(); SIE.volume = j["volume"].ToString(); SIE.yestclose = j["yestclose"].ToString(); } catch (JsonException) //读取失败则放弃这股票 { UpdateLog(s, NET_ERROR.NET_REQ_ERROR); //股票读取失败 continue; } dict.Add(s, SIE);//成功获取可用数据放入dict中返回 } } return(NET_ERROR.NET_REQ_OK); }
private void sync(StockInfoEntity SIE) { this.SIE = SIE;//委托修改 }