private void CacheStock(string symbol) { UpdateCache(GetKey(symbol, "StockSummary"), () => { StockDetails stock = Stocks.GetStockFromYahoo(symbol) as StockDetails; Serialize <StockDetails>(GetPath(symbol, "StockSummary"), stock); }); }
/// <summary> /// Get stock history by symbol name and the date range from yahoo API. /// </summary> /// <param name="Symbol"></param> /// <param name="startDate"></param> /// <param name="endDate"></param> /// <param name="interval"></param> /// <returns></returns> public static List <StockSummary> GetStockHistoryFromYahoo(string Symbol, DateTime startDate, DateTime endDate, string interval) { if (interval == "") { interval = "d"; } List <StockSummary> lst = new List <StockSummary>(); string query = "q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol=%22{0}%22%20and%20startDate=%22{1}%22%20and%20endDate=%22{2}%22&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"; string url = GetFullUrl(string.Format(query, Symbol, startDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"))); XDocument doc = GetYQLXDoc(url); XElement results = doc.Root.Element("results"); foreach (XElement ele in results.Elements("quote")) { StockDetails stock = new StockDetails(); if (ele.Element("Open") != null && ele.Element("Open").Value != "") { stock.Open = float.Parse(ele.Element("Open").Value); } if (ele.Element("Close") != null && ele.Element("Close").Value != "") { stock.Close = float.Parse(ele.Element("Close").Value); } if (ele.Element("High") != null && ele.Element("High").Value != "") { stock.DayHigh = float.Parse(ele.Element("High").Value); } if (ele.Element("Low") != null && ele.Element("Low").Value != "") { stock.DayLow = float.Parse(ele.Element("Low").Value); } if (ele.Element("Volume") != null && ele.Element("Volume").Value != "") { stock.Volume = long.Parse(ele.Element("Volume").Value); } stock.Valid = true; stock.Symbol = Symbol; //stock.DateString = DateTime.Parse(ele.Element("Date").Value).ToString("MM/dd/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo); // DateTime.ToString("MM/dd/yyyy") return MM-dd-yyyy (09-07-2011) in Chinese region. if (ele.Element("Date") != null && ele.Element("Date").Value != "") { stock.DateString = DateTime.Parse(ele.Element("Date").Value).ToString(@"MM'/'dd'/'yyyy"); } lst.Add(stock); } return(lst); }
/// <summary> /// Get stock summary by symbol name from yahoo API. /// </summary> /// <param name="symbol">symbol name</param> /// <returns></returns> public static StockSummary GetStockFromYahoo(string symbol) { StockDetails stock = new StockDetails(); string query = "q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22{0}%22)&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"; string url = GetFullUrl(string.Format(query, symbol)); XDocument doc = GetYQLXDoc(url); XElement results = doc.Root.Element("results"); XElement node = results.Elements("quote").Where(p => p.Attribute("symbol").Value == symbol).First(); // If contents[2] = "N/A". the stock symbol is invalid. if (node == null) { stock.Valid = false; } else { if (node.Element("Open") != null) { stock.Open = ParseFloat(node.Element("Open").Value); } if (node.Element("PreviousClose") != null) { stock.Close = ParseFloat(node.Element("PreviousClose").Value); } if (node.Element("DaysHigh") != null) { stock.DayHigh = ParseFloat(node.Element("DaysHigh").Value); } if (node.Element("DaysLow") != null) { stock.DayLow = ParseFloat(node.Element("DaysLow").Value); } if (node.Element("YearHigh") != null) { stock.YearHigh = ParseFloat(node.Element("YearHigh").Value); } if (node.Element("YearLow") != null) { stock.YearLow = ParseFloat(node.Element("YearLow").Value); } if (node.Element("Volume") != null && node.Element("Volume").Value != "") { stock.Volume = long.Parse(node.Element("Volume").Value); } if (node.Element("PERatio").Value != null) { stock.PE = ParseFloat(node.Element("PERatio").Value); } 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("Change") != null) { stock.Change = ParseFloat(node.Element("Change").Value); } if (node.Element("ChangeinPercent") != null) { stock.ChangePercent = node.Element("ChangeinPercent").Value; } if (node.Element("Ask") != null)//show N/A when null { stock.Ask = ParseFloat(node.Element("Ask").Value); } if (node.Element("Bid") != null) { stock.Bid = ParseFloat(node.Element("Bid").Value); } //LastSale //LastTradePriceOnly //EarningsShare // if (node.Element("DividendYield") != null) { stock.DividendYield = node.Element("DividendYield").Value; } if (node.Element("EarningsShare") != null) { stock.EarningsShare = ParseFloat(node.Element("EarningsShare").Value); } if (node.Element("LastTradePriceOnly") != null) { stock.LastSale = ParseFloat(node.Element("LastTradePriceOnly").Value); } if (node.Element("OneyrTargetPrice") != null) { stock.OneyrTargetPrice = ParseFloat(node.Element("OneyrTargetPrice").Value); } if (node.Element("AverageDailyVolume") != null) { stock.AverageDailyVolume = long.Parse(node.Element("AverageDailyVolume").Value); } if (node.Element("MarketCapitalization") != null) { stock.MarketCapitalization = node.Element("MarketCapitalization").Value; } stock.DateString = DateTime.Now.Date.ToString(); } return stock; }
/// <summary> /// Get stock history by symbol name and the date range from yahoo API. /// </summary> /// <param name="Symbol"></param> /// <param name="startDate"></param> /// <param name="endDate"></param> /// <param name="interval"></param> /// <returns></returns> public static List<StockSummary> GetStockHistoryFromYahoo(string Symbol, DateTime startDate, DateTime endDate, string interval) { if (interval == "") { interval = "d"; } List<StockSummary> lst = new List<StockSummary>(); string query = "q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol=%22{0}%22%20and%20startDate=%22{1}%22%20and%20endDate=%22{2}%22&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"; string url = GetFullUrl(string.Format(query, Symbol, startDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"))); XDocument doc = GetYQLXDoc(url); XElement results = doc.Root.Element("results"); foreach (XElement ele in results.Elements("quote")) { StockDetails stock = new StockDetails(); if (ele.Element("Open") != null && ele.Element("Open").Value != "") { stock.Open = float.Parse(ele.Element("Open").Value); } if (ele.Element("Close") != null && ele.Element("Close").Value != "") { stock.Close = float.Parse(ele.Element("Close").Value); } if (ele.Element("High") != null && ele.Element("High").Value != "") { stock.DayHigh = float.Parse(ele.Element("High").Value); } if (ele.Element("Low") != null && ele.Element("Low").Value != "") { stock.DayLow = float.Parse(ele.Element("Low").Value); } if (ele.Element("Volume") != null && ele.Element("Volume").Value != "") { stock.Volume = long.Parse(ele.Element("Volume").Value); } stock.Valid = true; stock.Symbol = Symbol; //stock.DateString = DateTime.Parse(ele.Element("Date").Value).ToString("MM/dd/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo); // DateTime.ToString("MM/dd/yyyy") return MM-dd-yyyy (09-07-2011) in Chinese region. if (ele.Element("Date") != null && ele.Element("Date").Value != "") { stock.DateString = DateTime.Parse(ele.Element("Date").Value).ToString(@"MM'/'dd'/'yyyy"); } lst.Add(stock); } return lst; }
/// <summary> /// Get stock summary by symbol name from yahoo API. /// </summary> /// <param name="symbol">symbol name</param> /// <returns></returns> public static StockSummary GetStockFromYahoo(string symbol) { StockDetails stock = new StockDetails(); string query = "q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22{0}%22)&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"; string url = GetFullUrl(string.Format(query, symbol)); XDocument doc = GetYQLXDoc(url); XElement results = doc.Root.Element("results"); XElement node = results.Elements("quote").Where(p => p.Attribute("symbol").Value == symbol).First(); // If contents[2] = "N/A". the stock symbol is invalid. if (node == null) { stock.Valid = false; } else { if (node.Element("Open") != null) { stock.Open = ParseFloat(node.Element("Open").Value); } if (node.Element("PreviousClose") != null) { stock.Close = ParseFloat(node.Element("PreviousClose").Value); } if (node.Element("DaysHigh") != null) { stock.DayHigh = ParseFloat(node.Element("DaysHigh").Value); } if (node.Element("DaysLow") != null) { stock.DayLow = ParseFloat(node.Element("DaysLow").Value); } if (node.Element("YearHigh") != null) { stock.YearHigh = ParseFloat(node.Element("YearHigh").Value); } if (node.Element("YearLow") != null) { stock.YearLow = ParseFloat(node.Element("YearLow").Value); } if (node.Element("Volume") != null && node.Element("Volume").Value != "") { stock.Volume = long.Parse(node.Element("Volume").Value); } if (node.Element("PERatio").Value != null) { stock.PE = ParseFloat(node.Element("PERatio").Value); } 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("Change") != null) { stock.Change = ParseFloat(node.Element("Change").Value); } if (node.Element("ChangeinPercent") != null) { stock.ChangePercent = node.Element("ChangeinPercent").Value; } if (node.Element("Ask") != null) //show N/A when null { stock.Ask = ParseFloat(node.Element("Ask").Value); } if (node.Element("Bid") != null) { stock.Bid = ParseFloat(node.Element("Bid").Value); } //LastSale //LastTradePriceOnly //EarningsShare // if (node.Element("DividendYield") != null) { stock.DividendYield = node.Element("DividendYield").Value; } if (node.Element("EarningsShare") != null) { stock.EarningsShare = ParseFloat(node.Element("EarningsShare").Value); } if (node.Element("LastTradePriceOnly") != null) { stock.LastSale = ParseFloat(node.Element("LastTradePriceOnly").Value); } if (node.Element("OneyrTargetPrice") != null) { stock.OneyrTargetPrice = ParseFloat(node.Element("OneyrTargetPrice").Value); } if (node.Element("AverageDailyVolume") != null) { stock.AverageDailyVolume = long.Parse(node.Element("AverageDailyVolume").Value); } if (node.Element("MarketCapitalization") != null) { stock.MarketCapitalization = node.Element("MarketCapitalization").Value; } stock.DateString = DateTime.Now.Date.ToString(); } return(stock); }