From aa4f63455b8449f1b88db2160074916c16eb45f7 Mon Sep 17 00:00:00 2001 From: OpenClaw Agent Date: Tue, 17 Mar 2026 04:22:00 +0000 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96YahooMarketServic?= =?UTF-8?q?e=EF=BC=8C=E5=A4=8D=E7=94=A8=E5=AE=9E=E4=BE=8B=E5=B9=B6?= =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=BC=82=E5=B8=B8=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/YahooMarketService.cs | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/AssetManager.Infrastructure/Services/YahooMarketService.cs b/AssetManager.Infrastructure/Services/YahooMarketService.cs index 079573c..b688299 100644 --- a/AssetManager.Infrastructure/Services/YahooMarketService.cs +++ b/AssetManager.Infrastructure/Services/YahooMarketService.cs @@ -34,7 +34,10 @@ public class YahooMarketService : IYahooMarketService public YahooMarketService(ILogger logger) { _logger = logger; - _yahooQuotes = new YahooQuotesBuilder().Build(); + // 默认获取最近60天的历史数据 + _yahooQuotes = new YahooQuotesBuilder() + .WithHistoryStartDate(Instant.FromDateTimeUtc(DateTime.UtcNow.AddDays(-60))) + .Build(); } public async Task GetStockPriceAsync(string symbol) @@ -43,11 +46,11 @@ public class YahooMarketService : IYahooMarketService Snapshot? snapshot = await _yahooQuotes.GetSnapshotAsync(symbol); if (snapshot is null) - throw new Exception($"Yahoo未知标的: {symbol}"); + throw new InvalidOperationException($"Yahoo未知标的: {symbol}"); decimal price = snapshot.RegularMarketPrice; if (price <= 0) - throw new Exception($"Yahoo获取价格失败,标的: {symbol}"); + throw new InvalidOperationException($"Yahoo获取价格失败,标的: {symbol}"); decimal previousClose = snapshot.RegularMarketPreviousClose; if (previousClose <= 0) @@ -70,16 +73,9 @@ public class YahooMarketService : IYahooMarketService { _logger.LogInformation("Yahoo获取历史数据: {Symbol}, {Timeframe}, {Limit}", symbol, timeframe, limit); - // 计算历史数据的开始时间 - Instant startDate = Instant.FromDateTimeUtc(DateTime.UtcNow.AddDays(-limit * 2)); - - YahooQuotes yahooQuotes = new YahooQuotesBuilder() - .WithHistoryStartDate(startDate) - .Build(); - - Result result = await yahooQuotes.GetHistoryAsync(symbol); + Result result = await _yahooQuotes.GetHistoryAsync(symbol); if (result.Value == null) - throw new Exception($"Yahoo获取历史数据失败,标的: {symbol}"); + throw new InvalidOperationException($"Yahoo获取历史数据失败,标的: {symbol}"); History history = result.Value; var ticks = history.Ticks;