From 02c08643937e2d08c71f0a5fe00d80e1f674a0a5 Mon Sep 17 00:00:00 2001 From: claw_bot Date: Tue, 10 Mar 2026 05:42:45 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=9B=E5=BB=BA=E7=BB=84=E5=90=88?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E7=AD=96=E7=95=A5=E6=97=B6=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=B8=A6=E5=85=A5=E7=AD=96=E7=95=A5=E9=85=8D=E7=BD=AE=E7=9A=84?= =?UTF-8?q?=E6=A0=87=E7=9A=84=E4=BD=9C=E4=B8=BA=E5=88=9D=E5=A7=8B=E6=8C=81?= =?UTF-8?q?=E4=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AssetManager.Services/PortfolioService.cs | 49 ++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/AssetManager.Services/PortfolioService.cs b/AssetManager.Services/PortfolioService.cs index 84a88d3..3074f15 100644 --- a/AssetManager.Services/PortfolioService.cs +++ b/AssetManager.Services/PortfolioService.cs @@ -36,8 +36,55 @@ public class PortfolioService : IPortfolioService _db.Insertable(portfolio).ExecuteCommand(); + // 如果选择了策略,自动加载策略配置的标的作为初始持仓 + var strategyStocks = new List(); + if (!string.IsNullOrEmpty(request.strategyId)) + { + var strategy = _db.Queryable() + .Where(s => s.Id == request.strategyId && s.UserId == userId) + .First(); + + if (strategy != null && !string.IsNullOrEmpty(strategy.Config)) + { + try + { + // 风险平价策略 + if (strategy.Type?.Equals("risk_parity", StringComparison.OrdinalIgnoreCase) == true) + { + var config = System.Text.Json.JsonSerializer.Deserialize(strategy.Config); + if (config?.Assets != null) + { + foreach (var asset in config.Assets) + { + if (!string.IsNullOrEmpty(asset.Symbol)) + { + strategyStocks.Add(new StockItem + { + code = asset.Symbol, + name = asset.Symbol, + price = 0, // 价格留空,用户后续填写 + amount = 0, // 数量留空,用户后续填写 + currency = request.currency, + assetType = "Stock" + }); + } + } + } + } + // 其他策略类型可以在这里扩展 + } + catch (Exception ex) + { + _logger.LogError(ex, "解析策略配置失败,策略ID: {StrategyId}", request.strategyId); + } + } + } + + // 合并用户传入的持仓和策略自动生成的持仓 + var allStocks = (request.stocks ?? new List()).Concat(strategyStocks).DistinctBy(s => s.code).ToList(); + // 创建初始持仓 - foreach (var stock in request.stocks ?? new List()) + foreach (var stock in allStocks) { if (stock.code == null || stock.name == null) {