/// <summary> /// Takes a XDocument, parses it and returns a list of stock objects that corresponds to valid /// stock symbols /// </summary> /// <param name="doc"></param> /// <returns></returns> public static List <Stock> getValidStocks(XDocument doc) { List <Stock> stocks = new List <Stock>(); foreach (var root in doc.Root.Elements("finance")) { try { if (root.Element("last") != null && root.Element("last").Attribute("data").Value != null && root.Element("last").Attribute("data").Value.Equals("0.00") == false) { stocks.Add(Stock_quotes.createNewStock(root)); } else { System.Windows.Forms.MessageBox.Show(root.Element("symbol").Attribute("data").Value + " is not a valid stock symbol"); } } catch (Exception er) { //Error message } } return(stocks); }
void updateTimer_Tick(object sender, EventArgs e) { //Fetching all the stocks at once in XDocument file XDocument doc = Stock_quotes.FetchQuote(this.getAllSymbolsFromTable(data_table) + Main_view.market_symbol_string); //This will update the data_table this.addValuesToTheTable(data_table, doc); //This will update the market_table this.addValuesToTheTable(market_data_table, doc); }
/// <summary> /// Retrieves a particular stock from the XDocument. /// </summary> /// <param name="doc"></param> /// <param name="symbol"></param> /// <param name="lookUpField"></param> /// <returns></returns> public static Stock getThisStock(XDocument doc, string symbol, string lookUpField) { Stock stock = null; foreach (var root in doc.Root.Elements("finance")) { if (root.Element(lookUpField).Attribute("data").Value.Equals(symbol)) { return(Stock_quotes.createNewStock(root)); } } return(stock); }
/// <summary> /// Adds a stock symbol to the table or throws an ArgumentException /// </summary> /// <param name="symbol">symbol(s) to the added. Multiple entries are allowed that are separated by " " or ","</param> /// <param name="table"></param> public void addStockSymbolToTheTable(string symbol, DataTable table) { if (symbol != null && symbol.Length > 0) { XDocument xDoc = Stock_quotes.FetchQuote(symbol); List <Stock> list = Stock_quotes.getValidStocks(xDoc); foreach (Stock stock in list) { table.Rows.Add(stock.Symbol, stock.Company, stock.Date, stock.Time, stock.Y_close, stock.Trade, stock.Chg, stock.Perc_chg, stock.Volume, stock.High, stock.Low, stock.Chart_url, stock.Market_cap, stock.Exchange, stock.Currency); } } else { throw new ArgumentException("Added symbol is not accepted as a valid input"); } }
/// <summary> /// Updates the table data /// </summary> /// <param name="table"></param> /// <param name="doc"></param> public void addValuesToTheTable(DataTable table, XDocument doc) { foreach (DataRow row in table.Rows) { Stock stock = Stock_quotes.getThisStock(doc, (string)row["Symbol"], "symbol"); row["Symbol"] = stock.Symbol; row["Company"] = stock.Company; row["Date"] = stock.Date; row["Time"] = stock.Time; row["Closed Yesterday"] = stock.Y_close; row["Trade"] = stock.Trade; row["Chg"] = stock.Chg; row["%Chg"] = stock.Perc_chg; row["Volume"] = stock.Volume; row["High"] = stock.High; row["Low"] = stock.Low; row["Chart"] = stock.Chart_url; row["Market Cap"] = stock.Market_cap; row["Exchange"] = stock.Exchange; row["Currency"] = stock.Currency; } }
/// <summary> /// Creates a new Stock from XElement. /// </summary> /// <param name="root"></param> /// <returns></returns> public static Stock createNewStock(XElement root) { Stock stock = new Stock(); stock.Symbol = root.Element("symbol").Attribute("data").Value; DateTime eastern = Stock_quotes.UTCtoEastern(root.Element("current_date_utc").Attribute("data").Value, root.Element("current_time_utc").Attribute("data").Value); stock.Date = eastern.ToShortDateString(); stock.Time = eastern.ToLongTimeString(); stock.Trade = root.Element("last").Attribute("data").Value; stock.Chg = root.Element("change").Attribute("data").Value; stock.Perc_chg = root.Element("perc_change").Attribute("data").Value; stock.Volume = root.Element("volume").Attribute("data").Value; stock.High = root.Element("high").Attribute("data").Value; stock.Low = root.Element("low").Attribute("data").Value; stock.Chart_url = "https://www.google.com" + root.Element("chart_url").Attribute("data").Value; stock.Market_cap = root.Element("market_cap").Attribute("data").Value; stock.Exchange = root.Element("exchange").Attribute("data").Value; stock.Currency = root.Element("currency").Attribute("data").Value; stock.Company = root.Element("company").Attribute("data").Value; stock.Y_close = root.Element("y_close").Attribute("data").Value; return(stock); }