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

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
+87 -13
View File
@@ -668,25 +668,72 @@ def _try_open_full(
def run_cond_orders_tick(
conn: sqlite3.Connection,
conn_or_get_db: Any,
cfg: dict[str, Any],
*,
send_wechat: Any = None,
account_label: str = "",
) -> None:
"""监控循环单步:周四未触发取消 / 穿越 / 偏离区 / 开仓 / 出场收尾."""
ensure_cond_order_table(conn)
expire_untriggered_cond_orders(
conn, send_wechat=send_wechat, account_label=account_label
)
ex = cfg.get("exchange_options")
if ex is None:
return
orders = list_cond_orders(conn, active_only=True)
if not orders:
"""监控循环单步:周四未触发取消 / 穿越 / 偏离区 / 开仓 / 出场收尾.
推荐传 get_db:开仓前会关闭连接,避免与买币/等待成交嵌套锁库。
"""
get_db = conn_or_get_db if callable(conn_or_get_db) else None
if get_db is None:
conn = conn_or_get_db
ensure_cond_order_table(conn)
expire_untriggered_cond_orders(
conn, send_wechat=send_wechat, account_label=account_label
)
ex = cfg.get("exchange_options")
if ex is None:
return
for order in list_cond_orders(conn, active_only=True):
try:
_tick_one(
conn,
cfg,
ex,
order,
send_wechat=send_wechat,
account_label=account_label,
get_db=None,
)
except Exception as e:
try:
conn.execute(
"UPDATE options_cond_orders SET message=?, updated_at=? WHERE hub_order_id=?",
(f"监控异常: {e}"[:200], _now(), order["hub_order_id"]),
)
conn.commit()
except Exception:
pass
return
ex: Any = None
orders: list[dict[str, Any]] = []
conn = get_db()
try:
ensure_cond_order_table(conn)
expire_untriggered_cond_orders(
conn, send_wechat=send_wechat, account_label=account_label
)
ex = cfg.get("exchange_options")
if ex is not None:
orders = list_cond_orders(conn, active_only=True)
try:
conn.commit()
except Exception:
pass
finally:
try:
conn.close()
except Exception:
pass
if ex is None or not orders:
return
for order in orders:
conn = get_db()
try:
_tick_one(
conn,
@@ -695,7 +742,12 @@ def run_cond_orders_tick(
order,
send_wechat=send_wechat,
account_label=account_label,
get_db=get_db,
)
try:
conn.commit()
except Exception:
pass
except Exception as e:
try:
conn.execute(
@@ -705,6 +757,11 @@ def run_cond_orders_tick(
conn.commit()
except Exception:
pass
finally:
try:
conn.close()
except Exception:
pass
def _tick_one(
@@ -715,6 +772,7 @@ def _tick_one(
*,
send_wechat: Any,
account_label: str,
get_db: Any = None,
) -> None:
status = str(order.get("status") or "")
underlying = str(order.get("underlying") or "ETH")
@@ -814,11 +872,27 @@ def _tick_one(
)
conn.commit()
return
# 开仓(买币/等成交)前释放本连接,避免嵌套锁库
try:
conn.commit()
except Exception:
pass
hub_id = order["hub_order_id"]
if get_db is not None:
try:
conn.close()
except Exception:
pass
conn = None
result = _try_open_full(cfg, ex, inst_id=inst_id, order=order)
if get_db is not None:
conn = get_db()
ensure_cond_order_table(conn)
assert conn is not None
if not result.get("ok"):
conn.execute(
"UPDATE options_cond_orders SET message=?, updated_at=? WHERE hub_order_id=?",
(f"开仓未成继续等: {result.get('msg') or '失败'}"[:180], _now(), order["hub_order_id"]),
(f"开仓未成继续等: {result.get('msg') or '失败'}"[:180], _now(), hub_id),
)
conn.commit()
return
@@ -837,7 +911,7 @@ def _tick_one(
_now(),
f"已开仓 {inst_id}",
_now(),
order["hub_order_id"],
hub_id,
),
)
conn.commit()