diff --git a/AssetManager.Services/PortfolioService.cs b/AssetManager.Services/PortfolioService.cs index 9c8f6a7..bcaf145 100644 --- a/AssetManager.Services/PortfolioService.cs +++ b/AssetManager.Services/PortfolioService.cs @@ -214,7 +214,8 @@ public class PortfolioService : IPortfolioService .Where(pos => pos.PortfolioId == id) .ToList(); - // 获取每个持仓的实时价格并计算 + // 获取每个持仓的实时价格并转换为组合本位币 + string targetCurrency = portfolio.Currency ?? "CNY"; decimal totalPortfolioValue = 0; decimal totalCost = 0; decimal totalTodayProfit = 0; @@ -238,9 +239,14 @@ public class PortfolioService : IPortfolioService double profitRate = cost > 0 ? (double)(profit / cost * 100) : 0; decimal todayProfit = previousClose > 0 ? pos.Shares * (currentPrice - previousClose) : 0; - totalPortfolioValue += positionValue; - totalCost += cost; - totalTodayProfit += todayProfit; + // 转换为组合本位币 + decimal positionValueInTarget = await _exchangeRateService.ConvertAmountAsync(positionValue, pos.Currency, targetCurrency); + decimal costInTarget = await _exchangeRateService.ConvertAmountAsync(cost, pos.Currency, targetCurrency); + decimal todayProfitInTarget = await _exchangeRateService.ConvertAmountAsync(todayProfit, pos.Currency, targetCurrency); + + totalPortfolioValue += positionValueInTarget; + totalCost += costInTarget; + totalTodayProfit += todayProfitInTarget; positionItems.Add(new PositionItem { @@ -251,13 +257,13 @@ public class PortfolioService : IPortfolioService amount = (int)pos.Shares, averagePrice = (double)pos.AvgPrice, currentPrice = (double)currentPrice, - totalValue = (double)positionValue, - profit = (double)profit, + totalValue = (double)positionValueInTarget, + profit = (double)(positionValueInTarget - costInTarget), profitRate = profitRate, - changeAmount = (double)todayProfit, + changeAmount = (double)todayProfitInTarget, ratio = 0, // 后面统一计算比例 deviationRatio = 0, // 后续实现 - currency = pos.Currency + currency = targetCurrency }); } @@ -274,7 +280,7 @@ public class PortfolioService : IPortfolioService { id = portfolio.Id, name = portfolio.Name, - currency = portfolio.Currency, + currency = targetCurrency, status = portfolio.Status, strategy = new StrategyInfo { @@ -287,7 +293,7 @@ public class PortfolioService : IPortfolioService todayProfit = (double)totalTodayProfit, historicalChange = totalReturnRate, dailyVolatility = 0, // 后续实现 - todayProfitCurrency = portfolio.Currency, + todayProfitCurrency = targetCurrency, logicModel = "HFEA 风险平价逻辑", logicModelStatus = "监控中", logicModelDescription = "目标权重 季度调仓",