fix: write_holdings_batch同步holding_strategies(新持仓即时出现在盯盘)

This commit is contained in:
hmo
2026-07-27 16:22:29 +08:00
parent 27d8d997d4
commit 12e3abc57d
+39
View File
@@ -1372,6 +1372,22 @@ def sync_recommend_tag(conn, code: str, timing_signal: str):
_pct = round(_pct * 0.85, 0)
_pct = max(5, min(20, _pct))
_pos_auto = f"{int(_pct)}%(系统按RR{_rr_pos:.1f}自动计算,见原建议仓位)"
# ── 股数换算(2026-07-27 老爸:方便快速操作)──
try:
_lp = conn.execute("SELECT price FROM live_prices WHERE code=?", (code,)).fetchone()
_cash = conn.execute("SELECT cash FROM portfolio_summary WHERE id=1").fetchone()
if _lp and _lp[0] and _cash and _cash[0]:
_price = float(_lp[0])
_cash_val = float(_cash[0])
_shares_raw = _cash_val * _pct / 100.0 / _price
if _shares_raw >= 100:
_shares = int(_shares_raw / 100) * 100 # A股整手
else:
_shares = int(_shares_raw)
if _shares > 0:
_pos_auto = f"{int(_pct)}% ≈ {_shares}股(系统按RR{_rr_pos:.1f}自动计算)"
except Exception:
pass
conn.execute(
"UPDATE holding_strategies SET position_advice=? WHERE code=? AND status='active'",
(_pos_auto, code))
@@ -1986,6 +2002,29 @@ def write_holdings_batch(conn, holdings: list[dict]) -> tuple[bool, str]:
h.get('currency', 'CNY'), h.get('position_pct'),
))
conn.commit()
# ── 同步 holding_strategies2026-07-27 老爸:导入持仓后盯盘应即时出现)──
for h in holdings:
code = h.get('code')
name = h.get('name', '')
shares = h.get('shares') or 0
if not code or shares <= 0:
continue
existing = conn.execute(
"SELECT id, decision_type FROM holding_strategies WHERE code=? AND status='active'",
(code,)).fetchone()
if not existing:
# 全新持仓:创建基础策略条目
conn.execute("""
INSERT INTO holding_strategies (code, name, decision_type, strategy_type,
status, timing_signal, created_at)
VALUES (?, ?, '持仓策略', 'holding', 'active', '关注', datetime('now','localtime'))
""", (code, name))
elif existing[1] != '持仓策略':
# 已有条目但类型不对(自选转持仓)
conn.execute(
"UPDATE holding_strategies SET decision_type='持仓策略' WHERE code=? AND status='active'",
(code,))
conn.commit()
return True, f"已写入 {len(holdings)} 条持仓"
except sqlite3.IntegrityError as e:
conn.rollback()