From 0e9a0977e9fe7d487fbec5270b29102aa122eeec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=87=E8=96=87=E5=AE=89?= Date: Mon, 19 Jan 2026 16:37:34 +0800 Subject: [PATCH] a --- trading_system/position_manager.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/trading_system/position_manager.py b/trading_system/position_manager.py index c46fdb8..a1e3c6a 100644 --- a/trading_system/position_manager.py +++ b/trading_system/position_manager.py @@ -349,6 +349,11 @@ class PositionManager: self._pending_entry_orders.pop(symbol, None) logger.info(f"{symbol} [智能入场] 限价超时,且偏离{drift_ratio*100:.2f}%≤{max_drift_ratio*100:.2f}%,转市价兜底") order = await self.client.place_order(symbol=symbol, side=side, quantity=quantity, order_type="MARKET") + # 关键:转市价后必须更新 entry_order_id,否则后续会继续查询“已取消的旧限价单”,导致误判 CANCELED + try: + entry_order_id = order.get("orderId") if isinstance(order, dict) else None + except Exception: + entry_order_id = None break else: logger.info(f"{symbol} [智能入场] 限价超时,但偏离{drift_ratio*100:.2f}%>{max_drift_ratio*100:.2f}%,取消并放弃本次交易") @@ -412,7 +417,14 @@ class PositionManager: actual_entry_price = float(res.get("avg_price") or 0) filled_quantity = float(res.get("executed_qty") or 0) else: - logger.error(f"{symbol} [开仓] ❌ 订单未成交,状态: {order_status},不保存到数据库") + # 未成交(NEW/超时/CANCELED 等)属于“策略未触发入场”或“挂单没成交” + # 这不应当当作系统错误;同时需要撤单,避免留下悬挂委托造成后续混乱。 + logger.warning(f"{symbol} [开仓] 未成交,状态: {order_status},跳过本次开仓并撤销挂单") + try: + if str(order_status).upper() in {"NEW", "PARTIALLY_FILLED", "PENDING_NEW", "TIMEOUT"}: + await self.client.cancel_order(symbol, int(entry_order_id)) + except Exception: + pass self._pending_entry_orders.pop(symbol, None) return None