1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#!/usr/bin/env python3
"""
A股量化日报 - 统一推送系统
功能:生成完整的交易协助推送(盘前和日报合并)
执行时机:工作日8:00(盘前推送)和18:30(日报推送)
"""
 
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'code'))
 
from datetime import datetime, timedelta
import logging
import json
import pickle
import pandas as pd
import numpy as np
import requests
 
# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('logs/unified_push.log'),
        logging.StreamHandler()
    ]
)
logger = logging.getLogger(__name__)
 
class UnifiedDailyPusher:
    """统一日报推送器"""
    
    def __init__(self):
        """初始化"""
        self.config = self._load_config()
        self.push_history_file = 'data/push_history.json'
        self.push_history = self._load_push_history()
        
    def _load_config(self):
        """加载配置"""
        config_file = 'config/feishu_config.json'
        if os.path.exists(config_file):
            with open(config_file, 'r') as f:
                return json.load(f)
        return {}
    
    def _load_push_history(self):
        """加载推送历史"""
        if os.path.exists(self.push_history_file):
            try:
                with open(self.push_history_file, 'r', encoding='utf-8') as f:
                    return json.load(f)
            except:
                return []
        return []
    
    def _save_push_history(self, push_data):
        """保存推送历史"""
        self.push_history.append(push_data)
        # 只保留最近30条
        if len(self.push_history) > 30:
            self.push_history = self.push_history[-30:]
        
        os.makedirs(os.path.dirname(self.push_history_file), exist_ok=True)
        with open(self.push_history_file, 'w', encoding='utf-8') as f:
            json.dump(self.push_history, f, ensure_ascii=False, indent=2)
    
    def _get_yesterday_push(self):
        """获取昨日推送"""
        if not self.push_history:
            return None
        
        today = datetime.now().strftime('%Y-%m-%d')
        # 找到最近一次非今日的推送
        for push in reversed(self.push_history):
            if push.get('date') != today:
                return push
        return None
    
    def _load_stock_data(self):
        """加载股票数据"""
        try:
            # 优先使用真实数据
            data_file = 'data/akshare_real_data_fixed.pkl'
            if os.path.exists(data_file):
                with open(data_file, 'rb') as f:
                    df = pickle.load(f)
                
                # 获取最新日期数据
                if 'date_dt' in df.columns:
                    latest_date = df['date_dt'].max()
                    latest_df = df[df['date_dt'] == latest_date].copy()
                else:
                    # 如果没有date_dt列,直接使用全部数据
                    latest_df = df.copy()
                
                # 过滤有效数据
                latest_df = latest_df[(latest_df['close'] > 0) & (latest_df['amount'] > 0)]
                
                # 过滤A股(sh和sz开头)
                if 'stock_code' in latest_df.columns:
                    a_stock_df = latest_df[latest_df['stock_code'].str[:2].isin(['sh', 'sz'])].copy()
                    return a_stock_df, latest_date if 'date_dt' in df.columns else datetime.now()
                
                return latest_df, latest_date if 'date_dt' in df.columns else datetime.now()
        except Exception as e:
            logger.error(f"加载数据失败: {e}")
            return None, None
    
    def _load_portfolio_state(self):
        """加载持仓状态"""
        portfolio_file = 'data/portfolio_state.json'
        if os.path.exists(portfolio_file):
            try:
                with open(portfolio_file, 'r', encoding='utf-8') as f:
                    return json.load(f)
            except:
                pass
        return None
    
    def _load_selection_result(self):
        """加载选股结果"""
        selection_file = 'data/selection_result.json'
        if os.path.exists(selection_file):
            try:
                with open(selection_file, 'r', encoding='utf-8') as f:
                    return json.load(f)
            except:
                pass
        return None
    
    def _load_rebalance_plan(self):
        """加载换仓计划"""
        rebalance_file = 'data/rebalance_plan.json'
        if os.path.exists(rebalance_file):
            try:
                with open(rebalance_file, 'r', encoding='utf-8') as f:
                    return json.load(f)
            except:
                pass
        return None
    
    def generate_push_content(self, push_type='morning'):
        """生成推送内容
        
        Args:
            push_type: 'morning'(盘前)或 'evening'(日报)
        """
        now = datetime.now()
        today = now.strftime('%Y-%m-%d')
        
        content = []
        
        # 标题
        if push_type == 'morning':
            content.append('🦞 A股量化日报 - 盘前推送')
        else:
            content.append('🦞 A股量化日报 - 日报推送')
        
        content.append('━━━━━━━━━━━━━━━━━━━━━━━━')
        content.append(f'📅 推送时间: {now.strftime("%Y-%m-%d %H:%M")}')
        content.append(f'📌 类型: 实盘推送(含持仓跟踪)')
        
        # 加载数据
        stock_data, data_date = self._load_stock_data()
        if stock_data is not None:
            if hasattr(data_date, 'strftime'):
                data_date_str = data_date.strftime('%Y-%m-%d')
            else:
                data_date_str = str(data_date)
            content.append(f'📊 数据日期: {data_date_str}')
            content.append(f'📊 覆盖股票: {len(stock_data)}只A股')
        
        content.append('')
        
        # 1. 昨日回顾
        content.append('🕐 昨日回顾')
        content.append('────────────────────')
        yesterday_push = self._get_yesterday_push()
        if yesterday_push:
            content.append(f'昨日推送: {yesterday_push.get("date", "N/A")}')
            content.append(f'持仓数量: {yesterday_push.get("positions_count", 0)}只')
            
            # 显示昨日决策
            yesterday_decision = yesterday_push.get('today_decision', [])
            if yesterday_decision:
                content.append('')
                content.append('昨日决策执行情况:')
                for decision in yesterday_decision[:5]:
                    emoji = "🟢" if decision.get('type') == '建仓' else ("🔴" if decision.get('type') in ['清仓', '止损'] else "🟡")
                    content.append(f'{emoji} {decision.get("name", "N/A")} - {decision.get("reason", "")}')
            else:
                content.append('昨日: 无调整建议,保持原持仓')
        else:
            content.append('状态: 无历史推送记录(首次推送)')
            content.append('建议: 按照今日选股结果建仓')
        
        content.append('')
        
        # 2. 今日持仓
        content.append('📊 今日持仓')
        content.append('────────────────────')
        portfolio = self._load_portfolio_state()
        if portfolio and 'positions' in portfolio:
            positions = portfolio['positions']
            if positions:
                content.append(f'持仓总数: {len(positions)}只')
                
                # 计算总资产
                total_value = sum(p.get('current_value', 0) for p in positions)
                total_cost = sum(p.get('cost_basis', 0) for p in positions)
                total_profit = total_value - total_cost
                profit_pct = (total_profit / total_cost * 100) if total_cost > 0 else 0
                
                content.append(f'总资产: {total_value:,.0f}元')
                content.append(f'总盈亏: {total_profit:,.0f}元 ({profit_pct:.2f}%)')
                content.append('')
                content.append('持仓明细:')
                for pos in positions[:5]:
                    emoji = "📈" if pos.get('profit_loss', 0) > 0 else ("📉" if pos.get('profit_loss', 0) < 0 else "➡️")
                    content.append(f'{emoji} {pos.get("name", "N/A")}({pos.get("code", "N/A")})')
                    content.append(f'   盈亏: {pos.get("profit_loss", 0):,.0f}元 ({pos.get("profit_loss_pct", 0):.2f}%)')
            else:
                content.append('当前无持仓')
                content.append('→ 建议按照今日选股结果建仓')
        else:
            content.append('当前无持仓')
            content.append('→ 建议按照今日选股结果建仓')
        
        content.append('')
        
        # 3. 今日选股
        content.append('🎯 今日选股')
        content.append('────────────────────')
        
        selection = self._load_selection_result()
        if selection and 'selected_stocks' in selection:
            selected_stocks = selection['selected_stocks']
            portfolio_config = selection.get('portfolio_config', {})
            
            content.append(f'选股结果: {len(selected_stocks)}只股票')
            content.append('')
            
            for idx, stock in enumerate(selected_stocks[:10], 1):
                code = stock.get('code', stock.get('stock_code', 'N/A'))
                name = stock.get('name', stock.get('stock_name', 'N/A'))
                alpha_score = stock.get('alpha_score', 0)
                
                # 判断是核心还是卫星
                is_core = False
                for core_stock in portfolio_config.get('core', []):
                    if core_stock.get('code') == code:
                        is_core = True
                        break
                
                tag = "🔑核心" if is_core else "🌟卫星"
                content.append(f'{idx}. {tag} {name}({code})')
                content.append(f'   α得分: {alpha_score:.1f}分')
        elif stock_data is not None and len(stock_data) > 0:
            # 如果没有选股结果,基于成交额排序
            content.append('基于市场活跃度推荐:')
            content.append('')
            
            # 按成交额排序,取前10只
            top_stocks = stock_data.nlargest(10, 'amount')
            
            # 保存选股结果到JSON(用于推送脚本读取)
            try:
                selection_data = {
                    "selected_stocks": [
                        {
                            "rank": idx + 1,
                            "stock_code": stock_code,
                            "stock_name": row.get('stock_name', 'N/A'),
                            "score": float(row.get('amount', 0) / 1e8),  # 使用成交额作为分数
                            "reasons": "基于市场活跃度推荐"
                        }
                        for idx, (stock_code, row) in enumerate(top_stocks.iterrows())
                    ],
                    "portfolio_config": {
                        "n": 10,
                        "rebalance_frequency": "daily",
                        "weighting_method": "equal_weight",
                        "score_threshold": 0,
                        "factor_weights": {}
                    },
                    "timestamp": datetime.now().isoformat(),
                    "data_month": "daily",
                    "control_summary": {
                        "industry": "行业分散: N/A",
                        "volatility": "未应用",
                        "drawdown": "未应用"
                    }
                }
                
                selection_file = 'data/selection_result.json'
                os.makedirs(os.path.dirname(selection_file), exist_ok=True)
                with open(selection_file, 'w', encoding='utf-8') as f:
                    json.dump(selection_data, f, ensure_ascii=False, indent=2)
                print(f"[DEBUG] 选股结果已保存到: {selection_file}")
            except Exception as e:
                print(f"[WARNING] 保存选股结果失败: {e}")
            
            for idx, (stock_code, row) in enumerate(top_stocks.iterrows(), 1):
                stock_name = row.get('stock_name', 'N/A')
                close = row.get('close', 0)
                change_pct = row.get('change_pct', 0)
                amount = row.get('amount', 0)
                
                emoji = "📈" if change_pct > 0 else ("📉" if change_pct < 0 else "➡️")
                content.append(f'{idx}. {stock_code} {stock_name}')
                content.append(f'   {emoji} 收盘: {close:.2f}元 | 涨跌: {change_pct:+.2f}% | 成交: {amount/1e8:.2f}亿')
        else:
            content.append('⚠️ 暂无选股数据')
        
        content.append('')
        
        # 4. 今日决策
        content.append('🔄 今日决策')
        content.append('────────────────────')
        
        rebalance = self._load_rebalance_plan()
        if rebalance and 'actions' in rebalance:
            actions = rebalance['actions']
            if actions:
                for action in actions[:5]:
                    action_type = action.get('action', '')
                    emoji = "🛑" if action_type == 'sell' else ("⬇️" if action_type == 'reduce' else ("⬆️" if action_type == 'add' else ("📈" if action_type == 'buy' else "👀")))
                    
                    action_name = {
                        'sell': '清仓',
                        'reduce': '减仓',
                        'add': '加仓',
                        'buy': '建仓',
                        'review': '复查'
                    }.get(action_type, action_type)
                    
                    content.append(f'{emoji} {action.get("name", "N/A")}({action.get("code", "N/A")}) - {action_name}')
                    content.append(f'   原因: {action.get("reason", "")}')
            else:
                content.append('✓ 无需调整,当前持仓符合策略')
        else:
            content.append('✓ 无需调整,建议按选股结果建仓')
            content.append('')
            content.append('操作建议:')
            content.append('• 分批建仓,避免一次性买入')
            content.append('• 每只股票占总仓位10-12%')
            content.append('• 保留20%现金应对加仓机会')
        
        content.append('')
        
        # 5. 换仓逻辑
        content.append('⚙️ 换仓逻辑')
        content.append('────────────────────')
        content.append('止盈触发: 收益>20% → 分批止盈')
        content.append('止损触发: 亏损>10% → 立即止损')
        content.append('时间触发: 持仓>60天 → 评估换仓')
        content.append('因子触发: α得分下降>20% → 建议换仓')
        
        content.append('')
        
        # 6. 明日计划
        content.append('📅 明日计划')
        content.append('────────────────────')
        content.append('• 监控今日建仓执行情况')
        content.append('• 如收盘前30分钟有调仓,及时跟进')
        content.append('• 如未触发调仓,保持当前持仓')
        content.append('• 继续监控止盈止损信号')
        
        content.append('')
        
        # 7. 仓位管理
        content.append('💰 仓位管理建议')
        content.append('────────────────────')
        content.append('• 总仓位: 80%(核心60%+卫星20%)')
        content.append('• 现金: 20%(应对加仓)')
        content.append('• 单股最大仓位: 12%')
        content.append('• 组合最大回撤: -15%风险线')
        
        content.append('')
        
        # 8. 风控规则
        content.append('⚠️ 风控规则')
        content.append('────────────────────')
        content.append('• 单股止损线: -10%')
        content.append('• 单股止盈线: +20%')
        content.append('• 组合最大回撤: -15%')
        content.append('• 根据自身风险承受能力调整')
        
        content.append('')
        
        content.append('━━━━━━━━━━━━━━━━━━━━━━━━')
        content.append('📊 数据来源: AKShare A股实时数据')
        content.append(f'🦞 A股量化系统 v2.0 | {today}')
        
        push_content = '\n'.join(content)
        
        # 保存推送历史
        self._save_push_history({
            'date': today,
            'time': now.strftime('%H:%M:%S'),
            'type': push_type,
            'positions_count': len(portfolio['positions']) if portfolio and 'positions' in portfolio else 0,
            'content': push_content[:500]  # 只保存前500字符
        })
        
        return push_content
    
    def send_feishu_push(self, report):
        """发送飞书推送"""
        webhook_url = self.config.get('webhook_url')
        if not webhook_url:
            logger.warning("未配置webhook_url")
            return False
        
        payload = {
            "msg_type": "text",
            "content": {
                "text": report
            }
        }
        
        try:
            response = requests.post(webhook_url, json=payload, timeout=10)
            if response.status_code == 200:
                logger.info("✓ 飞书推送成功")
                return True
            else:
                logger.error(f"飞书推送失败: {response.status_code} - {response.text}")
                return False
        except Exception as e:
            logger.error(f"发送推送失败: {e}", exc_info=True)
            return False
    
    def run(self, push_type='morning'):
        """执行推送"""
        logger.info("="*60)
        logger.info(f"A股量化日报推送开始 - {push_type}")
        logger.info("="*60)
        
        # 检查交易日
        now = datetime.now()
        if now.weekday() >= 5:
            logger.info(f"⚠️ 今天是周末,跳过推送")
            return 0
        
        logger.info(f"✓ 交易日")
        
        # 生成报告
        logger.info("生成推送报告...")
        report = self.generate_push_content(push_type)
        
        if report:
            logger.info("✓ 报告生成成功")
            
            # 保存报告
            os.makedirs('reports', exist_ok=True)
            report_file = f"reports/{push_type}_push_{now.strftime('%Y%m%d_%H%M')}.md"
            with open(report_file, 'w', encoding='utf-8') as f:
                f.write(report)
            logger.info(f"✓ 报告已保存: {report_file}")
            
            # 发送推送
            if self.send_feishu_push(report):
                logger.info("="*60)
                logger.info("✅ A股量化日报推送完成")
                logger.info("="*60)
                return 0
            else:
                logger.error("✗ 推送失败")
                return 1
        else:
            logger.error("✗ 报告生成失败")
            return 1
 
def main():
    """主函数"""
    import argparse
    
    parser = argparse.ArgumentParser(description='A股量化日报统一推送系统')
    parser.add_argument('--type', choices=['morning', 'evening'], default='morning',
                       help='推送类型: morning(盘前), evening(日报)')
    
    args = parser.parse_args()
    
    pusher = UnifiedDailyPusher()
    return pusher.run(args.type)
 
if __name__ == "__main__":
    sys.exit(main())