feat: v_next3生产落地——板块ADX入板块上下文+行业牛杠杆接入position_advice
This commit is contained in:
@@ -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 == "短炒":
|
||||
|
||||
+21
-6
@@ -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("""
|
||||
|
||||
Reference in New Issue
Block a user