private List <YahooHistoricalPriceData> GetHistoricalPriceData(string yahooStockCode, HistoryType historyType, DateTime?startDate, DateTime?endDate) { var historicalDataCsv = GetHistoricalDataAsCsv(yahooStockCode, historyType, startDate, endDate); var historicalPriceData = new List <YahooHistoricalPriceData>(); foreach (var line in historicalDataCsv.Split('\n').Skip(1)) { if (string.IsNullOrEmpty(line)) { continue; } var values = line.Split(','); var newPriceData = new YahooHistoricalPriceData { Date = DateTime.Parse(values[0], CultureInfo.InvariantCulture), Open = decimal.Parse(values[1], CultureInfo.InvariantCulture), High = decimal.Parse(values[2], CultureInfo.InvariantCulture), Low = decimal.Parse(values[3], CultureInfo.InvariantCulture), Close = decimal.Parse(values[4], CultureInfo.InvariantCulture), Volume = long.Parse(values[5], CultureInfo.InvariantCulture), AdjClose = decimal.Parse(values[6], CultureInfo.InvariantCulture) }; historicalPriceData.Add(newPriceData); } return(historicalPriceData); }
private List <YahooHistoricalPriceData> GetHistoricalPriceData(string yahooStockCode, DateTime?startDate, DateTime?endDate, HistoryType historyType) { var dateRangeOption = string.Empty; var addDateRangeOption = historyType == HistoryType.Day && startDate.HasValue && endDate.HasValue; if (addDateRangeOption) { var startDateValue = startDate.Value; var endDateValue = endDate.Value; //date range must be at least 30 days or the API will return a 404 error var dateRangeIsSmallerThenMinimum = startDateValue > endDateValue.AddDays(MinimumDateRangeDays); if (dateRangeIsSmallerThenMinimum) { startDateValue = endDateValue.AddDays(MinimumDateRangeDays); } dateRangeOption = GetDateRangeOption(startDateValue, endDateValue); } var historyTypeOption = GetHistoryType(historyType); var options = $"{dateRangeOption}{historyTypeOption}"; var historicalDataCsv = YahooApiRequest(yahooStockCode, options); var historicalPriceData = new List <YahooHistoricalPriceData>(); foreach (var line in historicalDataCsv.Split('\n').Skip(1)) { if (string.IsNullOrEmpty(line)) { continue; } var values = line.Split(','); var newPriceData = new YahooHistoricalPriceData { Date = DateTime.Parse(values[0]), Open = decimal.Parse(values[1]), High = decimal.Parse(values[2]), Low = decimal.Parse(values[3]), Close = decimal.Parse(values[4]), Volume = int.Parse(values[5]), AdjClose = decimal.Parse(values[6]) }; historicalPriceData.Add(newPriceData); } return(historicalPriceData); }