Watchlist↔Holdings双向自动迁移

regenerate_all() 每次全量重评时自动:
① 自选股变成持仓(>0股) → 从watchlist移除
② 持仓清仓(股数归零) → 自动加回watchlist(有买入区才加)
Dad要求的双向自动同步,避免手动清理遗忘
This commit is contained in:
知微
2026-06-28 00:15:37 +08:00
parent 91d2957ab0
commit 4477b06f12
+35
View File
@@ -1847,6 +1847,41 @@ def regenerate_all(stdout=True):
_h['change_pct'] = _old.get('change_pct', 0)
existing_pf["holdings"] = _new_holdings
existing_pf["updated_at"] = datetime.now().strftime('%Y-%m-%d %H:%M')
# ── Watchlist ↔ Holdings 双向自动迁移(2026-06-27 Dad要求)──
# ① 持仓已有 → 从自选移除(买入自动清除)
wl_codes = {s.get("code") for s in wl.get("stocks", []) if s.get("code")}
pf_codes = {h.get("code") for h in _new_holdings if h.get("code") and h.get("shares", 0) > 0}
removed_from_wl = []
for h_code in wl_codes & pf_codes:
# 持仓>0且量够 → 自选移除
wl["stocks"] = [s for s in wl.get("stocks", []) if s.get("code") != h_code]
removed_from_wl.append(h_code)
if removed_from_wl and stdout:
print(f" 自选→持仓自动移除: {', '.join(removed_from_wl)}")
# ② 清仓/卖光 → 加回自选(只要仍有关注价值)
added_to_wl = []
old_pf_codes = {_h.get("code") for _h in existing_pf.get("holdings", []) if _h.get("code")}
sold_codes = old_pf_codes - pf_codes # 曾持仓但现在没有(或不在了)
for sc in sold_codes:
# 已有自选就不重复加
if sc in wl_codes:
continue
# 从现有decisions看是否有关注价值
for d in decisions:
if d.get("code") == sc and d.get("entry_low") and d.get("entry_high"):
wl["stocks"].append({
"code": sc, "name": d.get("name", sc),
"entry_low": d.get("entry_low"), "entry_high": d.get("entry_high"),
"stop_loss": d.get("stop_loss", 0),
"analysis": {"action": d.get("action", ""), "tech_snapshot": d.get("tech_snapshot", "")}
})
added_to_wl.append(sc)
break
if added_to_wl and stdout:
print(f" 清仓→自选自动加入: {', '.join(added_to_wl)}")
json.dump(existing_pf, open(PORTFOLIO_PATH, "w"), ensure_ascii=False, indent=2)
json.dump(wl, open(WATCHLIST_PATH, "w"), ensure_ascii=False, indent=2)