增加行情接口降级机制:拉不到实时价格时使用成本价作为 fallback

This commit is contained in:
claw_bot 2026-03-10 09:25:43 +00:00
parent 1b8c98b7d6
commit 146212639b

View File

@ -222,11 +222,23 @@ public class PortfolioService : IPortfolioService
continue; continue;
} }
// 获取实时价格(自动路由数据源) // 获取实时价格(自动路由数据源),失败则降级使用成本价
decimal currentPrice = pos.AvgPrice;
decimal previousClose = pos.AvgPrice;
try
{
var priceResponse = await _marketDataService.GetPriceAsync(pos.StockCode, pos.AssetType ?? "Stock"); var priceResponse = await _marketDataService.GetPriceAsync(pos.StockCode, pos.AssetType ?? "Stock");
if (priceResponse.Price > 0)
{
currentPrice = priceResponse.Price;
previousClose = priceResponse.PreviousClose > 0 ? priceResponse.PreviousClose : currentPrice;
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "获取标的 {StockCode} 实时价格失败,使用成本价作为当前价", pos.StockCode);
}
decimal currentPrice = priceResponse.Price;
decimal previousClose = priceResponse.PreviousClose;
decimal currentPositionValue = pos.Shares * currentPrice; decimal currentPositionValue = pos.Shares * currentPrice;
decimal costPositionValue = pos.Shares * pos.AvgPrice; decimal costPositionValue = pos.Shares * pos.AvgPrice;
decimal todayProfit = previousClose > 0 ? pos.Shares * (currentPrice - previousClose) : 0; decimal todayProfit = previousClose > 0 ? pos.Shares * (currentPrice - previousClose) : 0;
@ -289,11 +301,23 @@ public class PortfolioService : IPortfolioService
continue; continue;
} }
// 获取实时价格(自动路由数据源) // 获取实时价格(自动路由数据源),失败则降级使用成本价
decimal currentPrice = pos.AvgPrice;
decimal previousClose = pos.AvgPrice;
try
{
var priceResponse = await _marketDataService.GetPriceAsync(pos.StockCode, pos.AssetType ?? "Stock"); var priceResponse = await _marketDataService.GetPriceAsync(pos.StockCode, pos.AssetType ?? "Stock");
if (priceResponse.Price > 0)
{
currentPrice = priceResponse.Price;
previousClose = priceResponse.PreviousClose > 0 ? priceResponse.PreviousClose : currentPrice;
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "获取标的 {StockCode} 实时价格失败,使用成本价作为当前价", pos.StockCode);
}
decimal currentPrice = priceResponse.Price;
decimal previousClose = priceResponse.PreviousClose;
decimal positionValue = pos.Shares * currentPrice; decimal positionValue = pos.Shares * currentPrice;
decimal cost = pos.Shares * pos.AvgPrice; decimal cost = pos.Shares * pos.AvgPrice;
decimal profit = positionValue - cost; decimal profit = positionValue - cost;
@ -551,10 +575,21 @@ public class PortfolioService : IPortfolioService
continue; continue;
} }
// 获取实时价格(自动路由数据源) // 获取实时价格(自动路由数据源),失败则降级使用成本价
decimal currentPrice = pos.AvgPrice;
try
{
var priceResponse = _marketDataService.GetPriceAsync(pos.StockCode, pos.AssetType ?? "Stock").GetAwaiter().GetResult(); var priceResponse = _marketDataService.GetPriceAsync(pos.StockCode, pos.AssetType ?? "Stock").GetAwaiter().GetResult();
if (priceResponse.Price > 0)
{
currentPrice = priceResponse.Price;
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "获取标的 {StockCode} 实时价格失败,使用成本价计算组合总价值", pos.StockCode);
}
decimal currentPrice = priceResponse.Price;
totalPortfolioValue += pos.Shares * currentPrice; totalPortfolioValue += pos.Shares * currentPrice;
} }