From a497eed56de36ffc9ceb0908b09800e2f5b6598a Mon Sep 17 00:00:00 2001
From: Variya <variya@VariyadeMacBook-Air.local>
Date: Wed, 11 Mar 2026 13:58:24 +0800
Subject: [PATCH] 更新策略系统和数据处理脚本

---
 a_stock_manager.py |  237 +++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 213 insertions(+), 24 deletions(-)

diff --git a/a_stock_manager.py b/a_stock_manager.py
index 6d29944..fbd4521 100644
--- a/a_stock_manager.py
+++ b/a_stock_manager.py
@@ -213,6 +213,62 @@
         print_error(f"执行失败: {e}")
         return False
 
+def run_enhanced_factor_system(data_file=None):
+    """运行增强因子系统"""
+    print_info("正在运行增强因子系统...")
+    
+    if data_file is None:
+        data_file = project_root / 'data' / 'akshare_real_data_fixed.pkl'
+    
+    try:
+        import pickle
+        import pandas as pd
+        
+        if not Path(data_file).exists():
+            print_error(f"数据文件不存在: {data_file}")
+            return False
+        
+        with open(data_file, 'rb') as f:
+            df = pickle.load(f)
+        
+        print_info(f"加载数据: {len(df)} 条记录")
+        
+        from scripts.enhanced_factor_system import EnhancedFactorSystem
+        
+        system = EnhancedFactorSystem()
+        
+        df = system.calculate_target_returns(df)
+        df = system.calculate_quality_factors(df)
+        df = system.calculate_growth_factors(df)
+        df = system.calculate_sentiment_factors(df)
+        df = system.calculate_cross_sectional_factors(df)
+        
+        model, df, ml_metrics = system.train_ml_model(df, target='return_5d')
+        results, df = system.evaluate_all_factors(df, target='return_5d')
+        best_factors = system.select_best_factors(results)
+        df = system.calculate_final_score(df, best_factors, results)
+        
+        with open(data_file, 'wb') as f:
+            pickle.dump(df, f)
+        
+        effective_count = sum(1 for r in results.values() if r.get('effective', False))
+        print_success(f"增强因子系统完成,有效因子: {effective_count}/{len(results)}")
+        
+        if ml_metrics and ml_metrics.get('ir', 0) > 0.3:
+            print_success(f"ML模型IR: {ml_metrics['ir']:.2f}")
+        
+        return True
+        
+    except ImportError as e:
+        print_warning(f"增强因子系统模块不可用: {e}")
+        print_info("跳过因子计算,使用原始数据")
+        return False
+    except Exception as e:
+        print_error(f"增强因子系统执行失败: {e}")
+        import traceback
+        print_error(traceback.format_exc())
+        return False
+
 # ==================== 一级菜单:数据工程 ====================
 
 def data_engineering_menu():
@@ -262,22 +318,38 @@
     print("  数据更新用于获取最新的股票市场数据")
     print("  • 支持多种数据源:AKShare、BaoStock")
     print("  • 自动计算技术因子指标")
+    print("  • 自动运行增强因子系统")
     print("  • 支持增量更新")
     
     print("\n选择数据更新方式:")
     print("1. 使用真实数据源(AKShare/BaoStock)⭐推荐")
     print("2. 使用本地模拟数据(仅测试用)")
     print("3. 从AKShare在线获取(需要网络)")
+    print("4. 仅重新计算因子(不更新数据)⭐新增")
     print("0. 返回")
     
-    choice = input("\n请选择 (0-3): ").strip()
+    choice = input("\n请选择 (0-4): ").strip()
     
     if choice == '1':
-        return fetch_real_data()
+        result = fetch_real_data()
+        if result:
+            print_info("数据更新完成,正在运行增强因子系统...")
+            run_enhanced_factor_system()
+        return result
     elif choice == '2':
-        return run_script('data_update_v3.py', "使用本地模拟数据")
+        result = run_script('data_update_v3.py', "使用本地模拟数据")
+        if result:
+            print_info("数据更新完成,正在运行增强因子系统...")
+            run_enhanced_factor_system()
+        return result
     elif choice == '3':
-        return run_script('data_update_v2.py', "从AKShare在线获取数据")
+        result = run_script('data_update_v2.py', "从AKShare在线获取数据")
+        if result:
+            print_info("数据更新完成,正在运行增强因子系统...")
+            run_enhanced_factor_system()
+        return result
+    elif choice == '4':
+        return run_enhanced_factor_system()
 
 def fetch_real_data():
     """获取真实数据"""
@@ -983,7 +1055,8 @@
     try:
         from code.strategy.rdagent_interface import RDAgentFactorInterface, is_rdagent_available
         
-        status = "已安装" if is_rdagent_available() else "未安装(使用本地方法)"
+        rdagent_installed = is_rdagent_available()
+        status = f"{Color.OKGREEN}已安装{Color.ENDC}" if rdagent_installed else f"{Color.WARNING}未安装{Color.ENDC}(使用本地方法)"
         print(f"\n{Color.BOLD}【系统状态】{Color.ENDC}")
         print(f"  RDAgent状态: {status}")
         
@@ -991,9 +1064,13 @@
         print("  1. 自动发现新因子")
         print("  2. 查看已发现因子")
         print("  3. 生成因子报告")
+        if not rdagent_installed:
+            print(f"  4. {Color.OKCYAN}安装 RDAgent ⭐推荐{Color.ENDC}")
+        else:
+            print("  4. 更新 RDAgent")
         print("  0. 返回")
         
-        choice = input("\n请选择 (0-3): ").strip()
+        choice = input("\n请选择 (0-4): ").strip()
         
         if choice == '1':
             print_info("开始自动发现因子...")
@@ -1073,6 +1150,9 @@
                     f.write(report)
                 print_success(f"报告已保存: {report_file}")
         
+        elif choice == '4':
+            install_rdagent()
+            
     except ImportError as e:
         print_error(f"导入RDAgent接口失败: {e}")
         print_info("请确保 code/strategy/rdagent_interface.py 存在")
@@ -1080,6 +1160,104 @@
         print_error(f"RDAgent因子挖掘失败: {e}")
         import traceback
         print_error(traceback.format_exc())
+
+def install_rdagent():
+    """安装 RDAgent"""
+    print_header("RDAgent 安装向导")
+    
+    print(f"\n{Color.BOLD}【安装方式】{Color.ENDC}")
+    print("  1. 快速安装 (pip install rdagent) ⭐推荐")
+    print("  2. 源码安装 (从 GitHub 克隆)")
+    print("  3. 查看安装说明")
+    print("  0. 返回")
+    
+    choice = input("\n请选择 (0-3): ").strip()
+    
+    if choice == '1':
+        print_info("\n开始安装 RDAgent...")
+        print(f"\n{Color.BOLD}安装命令:{Color.ENDC}")
+        print("  pip install rdagent")
+        
+        confirm = input("\n是否立即执行安装? (y/n): ").strip().lower()
+        if confirm == 'y':
+            import subprocess
+            print_info("正在安装,请稍候...")
+            try:
+                result = subprocess.run(
+                    ['pip', 'install', 'rdagent'],
+                    capture_output=True,
+                    text=True,
+                    timeout=300
+                )
+                if result.returncode == 0:
+                    print_success("RDAgent 安装成功!")
+                    print_info("\n下一步:配置 API Key")
+                    print("  1. 创建 .env 文件")
+                    print("  2. 添加: OPENAI_API_KEY=your_api_key")
+                    print("  3. 或使用国内中转 API")
+                else:
+                    print_error(f"安装失败: {result.stderr}")
+                    print_info("请尝试使用源码安装")
+            except subprocess.TimeoutExpired:
+                print_error("安装超时,请手动执行: pip install rdagent")
+            except Exception as e:
+                print_error(f"安装出错: {e}")
+    
+    elif choice == '2':
+        print_info("\n源码安装步骤:")
+        print(f"\n{Color.BOLD}1. 克隆仓库{Color.ENDC}")
+        print("  git clone https://github.com/microsoft/RD-Agent.git")
+        print("  cd RD-Agent")
+        
+        print(f"\n{Color.BOLD}2. 创建虚拟环境{Color.ENDC}")
+        print("  conda create -n rdagent python=3.10")
+        print("  conda activate rdagent")
+        
+        print(f"\n{Color.BOLD}3. 安装依赖{Color.ENDC}")
+        print("  pip install -e .")
+        
+        print(f"\n{Color.BOLD}4. 配置 API Key{Color.ENDC}")
+        print("  创建 .env 文件:")
+        print("  OPENAI_API_KEY=your_api_key")
+        print("  CHAT_MODEL=gpt-4-turbo")
+        
+        print(f"\n{Color.BOLD}5. 验证安装{Color.ENDC}")
+        print("  rdagent --version")
+        
+        print(f"\n{Color.WARNING}注意:{Color.ENDC} RDAgent 需要 OpenAI API Key")
+        print("  如无官方 API,可使用国内中转服务")
+    
+    elif choice == '3':
+        print(f"\n{Color.BOLD}【RDAgent 安装说明】{Color.ENDC}")
+        print(f"\n{Color.BOLD}系统要求:{Color.ENDC}")
+        print("  - Python 3.10+")
+        print("  - pip 或 conda")
+        print("  - OpenAI API Key (或兼容的 API)")
+        
+        print(f"\n{Color.BOLD}快速安装:{Color.ENDC}")
+        print("  pip install rdagent")
+        
+        print(f"\n{Color.BOLD}配置 API:{Color.ENDC}")
+        print("  创建 .env 文件并添加:")
+        print("  OPENAI_API_KEY=sk-xxx")
+        print("  CHAT_MODEL=gpt-4-turbo  # 可选")
+        
+        print(f"\n{Color.BOLD}国内用户:{Color.ENDC}")
+        print("  可使用 OpenAI 中转 API")
+        print("  在 .env 中添加:")
+        print("  OPENAI_API_BASE=https://your-proxy.com/v1")
+        
+        print(f"\n{Color.BOLD}官方文档:{Color.ENDC}")
+        print("  https://github.com/microsoft/RD-Agent")
+        
+        print(f"\n{Color.BOLD}当前状态:{Color.ENDC}")
+        try:
+            import importlib
+            importlib.import_module('rdagent')
+            print(f"  {Color.OKGREEN}✓ RDAgent 已安装{Color.ENDC}")
+        except ImportError:
+            print(f"  {Color.FAIL}✗ RDAgent 未安装{Color.ENDC}")
+            print(f"  {Color.OKCYAN}→ 选择选项1或2进行安装{Color.ENDC}")
 
 # ==================== 一级菜单:策略开发 ====================
 
@@ -1498,7 +1676,7 @@
     print_info("功能:分析策略在不同时间窗口的表现")
     
     try:
-        from code.backtest.rolling_performance import RollingPerformanceAnalyzer
+        from code.backtest.rolling_performance import RollingPerformanceAnalyzer, RollingConfig
         
         print("\n滚动分析指标:")
         print("  • 滚动收益率")
@@ -1516,7 +1694,8 @@
         
         if choice == '1':
             print_info("运行滚动性能分析...")
-            analyzer = RollingPerformanceAnalyzer(window=252)
+            config = RollingConfig(window=252)
+            analyzer = RollingPerformanceAnalyzer(config=config)
             print_info("需要准备净值序列数据")
         elif choice == '2':
             print_info("查看分析结果...")
@@ -1548,7 +1727,8 @@
         
         if choice == '1':
             print_info("运行压力测试...")
-            tester = StressTester()
+            data_path = project_root / 'data' / 'akshare_real_data_fixed.pkl'
+            tester = StressTester(str(data_path))
             print_info("需要准备当前持仓数据")
         elif choice == '2':
             print_info("查看测试结果...")
@@ -1580,7 +1760,8 @@
         
         if choice == '1':
             print_info("运行过拟合检测...")
-            detector = OverfittingDetector()
+            data_path = project_root / 'data' / 'akshare_real_data_fixed.pkl'
+            detector = OverfittingDetector(str(data_path))
             print_info("需要准备策略回测结果")
         elif choice == '2':
             print_info("查看检测结果...")
@@ -1705,7 +1886,7 @@
     print_header("推送系统")
     
     print(f"\n{Color.BOLD}【功能说明】{Color.ENDC}")
-    print("  统一的推送管理入口,支持盘前、日报、模拟交易等多种推送类型")
+    print("  统一的推送管理入口,使用 daily_master.py 作为主控脚本")
     print("  • 盘前推送:工作日8:00,包含选股建议、因子评估、持仓状态")
     print("  • 日报推送:工作日18:30,包含当日表现、风险分析、市场总结")
     print("  • 模拟交易推送:实时推送模拟交易信号")
@@ -1715,17 +1896,16 @@
     print("2. 日报推送(工作日18:30)⭐推荐")
     print("3. 模拟交易推送")
     print("4. 推送监控系统")
-    print("5. 启动早盘推送守护进程(后台运行)")
     print("0. 返回")
     
-    choice = input("\n请选择 (0-5): ").strip()
+    choice = input("\n请选择 (0-4): ").strip()
     
     if choice == '1':
         print_info("执行盘前推送...")
-        script_path = scripts_dir / 'unified_daily_push.py'
+        script_path = scripts_dir / 'daily_master.py'
         try:
             result = subprocess.run(
-                [sys.executable, str(script_path), '--type', 'morning'],
+                [sys.executable, str(script_path)],
                 cwd=project_root,
                 capture_output=True,
                 text=True
@@ -1741,10 +1921,10 @@
             
     elif choice == '2':
         print_info("执行日报推送...")
-        script_path = scripts_dir / 'unified_daily_push.py'
+        script_path = scripts_dir / 'daily_master.py'
         try:
             result = subprocess.run(
-                [sys.executable, str(script_path), '--type', 'evening'],
+                [sys.executable, str(script_path)],
                 cwd=project_root,
                 capture_output=True,
                 text=True
@@ -1763,11 +1943,6 @@
         
     elif choice == '4':
         return run_script('push_monitor.py', "推送监控系统")
-        
-    elif choice == '5':
-        print_warning("注意:守护进程将在后台持续运行")
-        print_info("启动早盘推送守护进程...")
-        return run_script('morning_push_daemon.py', "早盘推送守护进程")
 
 def position_management():
     """持仓管理"""
@@ -3151,6 +3326,9 @@
                             pickle.dump(cleaned_data, f)
                         print_success(f"修复完成,已保存到: {output_path}")
                         
+                        print_info("正在重新计算增强因子...")
+                        run_enhanced_factor_system(output_path)
+                        
                     elif fix_choice == '2':
                         print_info("详细问题列表:")
                         report_file = project_root / 'data' / 'akshare_real_data_quality_report.json'
@@ -3164,7 +3342,11 @@
                             print_warning("未找到详细报告文件")
                             
                     elif fix_choice == '3':
-                        return run_script('fix_data_quality_v2.py', "数据质量修复")
+                        result = run_script('fix_data_quality_v2.py', "数据质量修复")
+                        if result:
+                            print_info("正在重新计算增强因子...")
+                            run_enhanced_factor_system()
+                        return result
             else:
                 print_error(f"数据文件不存在: {data_file}")
                 
@@ -3212,8 +3394,15 @@
                         with open(output_path, 'wb') as f:
                             pickle.dump(cleaned_data, f)
                         print_success(f"清洗完成,已保存到: {output_path}")
+                        
+                        print_info("正在重新计算增强因子...")
+                        run_enhanced_factor_system(output_path)
                     elif fix_choice == '3':
-                        return run_script('fix_data_quality_v2.py', "数据质量修复")
+                        result = run_script('fix_data_quality_v2.py', "数据质量修复")
+                        if result:
+                            print_info("正在重新计算增强因子...")
+                            run_enhanced_factor_system()
+                        return result
             else:
                 print_error(f"数据文件不存在: {data_file}")
                 

--
Gitblit v1.8.0