diff --git a/mofin_db.py b/mofin_db.py index 54eed404..f319f6ab 100644 --- a/mofin_db.py +++ b/mofin_db.py @@ -1259,6 +1259,33 @@ def sync_recommend_tag(conn, code: str, timing_signal: str): _h = conn.execute("SELECT shares FROM holdings WHERE code=? AND is_active=1", (code,)).fetchone() if not (_h and (_h[0] or 0) > 0): timing_signal = "" # 非持仓的卖出/止盈不算动作信号 + # ── 仓位自动补全(2026-07-27 老爸:不明确就让它明确,不是丢弃)── + # LLM 经常输出"减仓或观望/中等仓位"等模糊表述,系统按公式自动计算。 + # 基础仓位 by RR(<1.5→不推荐,1.5~3→8%,3~5→12%,5+→15%) × 成长系数0.85(兜底) + # → 最终范围5-20% + if timing_signal in _ACTION_BUY: + import re as _re2 + _pos_r = conn.execute( + "SELECT rr_ratio, position_advice FROM holding_strategies WHERE code=? AND status='active'", (code,)).fetchone() + if _pos_r and not _re2.search(r'\d+(?:\.\d+)?\s*%', _pos_r[1] or ''): + _rr_pos = float(_pos_r[0] or 0) + if _rr_pos < 1.5: + _pct = 0 # RR不推荐 + elif _rr_pos < 3: + _pct = 8 + elif _rr_pos < 5: + _pct = 12 + else: + _pct = 15 + # 系数兜底:成长股0.85(最保守),大盘系数1.0(中性) + _pct = round(_pct * 0.85, 0) + _pct = max(5, min(20, _pct)) + _pos_auto = f"{int(_pct)}%(系统按RR{_rr_pos:.1f}自动计算,见原建议仓位)" + conn.execute( + "UPDATE holding_strategies SET position_advice=? WHERE code=? AND status='active'", + (_pos_auto, code)) + conn.commit() + print(f" [AUTO-POS] {code} 仓位'{_pos_r[1]}'→'{_pos_auto}'", flush=True) if timing_signal in _ACTION_BUY or timing_signal in _ACTION_SELL: conn.execute( "UPDATE holding_strategies SET tag='current_recommend' "