修复期权币本位高危缺陷:开仓估量、卖回封顶、握库超时与条件单嵌套锁。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-07 12:00:08 +08:00
parent ea3e029eb9
commit 77fe0fa679
16 changed files with 724 additions and 201 deletions
+10 -28
View File
@@ -129,12 +129,16 @@ def run_options_profit_alerts(
premium_ccy=str(bucket.get("premium_ccy") or pos.get("premium_ccy") or ""),
)
try:
# 推送前先提交,避免握锁等 webhook
try:
conn.commit()
except Exception:
pass
send_wechat(msg)
conn.execute(
f"UPDATE options_trades SET profit_alert_sent = 1 WHERE id IN ({','.join('?' * len(bucket['ids']))})",
tuple(bucket["ids"]),
)
# 立即提交,避免后续目标/翻倍/同步异常回滚后每轮重推
try:
conn.commit()
except Exception:
@@ -532,19 +536,12 @@ def options_monitor_loop(
pe_cfg.setdefault("send_wechat", send_wechat)
pe_cfg.setdefault("account_label", account_label)
# 条件单可能嵌套开仓/卖币:单独短连接
# 条件单自管连接(开仓前会释放)
if cond_order_tick_fn is not None:
conn = get_db()
try:
cond_order_tick_fn(conn)
conn.commit()
cond_order_tick_fn(None)
except Exception:
pass
finally:
try:
conn.close()
except Exception:
pass
conn = get_db()
try:
@@ -565,37 +562,28 @@ def options_monitor_loop(
except Exception:
pass
# 目标/翻倍出场:短连接内扫描;close_fn 在连接关闭后由内部仍可能嵌套—
# 此处每段自管连接,平仓前必 close
# 目标/翻倍出场:传 get_db,平仓时不握业务连接
if target_close_fn is not None:
from lib.options.options_target_lib import run_options_target_closes
conn = get_db()
try:
run_options_target_closes(
conn,
get_db,
positions,
close_fn=target_close_fn,
send_wechat=send_wechat,
account_label=account_label,
cfg=pe_cfg,
)
conn.commit()
except Exception:
pass
finally:
try:
conn.close()
except Exception:
pass
if profit_exit_close_fn is not None:
from lib.options.options_profit_exit_lib import run_options_profit_exits
conn = get_db()
try:
run_options_profit_exits(
conn,
get_db,
positions,
close_fn=profit_exit_close_fn,
send_wechat=send_wechat,
@@ -603,14 +591,8 @@ def options_monitor_loop(
cfg=pe_cfg,
ex=pe_cfg.get("exchange_options"),
)
conn.commit()
except Exception:
pass
finally:
try:
conn.close()
except Exception:
pass
if sync_trades_fn is not None:
try: