public async Task <IStockData> GetStockDataAsync(string symbol, DateTime start, DateTime end, CancellationToken cancellationToken) { const string format = "https://query1.finance.yahoo.com/v7/finance/download/{0}?period1={1}&period2={2}&interval=1d&events=history&crumb={3}"; var requestUri = string.Format(format, symbol, (start - UnixEpoch).TotalSeconds, (end - UnixEpoch).TotalSeconds, crumb); using (var stream = await client.GetStreamAsync(requestUri).ConfigureAwait(false)) return(await YahooStockData.LoadAsync(stream, cancellationToken).ConfigureAwait(false)); }
public static async Task <YahooStockData> LoadAsync(Stream stream, CancellationToken cancellationToken) { var stockData = new YahooStockData(); using (var reader = new StreamReader(stream, Encoding.ASCII, false, 4096, true)) { var line = await reader.ReadLineAsync(); var tokens = line.Split(CsvDelimeters); stockData.headers = tokens; while ((line = await reader.ReadLineAsync()) != null) { tokens = line.Split(CsvDelimeters); if (tokens.Length != stockData.headers.Length) { Console.WriteLine("Inconsistent number of columns: {0} vs {1}", tokens.Length, stockData.headers.Length); } var values = new object[tokens.Length]; if (tokens[0] != "null") { values[0] = DateTime.Parse(tokens[0], CultureInfo.InvariantCulture); } for (int i = 1; i < tokens.Length; i++) { if (tokens[i] == "null") { continue; } if (!double.TryParse(tokens[i], CsvValueStyle, CultureInfo.InvariantCulture, out var value)) { Console.WriteLine("Failed to parse CSV double value: {0}", tokens[i]); } else { values[i] = value; } } stockData.values.Add(values); } } return(stockData); }