From d28bf7fc3b438d571d066bb3cd3d28eae9c67efb Mon Sep 17 00:00:00 2001 From: hmo Date: Thu, 30 Jul 2026 21:59:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20v=5Fnext3=E7=94=9F=E4=BA=A7=E8=90=BD?= =?UTF-8?q?=E5=9C=B0=E2=80=94=E2=80=94=E6=9D=BF=E5=9D=97ADX=E5=85=A5?= =?UTF-8?q?=E6=9D=BF=E5=9D=97=E4=B8=8A=E4=B8=8B=E6=96=87+=E8=A1=8C?= =?UTF-8?q?=E4=B8=9A=E7=89=9B=E6=9D=A0=E6=9D=86=E6=8E=A5=E5=85=A5position?= =?UTF-8?q?=5Fadvice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/profile-scripts/strategy_lifecycle.py | 21 +++++++++++++++ strategy_lab.py | 27 +++++++++++++++----- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/deploy/profile-scripts/strategy_lifecycle.py b/deploy/profile-scripts/strategy_lifecycle.py index ed3d65ed..2db26829 100644 --- a/deploy/profile-scripts/strategy_lifecycle.py +++ b/deploy/profile-scripts/strategy_lifecycle.py @@ -1040,6 +1040,27 @@ def reassess_strategy(code, name, price, cost, shares, current_action, print(f" 分类: {stock_category} | {time_horizon} | {position_advice}") + # ── v_next3行业牛杠杆: 行业确认牛(行业ADX>25)时升一档仓位(2026-07-30落地)── + try: + _sec_adx = 0 + try: + from strategy_lab import sector_ctx, prepare_sector_context + from datetime import timedelta as _td + _end = datetime.now().strftime('%Y-%m-%d') + _start = (datetime.now() - _td(days=200)).strftime('%Y-%m-%d') + prepare_sector_context(_start, _end) + _sc = sector_ctx(code, _end) + _sec_adx = _sc.get('adx') or 0 + except Exception: + _sec_adx = 0 + if _sec_adx > 25: + _upgrade = {"小仓快进快出": "中等仓位", "中等仓位": "重仓", "正常配置": "重仓"} + if position_advice in _upgrade: + print(f" [行业牛杠杆] 行业ADX={_sec_adx:.0f}>25 → 仓位{position_advice}→{_upgrade[position_advice]}", flush=True) + position_advice = _upgrade[position_advice] + except Exception as _e: + print(f" [行业牛杠杆] 评估异常(跳过): {_e}", flush=True) + # ----- 短炒+强趋势检测:短炒分类但多周期多头时用移动止损代替弱支撑止损 ----- is_short_term_strong_trend = False if stock_category == "短炒": diff --git a/strategy_lab.py b/strategy_lab.py index 7bd69ff5..0cc355d8 100644 --- a/strategy_lab.py +++ b/strategy_lab.py @@ -552,17 +552,21 @@ def prepare_sector_context(start_date, end_date): try: # 1. 板块指数历史(全周期) try: - idx_rows = conn.execute(""" - SELECT sector, date, close, change_pct FROM sector_index_daily - WHERE date >= ? AND date <= ? ORDER BY sector, date - """, (start_date, end_date)).fetchall() + idx_rows = conn.execute( + "SELECT sector, date, close, change_pct, high, low FROM sector_index_daily " + "WHERE date >= ? AND date <= ? ORDER BY sector, date", + (start_date, end_date)).fetchall() except sqlite3.OperationalError: idx_rows = [] # 每板块计算 MA20 和斜率 from collections import defaultdict by_sector = defaultdict(list) - for sec, d, close, chg in idx_rows: + sec_hl = defaultdict(list) + for row in idx_rows: + sec, d, close, chg = row[0], row[1], row[2], row[3] by_sector[sec].append((d, close, chg)) + if len(row) >= 6: + sec_hl[sec].append((d, row[4], row[5])) for sec, series in by_sector.items(): closes = [c for _, c, _ in series] for i, (d, close, chg) in enumerate(series): @@ -574,8 +578,19 @@ def prepare_sector_context(start_date, end_date): ma20_5 = sum(closes[i-24:i-4]) / 20 if ma20_5 > 0: slope = round((ma20 - ma20_5) / ma20_5 * 100, 3) + _adx = None + _hl = sec_hl.get(sec, []) + if len(_hl) >= 20 and i >= 14: + from backtest_framework import calc_trend_strength + _hs = [x[1] for x in _hl] + _ls = [x[2] for x in _hl] + _cs = [c for _, c, _ in series] + if len(_cs) == len(_hl): + _av = calc_trend_strength(_hs, _ls, _cs, 14) + if i < len(_av): + _adx = _av[i] _SECTOR_CTX.setdefault(d, {})[sec] = { - 'change': chg, 'above_ma20': above, 'slope': slope, + 'change': chg, 'above_ma20': above, 'slope': slope, 'adx': _adx, } # 2. sector_snapshots 补充净流入和涨幅(近期,THS命名) snap_rows = conn.execute("""