fix: 更新 Tiingo API Key & 统一使用 /tiingo/daily 端点

This commit is contained in:
虾球 2026-03-06 08:33:55 +00:00
parent 14b51e636a
commit d9a8ea84c7

View File

@ -23,7 +23,7 @@ public class MarketDataService : IMarketDataService
_logger = logger; _logger = logger;
_httpClient = httpClientFactory.CreateClient(); _httpClient = httpClientFactory.CreateClient();
// TODO: 从配置读取 Tiingo API Key // TODO: 从配置读取 Tiingo API Key
_tiingoApiKey = Environment.GetEnvironmentVariable("TIINGO_API_KEY") ?? "YOUR_TIINGO_API_KEY"; _tiingoApiKey = Environment.GetEnvironmentVariable("TIINGO_API_KEY") ?? "bd00fee76d3012b047473078904001b33322cb46";
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Token {_tiingoApiKey}"); _httpClient.DefaultRequestHeaders.Add("Authorization", $"Token {_tiingoApiKey}");
} }
@ -38,21 +38,23 @@ public class MarketDataService : IMarketDataService
{ {
_logger.LogInformation($"Requesting stock price for symbol: {symbol}"); _logger.LogInformation($"Requesting stock price for symbol: {symbol}");
// Tiingo 最新价格端点 // Tiingo 日线最新价格端点取最近1条
var url = $"https://api.tiingo.com/iex/{symbol}?token={_tiingoApiKey}"; var url = $"https://api.tiingo.com/tiingo/daily/{symbol}/prices?token={_tiingoApiKey}";
var response = await _httpClient.GetFromJsonAsync<List<TiingoPriceResponse>>(url); var response = await _httpClient.GetFromJsonAsync<List<TiingoDailyResponse>>(url);
if (response == null || response.Count == 0) if (response == null || response.Count == 0)
{ {
throw new Exception($"No data found for {symbol}"); throw new Exception($"No data found for {symbol}");
} }
var latest = response[0]; var latest = response[^1];
decimal? prevClose = response.Count >= 2 ? response[^2].close : null;
return new MarketPriceResponse return new MarketPriceResponse
{ {
Symbol = symbol, Symbol = symbol,
Price = latest.tngoLast ?? latest.close ?? 0, Price = latest.close ?? 0,
PreviousClose = latest.prevClose ?? 0, PreviousClose = prevClose ?? 0,
Timestamp = latest.date ?? DateTime.UtcNow, Timestamp = latest.date ?? DateTime.UtcNow,
AssetType = "Stock" AssetType = "Stock"
}; };