internal static void GetStockHistory(ref List <Historical> historicalList, Portfolio portfolio) { string csvData = null; Historical historical = null; historicalList = new List <Historical>(); foreach (var company in portfolio.Buys) { try { using (WebClient web = new WebClient()) { csvData = web.DownloadString("http://www.google.com/finance/historical?q=" + company.TickerSymbol + "&histperiod=monthly&output=csv"); if (csvData != null) { historical = new Historical(); historical.historicalPrices = new List <HistoricalPrices>(); historical.Symbol = company.TickerSymbol; historical = ParseHistoryListings(historical, csvData); historicalList.Add(historical); } } } catch { } } }
private static Historical ParseHistoryListings(Historical historical, string csvData) { string path = HostingEnvironment.MapPath("~/Error Log File/Error.txt"); char[] delimiter = { ',', '\t', '\r', '\n', '"', '/', '{', '}' }; string[] rows = csvData.Replace("\r", "").Split('\n'); HistoricalPrices price = null; SailiRepository repository = new SailiRepository(); bool pass = false; foreach (string row in rows) { if (!pass) { pass = true; continue; } if (string.IsNullOrEmpty(row)) { continue; } string[] cols = row.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); decimal convert = 0; bool result = Decimal.TryParse(cols[1].ToString(), out convert); if (!result) { SaveErrorInRequest(path, cols); continue; } result = Decimal.TryParse(cols[2].ToString(), out convert); if (!result) { SaveErrorInRequest(path, cols); continue; } price = new HistoricalPrices(); price.Date = cols[0].ToString(); price.Open = Convert.ToDecimal(cols[1].ToString()); price.Close = Convert.ToDecimal(cols[4].ToString()); historical.historicalPrices.Add(price); } return(historical); }