public async Task<IEnumerable<Quote>> GetHistoricalData(string quoteSymbol, DateTime from, DateTime to) { // link example: // http://real-chart.finance.yahoo.com/table.csv?s=GOOG&d=11&e=21&f=2015&g=d&a=2&b=27&c=2014&ignore=.csv using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri("http://real-chart.finance.yahoo.com/"); string request = String.Format("table.csv?s={0}&d={1}&e={2}&f={3}&g=d&a={4}&b={5}&c={6}&ignore=.csv", quoteSymbol, to.Month - 1, to.Day, to.Year, from.Month - 1, from.Day, from.Year ); using (Stream stream = await client.GetStreamAsync(request)) using (StreamReader reader = new StreamReader(stream)) { // first row in csv contains column definitions reader.ReadLine(); List<Quote> quoteList = new List<Quote>(); CultureInfo en = new CultureInfo("en-US"); // for correct parsing while (reader.Peek() > 0) { string[] values = reader.ReadLine().Split(','); Quote quote = new Quote(); quote.Date = DateTime.Parse(values[0], en); quote.Open = decimal.Parse(values[1], en); quote.High = decimal.Parse(values[2], en); quote.Low = decimal.Parse(values[3], en); quote.Close = decimal.Parse(values[4], en); quote.Volume = uint.Parse(values[5]); quoteList.Add(quote); } return quoteList; } } }
public async Task<IEnumerable<Quote>> GetHistoricalData(string quoteSymbol, DateTime from, DateTime to) { // link example: // http://www.google.com/finance/historical?q=NASDAQ:MSFT&startdate=Nov+1%2C+2010&enddate=Nov+15%2C+2011&output=csv using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri("http://www.google.com/finance/"); CultureInfo en = new CultureInfo("en-US"); string request = String.Format("historical?q=NASDAQ:{0}&startdate={1}&enddate={2}&output=csv", quoteSymbol, from.ToString("MMM+dd\\%2C+yyyy", en), to.ToString("MMM+dd\\%2C+yyyy", en) ); using (Stream stream = await client.GetStreamAsync(request)) using (StreamReader reader = new StreamReader(stream)) { // first row in csv contains column definitions reader.ReadLine(); List<Quote> quoteList = new List<Quote>(); while (reader.Peek() > 0) { string[] values = reader.ReadLine().Split(','); Quote quote = new Quote(); quote.Date = DateTime.Parse(values[0], en); quote.Open = decimal.Parse(values[1], en); quote.High = decimal.Parse(values[2], en); quote.Low = decimal.Parse(values[3], en); quote.Close = decimal.Parse(values[4], en); quote.Volume = uint.Parse(values[5]); quoteList.Add(quote); } return quoteList; } } }