修复粉尘桥拦开仓,并继续消除模拟盘嵌套握锁导致的 database is locked。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -514,7 +514,94 @@ def sell_residual_after_option_flat(
|
||||
return _apply_bridge(conn_or_get_db)
|
||||
|
||||
|
||||
def bridge_blocks_new_open_msg(conn: sqlite3.Connection) -> str | None:
|
||||
def clear_dust_pending_sell_bridges(
|
||||
conn_or_get_db: Any,
|
||||
ex: Any | None,
|
||||
) -> int:
|
||||
"""待卖回桥若交易户只剩粉尘(或已空),关闭桥并返回关闭条数.
|
||||
|
||||
推荐传 get_db:余额查询(模拟盘会另开连接)绝不能套在未关闭的业务连接上。
|
||||
"""
|
||||
if ex is None:
|
||||
return 0
|
||||
get_db = conn_or_get_db if callable(conn_or_get_db) else None
|
||||
|
||||
def _pending(c: sqlite3.Connection) -> list[dict[str, Any]]:
|
||||
return [
|
||||
b
|
||||
for b in list_open_bridges(c)
|
||||
if str(b.get("status") or "") == BRIDGE_PENDING_SELL
|
||||
]
|
||||
|
||||
if get_db is not None:
|
||||
conn = get_db()
|
||||
try:
|
||||
pending = _pending(conn)
|
||||
try:
|
||||
conn.commit()
|
||||
except Exception:
|
||||
pass
|
||||
finally:
|
||||
try:
|
||||
conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
conn = conn_or_get_db
|
||||
pending = _pending(conn)
|
||||
try:
|
||||
conn.commit()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
to_close: list[dict[str, Any]] = []
|
||||
for b in pending:
|
||||
uly = str(b.get("underlying") or "ETH").upper() or "ETH"
|
||||
try:
|
||||
avail = fetch_trading_coin_available(ex, uly)
|
||||
except Exception:
|
||||
continue
|
||||
if avail is None:
|
||||
continue
|
||||
if is_sellable_spot_coin(ex, uly, avail):
|
||||
continue
|
||||
to_close.append(b)
|
||||
if not to_close:
|
||||
return 0
|
||||
|
||||
def _close_all(c: sqlite3.Connection) -> int:
|
||||
n = 0
|
||||
for b in to_close:
|
||||
try:
|
||||
update_bridge(
|
||||
c,
|
||||
int(b["id"]),
|
||||
status=BRIDGE_CLOSED,
|
||||
message="粉尘低于现货最小可卖,已忽略",
|
||||
close=True,
|
||||
)
|
||||
n += 1
|
||||
except Exception:
|
||||
pass
|
||||
return n
|
||||
|
||||
if get_db is not None:
|
||||
conn2 = get_db()
|
||||
try:
|
||||
return _close_all(conn2)
|
||||
finally:
|
||||
try:
|
||||
conn2.close()
|
||||
except Exception:
|
||||
pass
|
||||
return _close_all(conn_or_get_db)
|
||||
|
||||
|
||||
def bridge_blocks_new_open_msg(
|
||||
conn: sqlite3.Connection,
|
||||
ex: Any | None = None,
|
||||
) -> str | None:
|
||||
_ = ex # 清粉尘请调用方先 clear_dust_pending_sell_bridges(get_db, ex)
|
||||
bridges = list_open_bridges(conn)
|
||||
if not bridges:
|
||||
return None
|
||||
@@ -528,8 +615,18 @@ def bridge_blocks_new_open_msg(conn: sqlite3.Connection) -> str | None:
|
||||
return "存在未完成的币本位资金桥,暂不可开仓"
|
||||
|
||||
|
||||
def mode_switch_block_msg(conn: sqlite3.Connection, ex: Any | None = None) -> str | None:
|
||||
def mode_switch_block_msg(
|
||||
conn: sqlite3.Connection,
|
||||
ex: Any | None = None,
|
||||
*,
|
||||
get_db: Any | None = None,
|
||||
) -> str | None:
|
||||
"""有单笔期权仓或未完成桥时禁止切换本位."""
|
||||
try:
|
||||
if get_db is not None:
|
||||
clear_dust_pending_sell_bridges(get_db, ex)
|
||||
except Exception:
|
||||
pass
|
||||
if has_unfinished_bridge(conn):
|
||||
return "存在未完成的币本位资金桥,禁止切换期权本位模式"
|
||||
if ex is not None:
|
||||
|
||||
Reference in New Issue
Block a user