From c9a0711438d32f33b65ffa29d8efd7aa37d61bdc Mon Sep 17 00:00:00 2001
From: log
Date: Thu, 23 Jul 2026 21:54:18 +0800
Subject: [PATCH] feat: add validated ML factor model training

---
 scripts/portfolio_monitor.py |  105 ++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 72 insertions(+), 33 deletions(-)

diff --git a/scripts/portfolio_monitor.py b/scripts/portfolio_monitor.py
index a621b8b..3709411 100755
--- a/scripts/portfolio_monitor.py
+++ b/scripts/portfolio_monitor.py
@@ -56,17 +56,17 @@
             波动率信息字典
         """
         if not portfolio.get('positions'):
-            return {'volatility': 0, 'status': '无持仓', 'level': 'info'}
+            return {'volatility': 0, 'status': '无持仓', 'level': 'info', 'value': 'N/A'}
 
         positions = portfolio['positions']
 
         # 获取持仓股票代码
-        stock_codes = [pos['code'] for pos in positions]
-        weights = [pos.get('weight', 0) for pos in positions]
+        stock_codes = [pos['stock_code'] for pos in positions]
+        weights = [pos.get('current_value', 0) for pos in positions]
         total_weight = sum(weights)
 
         if total_weight == 0:
-            return {'volatility': 0, 'status': '空仓', 'level': 'info'}
+            return {'volatility': 0, 'status': '空仓', 'level': 'info', 'value': 'N/A'}
 
         weights = [w / total_weight for w in weights]
 
@@ -76,7 +76,15 @@
         for code, weight in zip(stock_codes, weights):
             # 从数据中获取该股票的20日收益率
             try:
-                stock_hist = stock_data[stock_data['stock_code'] == code].copy()
+                # 处理股票代码格式:sh601899 -> 601899 或 300000
+                code_num = code.replace('sh', '').replace('sz', '').replace('cn', '')
+                
+                # 尝试多种匹配方式
+                stock_hist = stock_data[
+                    (stock_data['stock_code'] == code) | 
+                    (stock_data['stock_code'] == code_num) |
+                    (stock_data['stock_code'].str.contains(code_num, na=False))
+                ].copy()
 
                 if len(stock_hist) < days:
                     # 如果数据不足,跳过
@@ -133,17 +141,17 @@
             VaR信息字典
         """
         if not portfolio.get('positions'):
-            return {'var': 0, 'status': '无持仓', 'level': 'info'}
+            return {'var': 0, 'status': '无持仓', 'level': 'info', 'value': 'N/A'}
 
         positions = portfolio['positions']
 
         # 获取持仓股票代码和权重
-        stock_codes = [pos['code'] for pos in positions]
-        weights = [pos.get('weight', 0) for pos in positions]
+        stock_codes = [pos['stock_code'] for pos in positions]
+        weights = [pos.get('current_value', 0) for pos in positions]
         total_weight = sum(weights)
 
         if total_weight == 0:
-            return {'var': 0, 'status': '空仓', 'level': 'info'}
+            return {'var': 0, 'status': '空仓', 'level': 'info', 'value': 'N/A'}
 
         weights = [w / total_weight for w in weights]
 
@@ -152,7 +160,14 @@
 
         for code, weight in zip(stock_codes, weights):
             try:
-                stock_hist = stock_data[stock_data['stock_code'] == code].copy()
+                # 处理股票代码格式
+                code_num = code.replace('sh', '').replace('sz', '').replace('cn', '')
+                
+                stock_hist = stock_data[
+                    (stock_data['stock_code'] == code) | 
+                    (stock_data['stock_code'] == code_num) |
+                    (stock_data['stock_code'].str.contains(code_num, na=False))
+                ].copy()
 
                 if len(stock_hist) < days:
                     continue
@@ -170,7 +185,7 @@
                 continue
 
         if not all_returns:
-            return {'var': 0, 'status': '数据不足', 'level': 'warning'}
+            return {'var': 0, 'status': '数据不足', 'level': 'warning', 'value': 'N/A', 'confidence': f'{confidence:.0%}'}
 
         # 计算组合收益率
         portfolio_returns = pd.Series(all_returns)
@@ -219,14 +234,14 @@
 
         # 统计行业权重
         industry_weights = {}
-        total_weight = sum(pos.get('weight', 0) for pos in positions)
+        total_weight = sum(pos.get('current_value', 0) for pos in positions)
 
         if total_weight == 0:
             return {'deviations': [], 'status': '空仓', 'level': 'info'}
 
         for pos in positions:
             industry = pos.get('industry', '其他')
-            weight = pos.get('weight', 0) / total_weight * 100
+            weight = pos.get('current_value', 0) / total_weight * 100
             industry_weights[industry] = industry_weights.get(industry, 0) + weight
 
         # 如果没有提供基准,使用等权重作为基准
@@ -288,7 +303,7 @@
             return {'concentrations': [], 'status': '无持仓', 'level': 'info'}
 
         positions = portfolio['positions']
-        total_weight = sum(pos.get('weight', 0) for pos in positions)
+        total_weight = sum(pos.get('current_value', 0) for pos in positions)
 
         if total_weight == 0:
             return {'concentrations': [], 'status': '空仓', 'level': 'info'}
@@ -297,7 +312,7 @@
         has_warning = False
 
         for pos in positions:
-            weight = pos.get('weight', 0) / total_weight * 100
+            weight = pos.get('current_value', 0) / total_weight * 100
 
             # 判断状态
             if weight > 15:
@@ -313,7 +328,7 @@
                 level = 'info'
 
             concentrations.append({
-                'code': pos.get('code', 'N/A'),
+                'code': pos.get('stock_code', 'N/A'),
                 'name': pos.get('name', 'N/A'),
                 'weight': round(weight, 1),
                 'status': status,
@@ -340,17 +355,17 @@
             最大回撤信息
         """
         if not portfolio.get('positions'):
-            return {'max_drawdown': 0, 'status': '无持仓', 'level': 'info'}
+            return {'max_drawdown': 0, 'status': '无持仓', 'level': 'info', 'value': 'N/A'}
 
         positions = portfolio['positions']
 
         # 获取持仓股票代码
-        stock_codes = [pos['code'] for pos in positions]
-        weights = [pos.get('weight', 0) for pos in positions]
+        stock_codes = [pos['stock_code'] for pos in positions]
+        weights = [pos.get('current_value', 0) for pos in positions]
         total_weight = sum(weights)
 
         if total_weight == 0:
-            return {'max_drawdown': 0, 'status': '空仓', 'level': 'info'}
+            return {'max_drawdown': 0, 'status': '空仓', 'level': 'info', 'value': 'N/A'}
 
         weights = [w / total_weight for w in weights]
 
@@ -359,30 +374,45 @@
 
         for code, weight in zip(stock_codes, weights):
             try:
-                stock_hist = stock_data[stock_data['stock_code'] == code].copy()
+                # 尝试多种匹配方式
+                code_num = code.replace('sh', '').replace('sz', '').replace('cn', '')
+                stock_hist = stock_data[
+                    (stock_data['stock_code'] == code) | 
+                    (stock_data['stock_code'] == code_num) |
+                    (stock_data['stock_code'].str.contains(code_num, na=False))
+                ].copy()
 
                 if len(stock_hist) == 0:
                     continue
 
                 stock_hist = stock_hist.sort_values('date_dt')
 
-                # 归一化价格
-                normalized_prices = stock_hist['close'] / stock_hist['close'].iloc[0]
+                # 检查close列是否存在且有值
+                if 'close' not in stock_hist.columns or stock_hist['close'].isnull().all():
+                    continue
+
+                # 归一化价格(避免除零错误)
+                first_close = stock_hist['close'].iloc[0]
+                if first_close == 0:
+                    continue
+                
+                normalized_prices = stock_hist['close'] / first_close
 
                 # 加权
                 weighted_values = normalized_prices * weight
+                weighted_values_list = weighted_values.tolist()
 
                 if len(portfolio_values) == 0:
-                    portfolio_values = weighted_values.tolist()
+                    portfolio_values = weighted_values_list
                 else:
                     # 对齐日期(简化处理)
-                    min_len = min(len(portfolio_values), len(weighted_values))
+                    min_len = min(len(portfolio_values), len(weighted_values_list))
                     portfolio_values = [
-                        portfolio_values[i] + weighted_values[i]
+                        portfolio_values[i] + weighted_values_list[i]
                         for i in range(min_len)
                     ]
             except Exception as e:
-                print(f"计算{code}净值失败: {e}")
+                print(f"计算{code}净值失败: {str(e)}")
                 continue
 
         if len(portfolio_values) < 2:
@@ -493,7 +523,8 @@
         lines.append('一、风险指标')
         risk = report['risk_indicators']
         lines.append(f"• 组合波动率:{risk['volatility']['value']}({risk['volatility']['status']})")
-        lines.append(f"• VaR({risk['var']['confidence']}):{risk['var']['value']}({risk['var']['status']})")
+        var_confidence = risk['var'].get('confidence', '95%')
+        lines.append(f"• VaR({var_confidence}):{risk['var']['value']}({risk['var']['status']})")
         drawdown_val = risk['max_drawdown'].get('value', '0%')
         drawdown_status = risk['max_drawdown'].get('status', 'N/A')
         lines.append(f"• 最大回撤:{drawdown_val}({drawdown_status})")
@@ -501,15 +532,23 @@
 
         # 行业偏离度
         lines.append('二、行业偏离度')
-        for dev in report['industry_deviation']['deviations'][:3]:
-            sign = '+' if dev['deviation'] > 0 else ''
-            lines.append(f"• {dev['industry']}:{sign}{dev['deviation']}%({dev['status']})")
+        deviations = report['industry_deviation'].get('deviations', [])
+        if deviations:
+            for dev in deviations[:3]:
+                sign = '+' if dev['deviation'] > 0 else ''
+                lines.append(f"• {dev['industry']}:{sign}{dev['deviation']}%({dev['status']})")
+        else:
+            lines.append(f"• {report['industry_deviation'].get('status', '无数据')}")
         lines.append('')
 
         # 个股集中度
         lines.append('三、个股集中度')
-        for conc in report['concentration']['concentrations'][:3]:
-            lines.append(f"• {conc['name']}:{conc['weight']}%({conc['status']})")
+        concentrations = report['concentration'].get('concentrations', [])
+        if concentrations:
+            for conc in concentrations[:3]:
+                lines.append(f"• {conc['name']}:{conc['weight']}%({conc['status']})")
+        else:
+            lines.append(f"• {report['concentration'].get('status', '无数据')}")
         lines.append('')
 
         return '\n'.join(lines)

--
Gitblit v1.8.0