From 8d2d09396b4e067ee5b4fbf30d84e37861411d71 Mon Sep 17 00:00:00 2001 From: dekun Date: Tue, 30 Jun 2026 21:23:44 +0800 Subject: [PATCH] Fix missing trade log after manual close when CTP is connected. Co-authored-by: Cursor --- install_trading.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/install_trading.py b/install_trading.py index b8b08f9..c367022 100644 --- a/install_trading.py +++ b/install_trading.py @@ -2405,23 +2405,23 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se lots=lots, price=price, settings=_settings_dict(), order_type="market", ) - if not ctp_status(mode).get("connected"): - write_manual_close_trade_log( - conn, - mon, - symbol=sym, - direction=direction, - lots=lots, - close_price=price, - entry_price=entry or price, - trading_mode=mode, - capital=capital, - stop_loss=float(mon["stop_loss"]) if mon and mon.get("stop_loss") is not None else None, - take_profit=float(mon["take_profit"]) if mon and mon.get("take_profit") is not None else None, - open_time=(mon.get("open_time") or "") if mon else "", - symbol_name=(mon.get("symbol_name") or "") if mon else "", - market_code=(mon.get("market_code") or "") if mon else "", - ) + # 始终写本地记录:CTP 同步依赖内存开平配对,重启后或成交回报延迟时会漏记 + write_manual_close_trade_log( + conn, + mon, + symbol=sym, + direction=direction, + lots=lots, + close_price=price, + entry_price=entry or price, + trading_mode=mode, + capital=capital, + stop_loss=float(mon["stop_loss"]) if mon and mon.get("stop_loss") is not None else None, + take_profit=float(mon["take_profit"]) if mon and mon.get("take_profit") is not None else None, + open_time=(mon.get("open_time") or "") if mon else "", + symbol_name=(mon.get("symbol_name") or "") if mon else "", + market_code=(mon.get("market_code") or "") if mon else "", + ) _close_all_monitors_for_sym_dir(conn, sym, direction) conn.commit() try: @@ -2432,7 +2432,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se logger.debug("sync trades after close: %s", exc) conn.close() _push_position_snapshot_async() - return jsonify({"ok": True, "message": "已平仓;交易记录将按柜台成交同步"}) + return jsonify({"ok": True, "message": "已平仓,交易记录已写入"}) except ValueError as exc: conn.close() return jsonify({"ok": False, "error": str(exc)}), 400