diff --git a/trading_system/position_manager.py b/trading_system/position_manager.py index e6ea05d..66f56b4 100644 --- a/trading_system/position_manager.py +++ b/trading_system/position_manager.py @@ -2915,6 +2915,8 @@ class PositionManager: except Exception: pass client_order_id_sync = None + # 标记是否「来历不明」:用于 DB entry_reason 与后续统计分析(sync_recovered_unknown_origin) + sync_unknown_origin = False # 仅当「明确查到开仓订单且 clientOrderId 非空且不以系统前缀开头」时标记为来历不明(仍会补建+挂 SL/TP+监控) is_clearly_manual = False if system_order_prefix: @@ -2935,12 +2937,15 @@ class PositionManager: is_clearly_manual = False logger.info(f" → {symbol} 开仓订单 clientOrderId 非系统前缀,但存在止损/止盈单,按系统单补建并监控") if is_clearly_manual: + sync_unknown_origin = True logger.info(f" → {symbol} 来历不明(开仓订单非系统前缀),将补建记录、自动挂止损止盈并纳入监控") else: # 未配置前缀时,不再因「无止损/止盈单」跳过:一律补建并自动挂 SL/TP、纳入监控 if sync_recover_only_has_sltp and not (await self._symbol_has_sltp_orders(symbol)): + sync_unknown_origin = True logger.info(f" → {symbol} 无止损/止盈单,将补建记录、自动挂止损止盈并纳入监控") # 不再因 is_clearly_manual 或 无 SL/TP 跳过,一律补建 + 挂 SL/TP + 监控 + entry_reason_sync = "sync_recovered_unknown_origin" if sync_unknown_origin else "sync_recovered" if entry_order_id and hasattr(Trade, "get_by_entry_order_id"): try: if Trade.get_by_entry_order_id(entry_order_id): @@ -2953,14 +2958,14 @@ class PositionManager: quantity=quantity, entry_price=entry_price, leverage=binance_position.get("leverage", 10), - entry_reason="sync_recovered", + entry_reason=entry_reason_sync, entry_order_id=entry_order_id, client_order_id=client_order_id_sync, notional_usdt=notional, margin_usdt=(notional / float(binance_position.get("leverage", 10) or 10)) if float(binance_position.get("leverage", 10) or 0) > 0 else None, account_id=self.account_id, ) - logger.info(f" ✓ {symbol} [状态同步] 已补建交易记录 (ID: {trade_id}, orderId: {entry_order_id or '-'})") + logger.info(f" ✓ {symbol} [状态同步] 已补建交易记录 (ID: {trade_id}, orderId: {entry_order_id or '-'}, entry_reason={entry_reason_sync})") ticker = await self.client.get_ticker_24h(symbol) current_price = ticker["price"] if ticker else entry_price lev = float(binance_position.get("leverage", 10)) @@ -2978,7 +2983,7 @@ class PositionManager: "symbol": symbol, "side": side, "quantity": quantity, "entryPrice": entry_price, "changePercent": 0, "orderId": entry_order_id, "tradeId": trade_id, "stopLoss": stop_loss_price, "takeProfit": take_profit_price, "initialStopLoss": stop_loss_price, - "leverage": lev, "entryReason": "sync_recovered", "atr": None, "maxProfit": 0.0, "trailingStopActivated": False, + "leverage": lev, "entryReason": entry_reason_sync, "atr": None, "maxProfit": 0.0, "trailingStopActivated": False, } self.active_positions[symbol] = position_info # 补建后立即在交易所挂/修正止损止盈(替换可能存在的异常远止损、缺止盈等)