| | |
| | | |
| | | def generate_daily_recommendation(run_date: datetime, factor_data: pd.DataFrame, |
| | | score_model, stock_selector, risk_controller, |
| | | n: int = 10) -> str: |
| | | n: int = 10, market_data: Dict = None) -> str: |
| | | """ |
| | | 生成每日推荐报告 |
| | | |
| | |
| | | stock_selector: 选股器 |
| | | risk_controller: 风险控制器 |
| | | n: 选股数量 |
| | | market_data: 市场数据 |
| | | |
| | | Returns: |
| | | Markdown格式的报告 |
| | |
| | | lines.append(f"**数据日期**: {latest_date.strftime('%Y-%m-%d')}") |
| | | lines.append("") |
| | | |
| | | # 获取因子权重信息 |
| | | lines.append("## 因子权重") |
| | | # 市场概览 |
| | | if market_data: |
| | | lines.append("## 📊 市场概览") |
| | | lines.append("") |
| | | lines.append("| 因子 | 权重 |") |
| | | lines.append("|------|------|") |
| | | lines.append("| 指数 | 最新价 | 涨跌幅 |") |
| | | lines.append("|------|--------|--------|") |
| | | for idx, data in market_data.items(): |
| | | lines.append(f"| {data['name']} | {data['price']} | {data['change_pct']:.2f}% |") |
| | | lines.append("") |
| | | |
| | | # 获取因子权重信息 |
| | | lines.append("## 🎯 因子权重") |
| | | lines.append("") |
| | | lines.append("| 因子 | 权重 | IC值 |") |
| | | lines.append("|------|------|------|") |
| | | |
| | | total_weighted = 0 |
| | | for factor, weight in sorted(score_model.factor_weights.items(), key=lambda x: -x[1]): |
| | | ic = score_model.factor_ic.get(factor, 0) |
| | | if not pd.isna(weight): |
| | | lines.append(f"| {factor} | {weight:.2%} (IC={ic:.3f}) |") |
| | | lines.append(f"| {factor} | {weight:.2%} | {ic:.3f} |") |
| | | total_weighted += weight |
| | | |
| | | lines.append("") |
| | |
| | | lines.append("") |
| | | |
| | | # 生成选股结果 |
| | | lines.append("## 选股结果") |
| | | lines.append("## 📈 选股结果") |
| | | lines.append("") |
| | | |
| | | if stock_selector.selected_stocks is not None: |
| | |
| | | pb_col = 'PB' if 'PB' in selected.columns else ('pb' if 'pb' in selected.columns else None) |
| | | cap_col = '市值_亿' if '市值_亿' in selected.columns else ('market_cap' if 'market_cap' in selected.columns else None) |
| | | name_col = '股票名称' if '股票名称' in selected.columns else ('stock_name' if 'stock_name' in selected.columns else None) |
| | | industry_col = 'industry' if 'industry' in selected.columns else ('行业' if '行业' in selected.columns else None) |
| | | |
| | | lines.append("| 排名 | 股票代码 | 股票名称 | 综合得分 | PE-TTM | PB | 市值(亿) |") |
| | | lines.append("|------|----------|----------|----------|--------|-----|---------|") |
| | | lines.append("| 排名 | 股票代码 | 股票名称 | 行业 | 综合得分 | PE-TTM | PB | 市值(亿) |") |
| | | lines.append("|------|----------|----------|------|----------|--------|-----|---------|") |
| | | |
| | | for idx, (stock_code, stock_data) in enumerate(selected.iterrows(), 1): |
| | | score = stock_data.get('综合得分', 0) |
| | |
| | | pb = stock_data.get(pb_col, 0) if pb_col else 0 |
| | | market_cap = stock_data.get(cap_col, 0) if cap_col else 0 |
| | | stock_name = stock_data.get(name_col, f'股票{stock_code}') if name_col else f'股票{stock_code}' |
| | | industry = stock_data.get(industry_col, '未知') if industry_col else '未知' |
| | | |
| | | lines.append(f"| {idx} | {stock_code} | {stock_name} | **{score:.2f}** | {pe_ttm:.2f} | {pb:.2f} | {market_cap:.2f} |") |
| | | lines.append(f"| {idx} | {stock_code} | {stock_name} | {industry} | **{score:.2f}** | {pe_ttm:.2f} | {pb:.2f} | {market_cap:.2f} |") |
| | | |
| | | lines.append("") |
| | | else: |
| | | lines.append("*暂无选股结果*") |
| | | lines.append("") |
| | | |
| | | # 行业暴露分析 |
| | | lines.append("## 🔍 行业暴露分析") |
| | | lines.append("") |
| | | if hasattr(stock_selector, '_analyze_industry_exposure') and stock_selector.selected_stocks is not None: |
| | | # 模拟行业暴露分析 |
| | | industry_counts = {} |
| | | for _, row in stock_selector.selected_stocks.head(n).iterrows(): |
| | | industry = row.get('industry', row.get('行业', '未知')) |
| | | industry_counts[industry] = industry_counts.get(industry, 0) + 1 |
| | | |
| | | if industry_counts: |
| | | lines.append("| 行业 | 数量 | 占比 |") |
| | | lines.append("|------|------|------|") |
| | | total = sum(industry_counts.values()) |
| | | for industry, count in sorted(industry_counts.items(), key=lambda x: -x[1]): |
| | | percentage = (count / total) * 100 |
| | | lines.append(f"| {industry} | {count} | {percentage:.1f}% |") |
| | | lines.append("") |
| | | else: |
| | | lines.append("*暂无行业数据*") |
| | | lines.append("") |
| | | |
| | | # 风险控制摘要 |
| | | if risk_controller.control_summary: |
| | | lines.append("## 风险控制") |
| | | if risk_controller and risk_controller.control_summary: |
| | | lines.append("## 🛡️ 风险控制") |
| | | lines.append("") |
| | | |
| | | # 格式化控制摘要 |
| | |
| | | for line in summary_text.split('\n'): |
| | | lines.append(f"{line}") |
| | | |
| | | lines.append("") |
| | | |
| | | # 投资建议 |
| | | lines.append("## 💡 投资建议") |
| | | lines.append("") |
| | | lines.append("### 建仓策略:") |
| | | lines.append("- 分批建仓:9:30-11:30(分散执行)") |
| | | lines.append("- 第一批(30%): 9:30-10:00") |
| | | lines.append("- 第二批(30%): 10:00-10:30") |
| | | lines.append("- 第三批(40%): 10:30-11:30") |
| | | lines.append("") |
| | | lines.append("### 止盈止损:") |
| | | lines.append("- 动态止盈:基于波动率和市场环境调整") |
| | | lines.append("- 动态止损:基于波动率和市场环境调整") |
| | | lines.append("- 持仓时间:最长60天") |
| | | lines.append("") |
| | | lines.append("### 风险控制:") |
| | | lines.append("- 单只股票最大持仓:12%") |
| | | lines.append("- 单个行业最大持仓:30%") |
| | | lines.append("- 组合最大回撤:15%") |
| | | lines.append("") |
| | | |
| | | # 免责声明 |
| | |
| | | |
| | | lines.append("---") |
| | | lines.append("") |
| | | lines.append(f"*Generated by A股量化系统 at {run_date.strftime('%Y-%m-%d %H:%M:%S')}*") |
| | | lines.append(f"*Generated by A股量化系统 v2.1 at {run_date.strftime('%Y-%m-%d %H:%M:%S')}*") |
| | | |
| | | return "\n".join(lines) |
| | | |