/// <summary> /// Get stock summary by symbol name. /// </summary> /// <param name="symbol">symbol name</param> /// <returns></returns> public static StockSummary GetStock(string symbol) { StockSummary stock = null; try { stock = GetStockFromYahoo(symbol); } catch { try { CacheData cacheData = new CacheData(); stock = cacheData.GetCachedStock(symbol); } catch { } } return(stock); }
/// <summary> /// Get stock list by a list of symbol from yahoo API /// </summary> /// <param name="Symbols">symbol list (separator ',')</param> /// <returns></returns> public static List<StockSummary> GetStocksFromYahoo(string Symbols) { List<StockSummary> lst = new List<StockSummary>(); string[] symbols = Symbols.Replace(",", " ").Split(' '); string query = "q=select%20symbol%2CSymbol%2CName%2CAsk%2CBid%2CChange%2CLastTradePriceOnly%20from%20yahoo.finance.quotes%20where%20symbol%20in%20({0})&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"; string symbolList = String.Join("%2C", symbols.Select(w => "%22" + w + "%22").ToArray()); string url = GetFullUrl(string.Format(query, symbolList)); XDocument doc = GetYQLXDoc(url); XElement results = doc.Root.Element("results"); var stockList = results.Elements("quote"); stockList.First(); //string content = ""; for (int i = 0; i < symbols.Length; i++) { XElement node; // Loop through each line from the stream, building the return XML Document string if (symbols[i].Trim() == "" || symbols[i] == null) continue; var symbol = stockList.Where(p => p.Attribute("symbol").Value == symbols[i]); if (symbol.Count() != 0) { node = symbol.First(); } else { continue; } // If contents[2] = "N/A". the stock symbol is invalid. if (node == null) { StockSummary stock = new StockSummary(); stock.Valid = false; } else { StockSummary stock = new StockSummary(); if (node.Element("Symbol").Value != null) { stock.Symbol = node.Element("Symbol").Value; } if (node.Element("Name") != null) { stock.Name = node.Element("Name").Value; } if (node.Element("Ask") != null)//show N/A when null { //Random d = new Random(); //float q = (float)d.Next(-100, 100) / 100F; //stock.Ask = ParseFloat(node.Element("Ask").Value) + q stock.Ask = ParseFloat(node.Element("Ask").Value); } if (node.Element("Bid") != null) { stock.Bid = ParseFloat(node.Element("Bid").Value); } if (node.Element("LastTradePriceOnly") != null) { stock.LastSale = ParseFloat(node.Element("LastTradePriceOnly").Value); } lst.Add(stock); } } return lst; }
/// <summary> /// Get stock list by a list of symbol from yahoo API /// </summary> /// <param name="Symbols">symbol list (separator ',')</param> /// <returns></returns> public static List <StockSummary> GetStocksFromYahoo(string Symbols) { List <StockSummary> lst = new List <StockSummary>(); string[] symbols = Symbols.Replace(",", " ").Split(' '); string query = "q=select%20symbol%2CSymbol%2CName%2CAsk%2CBid%2CChange%2CLastTradePriceOnly%20from%20yahoo.finance.quotes%20where%20symbol%20in%20({0})&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"; string symbolList = String.Join("%2C", symbols.Select(w => "%22" + w + "%22").ToArray()); string url = GetFullUrl(string.Format(query, symbolList)); XDocument doc = GetYQLXDoc(url); XElement results = doc.Root.Element("results"); var stockList = results.Elements("quote"); stockList.First(); //string content = ""; for (int i = 0; i < symbols.Length; i++) { XElement node; // Loop through each line from the stream, building the return XML Document string if (symbols[i].Trim() == "" || symbols[i] == null) { continue; } var symbol = stockList.Where(p => p.Attribute("symbol").Value == symbols[i]); if (symbol.Count() != 0) { node = symbol.First(); } else { continue; } // If contents[2] = "N/A". the stock symbol is invalid. if (node == null) { StockSummary stock = new StockSummary(); stock.Valid = false; } else { StockSummary stock = new StockSummary(); if (node.Element("Symbol").Value != null) { stock.Symbol = node.Element("Symbol").Value; } if (node.Element("Name") != null) { stock.Name = node.Element("Name").Value; } if (node.Element("Ask") != null) //show N/A when null { //Random d = new Random(); //float q = (float)d.Next(-100, 100) / 100F; //stock.Ask = ParseFloat(node.Element("Ask").Value) + q stock.Ask = ParseFloat(node.Element("Ask").Value); } if (node.Element("Bid") != null) { stock.Bid = ParseFloat(node.Element("Bid").Value); } if (node.Element("LastTradePriceOnly") != null) { stock.LastSale = ParseFloat(node.Element("LastTradePriceOnly").Value); } lst.Add(stock); } } return(lst); }