From 66a78759d31c413019ae0ade4acb665ee73422ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=87=E8=96=87=E5=AE=89?= Date: Sun, 15 Feb 2026 10:15:42 +0800 Subject: [PATCH] 1 --- trading_system/position_manager.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/trading_system/position_manager.py b/trading_system/position_manager.py index f49d1c4..e6ea05d 100644 --- a/trading_system/position_manager.py +++ b/trading_system/position_manager.py @@ -2888,10 +2888,10 @@ class PositionManager: system_order_prefix = (config.TRADING_CONFIG.get("SYSTEM_ORDER_ID_PREFIX") or "").strip() if system_order_prefix: logger.info( - f" → 补建「系统单」记录:仅当开仓订单 clientOrderId 明确非 {system_order_prefix!r} 前缀时跳过(视为手动单),其余一律补建" + f" → 补建缺失持仓:一律写入 DB、自动挂止损止盈并纳入监控(含 clientOrderId 非 {system_order_prefix!r} 前缀的来历不明仓)" ) else: - logger.info(" → 补建「系统单」记录(仅当存在止损/止盈单时创建,避免手动单误建)" if sync_recover_only_has_sltp else " → 补建缺失持仓记录") + logger.info(" → 补建缺失持仓:一律写入 DB、自动挂止损止盈并纳入监控(含无 SL/TP 的仓位)") for symbol in missing_in_db: try: binance_position = next((p for p in binance_positions if p.get("symbol") == symbol), None) @@ -2915,7 +2915,7 @@ class PositionManager: except Exception: pass client_order_id_sync = None - # 仅当「明确查到开仓订单且 clientOrderId 非空且不以系统前缀开头」时视为手动单并跳过;其余一律补建(避免漏掉系统单) + # 仅当「明确查到开仓订单且 clientOrderId 非空且不以系统前缀开头」时标记为来历不明(仍会补建+挂 SL/TP+监控) is_clearly_manual = False if system_order_prefix: if entry_order_id: @@ -2925,17 +2925,22 @@ class PositionManager: client_order_id_sync = cid or None if cid and not cid.startswith(system_order_prefix): is_clearly_manual = True - logger.debug(f" {symbol} 开仓订单 clientOrderId={cid!r} 非系统前缀,视为手动单,跳过补建") + logger.debug(f" {symbol} 开仓订单 clientOrderId={cid!r} 非系统前缀,将按来历不明仓补建并挂 SL/TP") except Exception as e: logger.debug(f" {symbol} 查询开仓订单失败: {e},按系统单补建") # 无法获取订单或 cid 为空(历史单/未带前缀)时不视为手动单,继续补建 + # 例外:若开启「仅币安有仓且存在 SL/TP 也监控」,且该 symbol 有止损/止盈单,可视为系统单(仅影响日志) + if is_clearly_manual and config.TRADING_CONFIG.get("SYNC_MONITOR_BINANCE_POSITIONS_WITH_SLTP", True): + if await self._symbol_has_sltp_orders(symbol): + is_clearly_manual = False + logger.info(f" → {symbol} 开仓订单 clientOrderId 非系统前缀,但存在止损/止盈单,按系统单补建并监控") + if is_clearly_manual: + logger.info(f" → {symbol} 来历不明(开仓订单非系统前缀),将补建记录、自动挂止损止盈并纳入监控") else: - # 未配置前缀时,用「是否有止损/止盈单」区分 + # 未配置前缀时,不再因「无止损/止盈单」跳过:一律补建并自动挂 SL/TP、纳入监控 if sync_recover_only_has_sltp and not (await self._symbol_has_sltp_orders(symbol)): - logger.debug(f" {symbol} 无止损/止盈单,跳过补建") - continue - if is_clearly_manual: - continue + logger.info(f" → {symbol} 无止损/止盈单,将补建记录、自动挂止损止盈并纳入监控") + # 不再因 is_clearly_manual 或 无 SL/TP 跳过,一律补建 + 挂 SL/TP + 监控 if entry_order_id and hasattr(Trade, "get_by_entry_order_id"): try: if Trade.get_by_entry_order_id(entry_order_id):