private List<Stock> DownloadStocksInfo(int numberOfStocks, int numberOfDays) { var randomStockNames = GetRandomStocksList(numberOfStocks); List<Stock> stocks = new List<Stock>(); using (var client = new WebClient()) { string baseUrl = "http://ichart.yahoo.com/table.csv?s="; foreach (var currStockName in randomStockNames) { Stock stock = new Stock(); stock.Name = currStockName; try { var fromDateMonth = DateTime.Now.AddDays(-numberOfDays).Month - 1; var fromDateDay = DateTime.Now.AddDays(-numberOfDays).Day; var fromDateYear = DateTime.Now.AddDays(-numberOfDays).Year; var ToDateMonth = DateTime.Now.Month - 1; var ToDateDay = DateTime.Now.Day; var ToDateYear = DateTime.Now.Year; string daysCohise = String.Format("&a={0}&b={1}&c={2}&d={3}&e={4}&f={5}", fromDateMonth, fromDateDay, fromDateYear, ToDateMonth, ToDateDay, ToDateYear); var stockTxt = client.DownloadString(baseUrl + currStockName + daysCohise); char[] delimiters = { '\n' }; var stockList = stockTxt.Trim().Split(delimiters); delimiters[0] = ','; for (int i = 1; i < stockList.Length; i++) { var currRow = stockList[i].Split(delimiters); var day = new StockDayInfo(); day.Date = Convert.ToDateTime(currRow[0]); day.Open = Convert.ToDouble(currRow[1]); day.High = Convert.ToDouble(currRow[2]); day.Low = Convert.ToDouble(currRow[3]); day.Close = Convert.ToDouble(currRow[4]); stock.Days.Add(day); } stocks.Add(stock); } catch (Exception) { Console.WriteLine(currStockName); } } } return stocks; }
private static Stock NormalizeStock(Stock stock) { Stock normelizedStock = new Stock { Name = stock.Name }; foreach (var day in stock.Days) { StockDayInfo normelizedDay = new StockDayInfo(); normelizedDay.Open = NormelizeFeature(day.Open, stock.Days.Select(x => x.Open)); normelizedDay.Close = NormelizeFeature(day.Close, stock.Days.Select(x => x.Close)); normelizedDay.High = NormelizeFeature(day.High, stock.Days.Select(x => x.High)); normelizedDay.Low = NormelizeFeature(day.Low, stock.Days.Select(x => x.Low)); normelizedStock.Days.Add(normelizedDay); } return normelizedStock; }