File was renamed from strategy/index_market_trend_strategy.py |
| | |
| | | # 获取logger实例 |
| | | logger = logger_common |
| | | |
| | | # ====================== |
| | | # 模拟数据生成(完整版) |
| | | # ====================== |
| | | # data = { |
| | | # 'total_stocks': 5000, # 全市场股票总数 |
| | | # 'limit_up': 120, # 涨停股数量 |
| | | # 'limit_down': 5, # 跌停股数量 |
| | | # 'advance_count': 2800, # 上涨家数 |
| | | # 'decline_count': 1500, # 下跌家数 |
| | | # 'high_retreat_count': 30, # 当日回撤超10%的个股数量 |
| | | # 'turnover': 1.2e12, # 当日成交额(元) |
| | | # 'turnover_prev_day': 1.0e12, # 前一日成交额 |
| | | # 'northbound_inflow': 8e9, # 北向资金净流入(元) |
| | | # 'max_northbound_30d': 15e9, # 近30日最大北向净流入 |
| | | # 'margin_buy': 8e10, # 融资买入额 |
| | | # 'sector_gains': [3.5, 2.8, 1.9, -0.5], # 前4大板块涨幅(%) |
| | | # 'max_continuous_boards': 7, # 最高连板数(如7连板) |
| | | # 'continuous_boards': [7, 5, 3, 2], # 连板梯队(各层级连板数量) |
| | | # } |
| | | # |
| | | # |
| | | # # ====================== |
| | | # # 因子计算与标准化处理 |
| | | # # ====================== |
| | | # def calculate_factors(data): |
| | | # factors = {} |
| | | # |
| | | # # === 新增核心因子 === |
| | | # # 1. 涨跌家数比(0-100分) |
| | | # if data['decline_count'] == 0: |
| | | # factors['advance_ratio'] = 100.0 |
| | | # else: |
| | | # factors['advance_ratio'] = min( |
| | | # (data['advance_count'] / data['decline_count']) * 50, # 比值1:2对应100分 |
| | | # 100.0 |
| | | # ) |
| | | # |
| | | # # 2. 涨停强度(标准化到0-100) |
| | | # factors['limit_strength'] = ( |
| | | # (data['limit_up'] - data['limit_down']) / data['total_stocks'] * 1000 # 放大差异 |
| | | # ) |
| | | # |
| | | # # 3. 大幅回撤比例(0-100分) |
| | | # factors['high_retreat_ratio'] = ( |
| | | # data['high_retreat_count'] / data['total_stocks'] * 1000 # 千分比更敏感 |
| | | # ) |
| | | # |
| | | # # 4. 连板高度与梯队(根据历史极值归一化) |
| | | # factors['max_continuous'] = ( |
| | | # data['max_continuous_boards'] / 10 * 100 # 假设历史最高10连板 |
| | | # ) |
| | | # factors['board_ladder'] = ( |
| | | # len(data['continuous_boards']) / 5 * 100 # 假设最多5个连板层级 |
| | | # ) |
| | | # |
| | | # # === 原有因子优化 === |
| | | # # 5. 量能变化(成交额增长率) |
| | | # turnover_growth = ( |
| | | # (data['turnover'] - data['turnover_prev_day']) / |
| | | # data['turnover_prev_day'] * 100 |
| | | # ) |
| | | # factors['liquidity'] = turnover_growth |
| | | # |
| | | # # 6. 板块强度(前3板块平均涨幅) |
| | | # top_sectors = sorted(data['sector_gains'], reverse=True)[:3] |
| | | # sector_avg = sum(top_sectors) / len(top_sectors) |
| | | # |
| | | # factors['sector_strength'] = ((sector_avg - (-5.0)) / (10.0 - (-5.0)) * 100) # 历史范围-5%~10% |
| | | # # 7. 北向资金强度 |
| | | # factors['northbound_ratio'] = (data['northbound_inflow'] / data['max_northbound_30d'] * 100) |
| | | # |
| | | # # 8. 融资买入占比 |
| | | # factors['margin_ratio'] = (data['margin_buy'] / data['turnover'] * 100) |
| | | # |
| | | # # # 强制所有因子在0-100范围内 |
| | | # for key in factors: |
| | | # factors[key] = max(0.0, min(factors[key], 100.0)) |
| | | # |
| | | # return factors |
| | | # |
| | | # |
| | | # # ====================== |
| | | # # 权重分配(总权重1.0) |
| | | # # ====================== |
| | | # def get_weights(): |
| | | # return { |
| | | # # 新增因子权重 |
| | | # 'advance_ratio': 0.15, # 涨跌家数比 |
| | | # 'limit_strength': 0.2, # 涨停强度 |
| | | # 'high_retreat_ratio': 0.1, # 大幅回撤 |
| | | # 'max_continuous': 0.1, # 连板高度 |
| | | # 'board_ladder': 0.05, # 连板梯队 |
| | | # # 原有因子权重 |
| | | # 'liquidity': 0.15, # 量能变化 |
| | | # 'sector_strength': 0.1, # 板块强度 |
| | | # 'northbound_ratio': 0.1, # 北向资金 |
| | | # 'margin_ratio': 0.05 # 融资买入 |
| | | # } |
| | | # |
| | | # |
| | | # # ====================== |
| | | # # 综合强度分数计算(含动态修正) |
| | | # # ====================== |
| | | # def composite_strength_score(data): |
| | | # factors = calculate_factors(data) |
| | | # weights = get_weights() |
| | | # |
| | | # # 基础加权得分 |
| | | # score = sum(factors[key] * weights[key] for key in factors) |
| | | # |
| | | # # === 动态修正规则 === |
| | | # # 规则1:涨停数量超过100家时额外加分 |
| | | # if data['limit_up'] > 100: |
| | | # score += min((data['limit_up'] - 100) * 0.2, 10) # 最多加10分 |
| | | # |
| | | # # 规则2:连板梯队断裂时扣分(如最高板与次高板差距≥3) |
| | | # continuous_boards = sorted(data['continuous_boards'], reverse=True) |
| | | # if len(continuous_boards) >= 2 and (continuous_boards[0] - continuous_boards[1] >= 3): |
| | | # score -= 15 # 梯队断裂惩罚 |
| | | # |
| | | # # 规则3:北向资金与涨跌家数背离时调整 |
| | | # if (factors['northbound_ratio'] > 50) and (factors['advance_ratio'] < 40): |
| | | # score *= 0.9 # 权重股拉升导致的虚假繁荣 |
| | | # |
| | | # return max(0.0, min(score, 100.0)) |
| | | |
| | | |
| | | # ====================== |
| | | # 执行计算与结果输出 |
| | | # ====================== |
| | | # final_score = composite_strength_score(data) |
| | | # print("=== 综合强度分数 ===") |
| | | # print(f"当前得分: {final_score:.1f}/100") |
| | | # |
| | | # # 输出因子贡献度分析 |
| | | # factors = calculate_factors(data) |
| | | # weights = get_weights() |
| | | # print("\n=== 因子贡献度明细 ===") |
| | | # for key in factors: |
| | | # print(f"{key:20s}: {factors[key]:5.1f} × {weights[key]:.0%} = {factors[key] * weights[key]:5.1f}") |
| | | |
| | | ''' 代码输出示例: |
| | | === 综合强度分数 === |
| | | 当前得分: 78.4/100 |
| | | |
| | | === 因子贡献度明细 === |
| | | advance_ratio : 93.3 × 15% = 14.0 |
| | | limit_strength : 23.0 × 20% = 4.6 |
| | | high_retreat_ratio : 6.0 × 10% = 0.6 |
| | | max_continuous : 70.0 × 10% = 7.0 |
| | | board_ladder : 80.0 × 5% = 4.0 |
| | | liquidity : 20.0 × 15% = 3.0 |
| | | sector_strength : 60.0 × 10% = 6.0 |
| | | northbound_ratio : 53.3 × 10% = 5.3 |
| | | margin_ratio : 10.0 × 5% = 0.5 |
| | | ''' |
| | | |
| | | |
| | | # 指数行情策略函数 |
| | | def instant_trend_strategy(current_info): |