chore: position_management策略卡脚本入库(add_position_mgmt.py)

This commit is contained in:
xxm
2026-08-19 00:56:40 +08:00
parent 47e6890bba
commit 73ff64ed4b
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""建 position_management 持仓处置策略卡(2026-08-18 老莫)
规则明确(非LLM拍脑袋):
- 深套(浮亏>20%):不补不割等反弹 + 止损移动保护 + 换股时RR最低优先处置
- 未深套:按当前温区合格策略复核持仓(持有/减/清)
"""
import sqlite3, json, hashlib
DB = "/home/hmo/MoFin/data/mofin.db"
conn = sqlite3.connect(DB, timeout=30)
conn.execute("PRAGMA busy_timeout=30000")
def upsert(s):
hp = hashlib.md5((s.get("key_params") or "").encode()).hexdigest()[:12]
s2 = dict(s); s2["params_hash"] = hp; s2["updated_at"] = "2026-08-18 21:50:00"; s2["updated_by"] = "add_position_mgmt.py"
cols = ",".join(s2.keys()); ph = ",".join("?" * len(s2))
conn.execute(
f"INSERT INTO strategy_defs ({cols}) VALUES ({ph}) ON CONFLICT(strategy_name) DO UPDATE SET "
f"display_name=excluded.display_name, aliases=excluded.aliases, market=excluded.market, "
f"regime=excluded.regime, family=excluded.family, summary=excluded.summary, "
f"entry_logic=excluded.entry_logic, exit_logic=excluded.exit_logic, "
f"key_params=excluded.key_params, review_focus=excluded.review_focus, "
f"holding_style=excluded.holding_style, status=excluded.status, "
f"retired_reason=excluded.retired_reason, superseded_by=excluded.superseded_by, "
f"params_hash=excluded.params_hash, needs_review=excluded.needs_review, "
f"doc_ref=excluded.doc_ref, scanner_file=excluded.scanner_file, "
f"version=excluded.version, updated_at=excluded.updated_at, updated_by=excluded.updated_by",
[s2[k] for k in s2])
print(f" + {s['strategy_name']} 已写入/更新")
upsert(dict(
strategy_name="position_management",
display_name="持仓处置(情势变更兜底)",
aliases=json.dumps(["position_management", "pos_mgmt", "holding_disposal"]),
market="A", regime="all",
family="position_management",
summary="原策略失效且无合适新策略时的持仓处置框架(规则明确,非LLM拍脑袋):深套(浮亏>20%)不补不割等反弹+止损移动保护+换股时RR最低优先处置;未深套按当前温区合格策略复核持仓。",
entry_logic="不适用(持仓已有,非选股策略)——处置决策基于持仓成本 + 当前情势 + 当前温区合格策略框架。",
exit_logic="深套(浮亏>20%):不补不割等反弹,止损移动保护(止损只上不下),换股机会时RR最低优先处置;未深套:按当前温区合格策略的评估框架复核——持有(新止损/止盈)/减仓/清仓/换股。",
key_params=json.dumps({"deep_loss_pct": -20, "deep_loss_rule": "不补不割等反弹+止损移动保护", "swap_rule": "换股机会时RR最低优先处置", "normal_rule": "按当前温区合格策略复核持仓"}),
review_focus="①是否深套(浮亏>20%)?深套→不补不割等反弹,止损移动保护②未深套→当前温区合格策略是什么?按该策略的入场逻辑复核:现价在买区/止损/止盈什么位置③该策略的止损/止盈是否还适用(温区变了参数要不要调)④是否该腾资金换股(RR对比其他持仓)⑤若换股,选哪只(RR最低优先处置)",
holding_style="持仓管理(处置优先于换策略)",
status="active", version="v1",
scanner_file="",
doc_ref="docs/v_mr_strategy.md(L240弱市仓位管理)+max_hold分析结论.md",
))
conn.commit()
n = conn.execute("SELECT COUNT(*) FROM strategy_defs").fetchone()[0]
print(f"\nstrategy_defs 共 {n} 张卡")
conn.close()