in workshop/dotnet/Core.Utilities/Services/StocksService.cs [45:75]
public async Task<Stock> GetStockAggregate(string symbol, DateTime? date = null)
{
logger.LogDebug("> Getting stock aggregate");
//Default to yesterday's date if no date is provided as current day pricing requires premium subscription
DateTime toDate = date ?? DateTime.Now.AddDays(-1);
string toDateFormatted = toDate.ToString("yyyy-MM-dd");
//Default fromDate to 4 days ago to account for weekends and Monday holidays
string fromDateFormatted = toDate.AddDays(-4).ToString("yyyy-MM-dd");
var requestUri = $"/v2/aggs/ticker/{symbol}/range/1/day/{fromDateFormatted}/{toDateFormatted}?adjusted=true&sort=desc&apiKey=";
// Log the requestUri without the apiKey
logger.LogDebug($"Request URI: {requestUri}");
requestUri += $"{_apiKey}";
AggregateStockResponse response = await GetHttpResponse<AggregateStockResponse>(requestUri);
Stock stock = null;
if (response.StockResults.Count > 0) {
StockResult stockResult = response.StockResults[0];
stock = new Stock(
Symbol: symbol,
Open: stockResult.Open,
High: stockResult.High,
Low: stockResult.Low,
Close: stockResult.Close,
From: toDateFormatted,
Status: response.Status
);
}
logger.LogDebug("< Getting stock aggregate count: " + response.ResultsCount);
return stock;
}