diff --git a/trading_system/position_manager.py b/trading_system/position_manager.py index 2001d19..e9e77a9 100644 --- a/trading_system/position_manager.py +++ b/trading_system/position_manager.py @@ -3423,7 +3423,7 @@ class PositionManager: # 启动WebSocket监控 if self._monitoring_enabled: await self._start_position_monitoring(symbol) - logger.info(f"{symbol} [状态同步] ✓ 已启动实时监控") + logger.info(f"[账号{self.account_id}] {symbol} [状态同步] ✓ 已启动实时监控") logger.info(f"{symbol} [状态同步] ✓ 手动开仓同步完成") @@ -3858,12 +3858,12 @@ class PositionManager: profit_protection_enabled = bool(config.TRADING_CONFIG.get('PROFIT_PROTECTION_ENABLED', True)) use_trailing = profit_protection_enabled and bool(config.TRADING_CONFIG.get('USE_TRAILING_STOP', False)) if use_trailing: - logger.debug(f"{symbol} [实时监控-移动止损] 已启用,将检查移动止损逻辑") + logger.debug(f"[账号{self.account_id}] {symbol} [实时监控-移动止损] 已启用,将检查移动止损逻辑") else: if not profit_protection_enabled: - logger.debug(f"{symbol} [实时监控-移动止损/保本] 已禁用(PROFIT_PROTECTION_ENABLED=False)") + logger.debug(f"[账号{self.account_id}] {symbol} [实时监控-移动止损/保本] 已禁用(PROFIT_PROTECTION_ENABLED=False)") else: - logger.debug(f"{symbol} [实时监控-移动止损] 已禁用(USE_TRAILING_STOP=False),跳过移动止损检查") + logger.debug(f"[账号{self.account_id}] {symbol} [实时监控-移动止损] 已禁用(USE_TRAILING_STOP=False),跳过移动止损检查") if use_trailing: trailing_activation = config.TRADING_CONFIG.get('TRAILING_STOP_ACTIVATION', 0.01) # 相对于保证金 trailing_protect = config.TRADING_CONFIG.get('TRAILING_STOP_PROTECT', 0.01) # 相对于保证金 @@ -3886,7 +3886,7 @@ class PositionManager: position_info['stopLoss'] = breakeven position_info['breakevenStopSet'] = True logger.info( - f"{symbol} [实时监控] 盈利{pnl_percent_margin:.2f}%≥{lock_pct*100:.0f}%,止损已移至含手续费保本价 {breakeven:.4f}(留住盈利)" + f"[账号{self.account_id}] {symbol} [实时监控] 盈利{pnl_percent_margin:.2f}%≥{lock_pct*100:.0f}%,止损已移至含手续费保本价 {breakeven:.4f}(留住盈利)" ) try: await self._ensure_exchange_sltp_orders(symbol, position_info, current_price=current_price_float) @@ -3908,13 +3908,13 @@ class PositionManager: position_info['stopLoss'] = new_stop_loss logger.info( - f"{symbol} [实时监控] 移动止损激活: 止损移至保护利润位 {new_stop_loss:.4f} " + f"[账号{self.account_id}] {symbol} [实时监控] 移动止损激活: 止损移至保护利润位 {new_stop_loss:.4f} " f"(盈利: {pnl_percent_margin:.2f}% of margin, 保护: {trailing_protect*100:.1f}% of margin)" ) # 同步至交易所:取消原止损单并按新止损价重挂,使移动止损也有交易所保护 try: await self._ensure_exchange_sltp_orders(symbol, position_info, current_price=current_price_float) - logger.info(f"{symbol} [实时监控] 已同步移动止损至交易所") + logger.info(f"[账号{self.account_id}] {symbol} [实时监控] 已同步移动止损至交易所") except Exception as sync_e: logger.warning(f"{symbol} 同步移动止损至交易所失败(不影响本地监控): {sync_e}") else: @@ -3923,7 +3923,7 @@ class PositionManager: # 移动止损不应该覆盖分步止盈设置的止损价 if position_info.get('partialProfitTaken', False): # 分步止盈第一目标已触发,移动止损不再更新 - logger.debug(f"{symbol} [实时监控-移动止损] 分步止盈第一目标已触发,移动止损不再更新剩余仓位止损价") + logger.debug(f"[账号{self.account_id}] {symbol} [实时监控-移动止损] 分步止盈第一目标已触发,移动止损不再更新剩余仓位止损价") else: # 盈利超过阈值后,止损移至保护利润位(基于保证金);保护金额至少覆盖手续费 protect_amount = max(margin * trailing_protect, self._min_protect_amount_for_fees(margin, leverage)) @@ -3933,12 +3933,12 @@ class PositionManager: if new_stop_loss > position_info['stopLoss']: position_info['stopLoss'] = new_stop_loss logger.info( - f"{symbol} [实时监控] 移动止损更新: {new_stop_loss:.4f} " + f"[账号{self.account_id}] {symbol} [实时监控] 移动止损更新: {new_stop_loss:.4f} " f"(保护{trailing_protect*100:.1f}% of margin = {protect_amount:.4f} USDT)" ) try: await self._ensure_exchange_sltp_orders(symbol, position_info, current_price=current_price_float) - logger.info(f"{symbol} [实时监控] 已同步移动止损至交易所") + logger.info(f"[账号{self.account_id}] {symbol} [实时监控] 已同步移动止损至交易所") except Exception as sync_e: logger.warning(f"{symbol} 同步移动止损至交易所失败(不影响本地监控): {sync_e}") else: # SELL @@ -3947,12 +3947,12 @@ class PositionManager: if new_stop_loss > position_info['stopLoss'] and pnl_amount > 0: position_info['stopLoss'] = new_stop_loss logger.info( - f"{symbol} [实时监控] 移动止损更新: {new_stop_loss:.4f} " + f"[账号{self.account_id}] {symbol} [实时监控] 移动止损更新: {new_stop_loss:.4f} " f"(保护{trailing_protect*100:.1f}% of margin = {protect_amount:.4f} USDT)" ) try: await self._ensure_exchange_sltp_orders(symbol, position_info, current_price=current_price_float) - logger.info(f"{symbol} [实时监控] 已同步移动止损至交易所") + logger.info(f"[账号{self.account_id}] {symbol} [实时监控] 已同步移动止损至交易所") except Exception as sync_e: logger.warning(f"{symbol} 同步移动止损至交易所失败(不影响本地监控): {sync_e}") @@ -3986,7 +3986,7 @@ class PositionManager: price_change_pct = (entry_price - current_price_float) / entry_price * 100 logger.warning( - f"{symbol} [实时监控] 诊断: \n" + f"[账号{self.account_id}] {symbol} [实时监控] 诊断: \n" f" • ROE(保证金盈亏): {pnl_percent_margin:.2f}% (用户关注)\n" f" • 价格变动: {price_change_pct:.2f}% (实际币价涨跌)\n" f" • 杠杆倍数: {leverage}x (放大倍数)\n" @@ -4004,16 +4004,16 @@ class PositionManager: # 盈利单触发止损,应该是移动止损 if position_info.get('trailingStopActivated'): exit_reason_sl = 'trailing_stop' - logger.warning(f"{symbol} [实时监控] ⚠️ 盈利单触发止损,标记为移动止损(盈利: {pnl_percent_margin:.2f}% of margin)") + logger.warning(f"[账号{self.account_id}] {symbol} [实时监控] ⚠️ 盈利单触发止损,标记为移动止损(盈利: {pnl_percent_margin:.2f}% of margin)") else: # 盈利单但未激活移动止损,可能是分步止盈后的剩余仓位止损 if partial_profit_taken: exit_reason_sl = 'take_profit_partial_then_stop' - logger.info(f"{symbol} [实时监控] 第一目标止盈后,剩余仓位触发止损(保本)") + logger.info(f"[账号{self.account_id}] {symbol} [实时监控] 第一目标止盈后,剩余仓位触发止损(保本)") else: # 异常情况:盈利单触发止损但未激活移动止损 exit_reason_sl = 'trailing_stop' # 默认标记为移动止损 - logger.warning(f"{symbol} [实时监控] ⚠️ 异常:盈利单触发止损但未激活移动止损,标记为移动止损") + logger.warning(f"[账号{self.account_id}] {symbol} [实时监控] ⚠️ 异常:盈利单触发止损但未激活移动止损,标记为移动止损") else: # 正常止损逻辑 should_close_due_to_sl = True @@ -4041,7 +4041,7 @@ class PositionManager: # 详细诊断日志:记录平仓时的所有关键信息 logger.warning("=" * 80) - logger.warning(f"{symbol} [实时监控-平仓诊断日志] ===== 触发止损平仓 =====") + logger.warning(f"[账号{self.account_id}] {symbol} [实时监控-平仓诊断日志] ===== 触发止损平仓 =====") logger.warning(f" 平仓原因: {exit_reason_sl}") logger.warning(f" 入场价格: {entry_price:.6f} USDT") logger.warning(f" 当前价格: {current_price_float:.6f} USDT") @@ -4059,11 +4059,11 @@ class PositionManager: # ⚠️ 2026-01-27优化:如果已部分止盈,细分状态为"第一目标止盈后剩余仓位止损" if partial_profit_taken: exit_reason_sl = 'take_profit_partial_then_stop' - logger.info(f"{symbol} [实时监控] 第一目标止盈后,剩余仓位触发止损(保本)") + logger.info(f"[账号{self.account_id}] {symbol} [实时监控] 第一目标止盈后,剩余仓位触发止损(保本)") # ⚠️ 关键修复:止损必须立即执行,不受时间锁限制 if await self.close_position(symbol, reason=exit_reason_sl): - logger.info(f"{symbol} [实时监控] 止损平仓成功(不受时间锁限制)") + logger.info(f"[账号{self.account_id}] {symbol} [实时监控] 止损平仓成功(不受时间锁限制)") return # 止损已执行,跳过后续止盈检查 # 检查分步止盈(实时监控) @@ -4097,7 +4097,7 @@ class PositionManager: take_profit_pct_config = take_profit_pct_config / 100.0 take_profit_pct_display = take_profit_pct_config * 100 logger.info( - f"{symbol} [实时监控] 触发第一目标止盈({take_profit_pct_display:.1f}%固定止盈,基于保证金): " + f"[账号{self.account_id}] {symbol} [实时监控] 触发第一目标止盈({take_profit_pct_display:.1f}%固定止盈,基于保证金): " f"当前盈亏={pnl_percent_margin:.2f}% of margin >= 目标={take_profit_1_pct_margin:.2f}% of margin | " f"当前价={current_price_float:.4f}, 目标价={take_profit_1:.4f} | " f"将平掉50%仓位,锁定{take_profit_pct_display:.1f}%盈利,剩余50%追求更高收益" @@ -4109,7 +4109,7 @@ class PositionManager: close_position_side = 'LONG' if position_info['side'] == 'BUY' else 'SHORT' live_amt = await self._get_live_position_amt(symbol, position_side=close_position_side) if live_amt is None or abs(live_amt) <= 0: - logger.warning(f"{symbol} [实时监控] 部分止盈:实时持仓已为0,跳过部分平仓") + logger.warning(f"[账号{self.account_id}] {symbol} [实时监控] 部分止盈:实时持仓已为0,跳过部分平仓") else: partial_quantity = min(partial_quantity, abs(live_amt)) partial_quantity = await self._adjust_close_quantity(symbol, partial_quantity) @@ -4126,18 +4126,18 @@ class PositionManager: position_info['partialProfitTaken'] = True position_info['remainingQuantity'] = remaining_quantity - partial_quantity logger.info( - f"{symbol} [实时监控] 部分止盈成功: 平仓{partial_quantity:.4f},剩余{position_info['remainingQuantity']:.4f}" + f"[账号{self.account_id}] {symbol} [实时监控] 部分止盈成功: 平仓{partial_quantity:.4f},剩余{position_info['remainingQuantity']:.4f}" ) # 分步止盈后的"保本"处理:仅在盈利保护总开关开启时移至含手续费保本价 if profit_protection_enabled: breakeven = self._breakeven_stop_price(entry_price, position_info['side']) position_info['stopLoss'] = breakeven logger.info( - f"{symbol} [实时监控] 部分止盈后:剩余仓位止损移至含手续费保本价 {breakeven:.4f}(入场: {entry_price:.4f})," + f"[账号{self.account_id}] {symbol} [实时监控] 部分止盈后:剩余仓位止损移至含手续费保本价 {breakeven:.4f}(入场: {entry_price:.4f})," f"剩余50%仓位追求更高收益(第二目标:4.0:1盈亏比或更高)" ) except Exception as e: - logger.error(f"{symbol} [实时监控] 部分止盈失败: {e}") + logger.error(f"[账号{self.account_id}] {symbol} [实时监控] 部分止盈失败: {e}") # 第二目标:4.0:1止盈,平掉剩余仓位(山寨币策略) if partial_profit_taken and take_profit_2 is not None and not should_close: @@ -4162,7 +4162,7 @@ class PositionManager: # ⚠️ 2026-01-27优化:细分状态,区分"第一目标止盈后第二目标止盈" exit_reason = 'take_profit_partial_then_take_profit' logger.info( - f"{symbol} [实时监控] 触发第二目标止盈(4.0:1,山寨币策略): " + f"[账号{self.account_id}] {symbol} [实时监控] 触发第二目标止盈(4.0:1,山寨币策略): " f"剩余仓位盈亏={remaining_pnl_pct_margin:.2f}% of margin >= 目标={take_profit_2_pct_margin:.2f}% of margin | " f"当前价={current_price_float:.4f}, 目标价={take_profit_2:.4f}, " f"剩余数量={remaining_quantity:.4f}" @@ -4194,7 +4194,7 @@ class PositionManager: if should_log: trigger_condition = pnl_percent_margin >= take_profit_pct_margin logger.warning( - f"{symbol} [实时监控] 诊断: 盈利{pnl_percent_margin:.2f}% of margin | " + f"[账号{self.account_id}] {symbol} [实时监控] 诊断: 盈利{pnl_percent_margin:.2f}% of margin | " f"当前价: {current_price_float:.4f} | " f"入场价: {entry_price:.4f} | " f"止盈价: {take_profit:.4f} (配置目标: {take_profit_pct_margin:.2f}% of margin, 价格对应: {take_profit_pct_margin_from_price:.2f}%) | " @@ -4224,7 +4224,7 @@ class PositionManager: # 详细诊断日志:记录平仓时的所有关键信息 logger.info("=" * 80) - logger.info(f"{symbol} [实时监控-平仓诊断日志] ===== 触发止盈平仓 =====") + logger.info(f"[账号{self.account_id}] {symbol} [实时监控-平仓诊断日志] ===== 触发止盈平仓 =====") logger.info(f" 平仓原因: {exit_reason}") logger.info(f" 入场价格: {entry_price:.6f} USDT") logger.info(f" 当前价格: {current_price_float:.6f} USDT")