fix: 更新 Tiingo API Key & 统一使用 /tiingo/daily 端点
This commit is contained in:
parent
14b51e636a
commit
d9a8ea84c7
@ -23,7 +23,7 @@ public class MarketDataService : IMarketDataService
|
||||
_logger = logger;
|
||||
_httpClient = httpClientFactory.CreateClient();
|
||||
// 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}");
|
||||
}
|
||||
|
||||
@ -38,21 +38,23 @@ public class MarketDataService : IMarketDataService
|
||||
{
|
||||
_logger.LogInformation($"Requesting stock price for symbol: {symbol}");
|
||||
|
||||
// Tiingo 最新价格端点
|
||||
var url = $"https://api.tiingo.com/iex/{symbol}?token={_tiingoApiKey}";
|
||||
var response = await _httpClient.GetFromJsonAsync<List<TiingoPriceResponse>>(url);
|
||||
// Tiingo 日线最新价格端点(取最近1条)
|
||||
var url = $"https://api.tiingo.com/tiingo/daily/{symbol}/prices?token={_tiingoApiKey}";
|
||||
var response = await _httpClient.GetFromJsonAsync<List<TiingoDailyResponse>>(url);
|
||||
|
||||
if (response == null || response.Count == 0)
|
||||
{
|
||||
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
|
||||
{
|
||||
Symbol = symbol,
|
||||
Price = latest.tngoLast ?? latest.close ?? 0,
|
||||
PreviousClose = latest.prevClose ?? 0,
|
||||
Price = latest.close ?? 0,
|
||||
PreviousClose = prevClose ?? 0,
|
||||
Timestamp = latest.date ?? DateTime.UtcNow,
|
||||
AssetType = "Stock"
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user