feat(position_manager): 增强日志记录,添加账号信息
在 `position_manager.py` 中更新了日志记录,添加了账号 ID 信息,以便于在多账号环境中更好地追踪和管理交易记录。这一改进提升了系统的可维护性和调试能力。
This commit is contained in:
parent
1a9e5a382a
commit
8e0233dd5d
|
|
@ -1644,7 +1644,7 @@ class PositionManager:
|
|||
sl_order = None
|
||||
|
||||
if sl_order:
|
||||
logger.info(f"{symbol} ✓ 止损单已成功挂到交易所: {sl_order.get('algoId', 'N/A')}")
|
||||
logger.info(f"[账号{self.account_id}] {symbol} ✓ 止损单已成功挂到交易所: {sl_order.get('algoId', 'N/A')}")
|
||||
else:
|
||||
if sl_failed_due_to_gte:
|
||||
logger.warning(f"{symbol} 条件单被拒(持仓未就绪或已平),跳过交易所止损单,将依赖 WebSocket 监控")
|
||||
|
|
@ -1747,7 +1747,7 @@ class PositionManager:
|
|||
logger.warning(f"{symbol} 挂止盈单失败: {e}")
|
||||
tp_order = None
|
||||
if tp_order:
|
||||
logger.info(f"{symbol} ✓ 止盈单已成功挂到交易所: {tp_order.get('algoId', 'N/A')}")
|
||||
logger.info(f"[账号{self.account_id}] {symbol} ✓ 止盈单已成功挂到交易所: {tp_order.get('algoId', 'N/A')}")
|
||||
else:
|
||||
logger.warning(f"{symbol} ⚠️ 止盈单挂单失败,将依赖WebSocket监控")
|
||||
|
||||
|
|
@ -1763,7 +1763,7 @@ class PositionManager:
|
|||
|
||||
if position_info.get("exchangeSlOrderId") or position_info.get("exchangeTpOrderId"):
|
||||
logger.info(
|
||||
f"{symbol} 已挂币安保护单: "
|
||||
f"[账号{self.account_id}] {symbol} 已挂币安保护单: "
|
||||
f"SL={position_info.get('exchangeSlOrderId') or '-'} "
|
||||
f"TP={position_info.get('exchangeTpOrderId') or '-'}"
|
||||
)
|
||||
|
|
@ -3496,15 +3496,15 @@ class PositionManager:
|
|||
active_symbols = set(self.active_positions.keys())
|
||||
sync_create_manual = config.TRADING_CONFIG.get("SYNC_CREATE_MANUAL_ENTRY_RECORD", False)
|
||||
|
||||
logger.info(f"币安持仓: {len(binance_symbols)} 个 ({', '.join(binance_symbols) if binance_symbols else '无'})")
|
||||
logger.info(f"本地持仓记录: {len(active_symbols)} 个 ({', '.join(active_symbols) if active_symbols else '无'})")
|
||||
logger.info(f"[账号{self.account_id}] 币安持仓: {len(binance_symbols)} 个 ({', '.join(binance_symbols) if binance_symbols else '无'})")
|
||||
logger.info(f"[账号{self.account_id}] 本地持仓记录: {len(active_symbols)} 个 ({', '.join(active_symbols) if active_symbols else '无'})")
|
||||
|
||||
# 仅为本系统已有记录的持仓启动监控;若未开启「同步创建手动开仓记录」,则不为「仅币安有仓」创建临时记录或监控
|
||||
# 例外:SYNC_MONITOR_BINANCE_POSITIONS_WITH_SLTP=True 时,对「仅币安有仓且存在止损/止盈单」的视为可监管(多为系统单),补建并监控
|
||||
only_binance = binance_symbols - active_symbols
|
||||
monitor_binance_with_sltp = config.TRADING_CONFIG.get("SYNC_MONITOR_BINANCE_POSITIONS_WITH_SLTP", True)
|
||||
if only_binance and not sync_create_manual and not monitor_binance_with_sltp:
|
||||
logger.info(f"跳过 {len(only_binance)} 个仅币安持仓的监控(SYNC_CREATE_MANUAL_ENTRY_RECORD=False 且 SYNC_MONITOR_BINANCE_POSITIONS_WITH_SLTP=False): {', '.join(only_binance)}")
|
||||
logger.info(f"[账号{self.account_id}] 跳过 {len(only_binance)} 个仅币安持仓的监控(SYNC_CREATE_MANUAL_ENTRY_RECORD=False 且 SYNC_MONITOR_BINANCE_POSITIONS_WITH_SLTP=False): {', '.join(only_binance)}")
|
||||
|
||||
for position in positions:
|
||||
symbol = position['symbol']
|
||||
|
|
@ -3516,9 +3516,9 @@ class PositionManager:
|
|||
if not should_create:
|
||||
continue
|
||||
if sync_create_manual:
|
||||
logger.warning(f"{symbol} 在币安有持仓但不在本地记录中,可能是手动开仓,尝试创建记录...")
|
||||
logger.warning(f"[账号{self.account_id}] {symbol} 在币安有持仓但不在本地记录中,可能是手动开仓,尝试创建记录...")
|
||||
else:
|
||||
logger.info(f"{symbol} 仅币安有仓且存在止损/止盈单,按系统单接入监控并补建记录")
|
||||
logger.info(f"[账号{self.account_id}] {symbol} 仅币安有仓且存在止损/止盈单,按系统单接入监控并补建记录")
|
||||
try:
|
||||
entry_price = position.get('entryPrice', 0)
|
||||
position_amt = position['positionAmt']
|
||||
|
|
@ -3597,7 +3597,7 @@ class PositionManager:
|
|||
except Exception as db_e:
|
||||
logger.debug(f"{symbol} 补建 DB 记录失败(不影响监控): {db_e}")
|
||||
self.active_positions[symbol] = position_info
|
||||
logger.info(f"{symbol} 已创建持仓记录用于监控" + (" (已写入 DB)" if position_info.get("tradeId") else ""))
|
||||
logger.info(f"[账号{self.account_id}] {symbol} 已创建持仓记录用于监控" + (" (已写入 DB)" if position_info.get("tradeId") else ""))
|
||||
# 也为“现有持仓”补挂交易所保护单(重启/掉线更安全)
|
||||
try:
|
||||
mp = None
|
||||
|
|
@ -3609,7 +3609,7 @@ class PositionManager:
|
|||
except Exception as e:
|
||||
logger.warning(f"{symbol} 补挂币安止盈止损失败(不影响监控): {e}")
|
||||
except Exception as e:
|
||||
logger.error(f"{symbol} 创建临时持仓记录失败: {e}")
|
||||
logger.error(f"[账号{self.account_id}] {symbol} 创建临时持仓记录失败: {e}")
|
||||
else:
|
||||
# 已在 active_positions 的持仓:启动前统一补挂/修正交易所 SL/TP(识别缺止盈、止损过远等异常并替换)
|
||||
position_info = self.active_positions.get(symbol)
|
||||
|
|
@ -3626,7 +3626,7 @@ class PositionManager:
|
|||
|
||||
await self._start_position_monitoring(symbol)
|
||||
|
||||
logger.info(f"已启动 {len(self._monitor_tasks)} 个持仓的实时监控")
|
||||
logger.info(f"[账号{self.account_id}] 已启动 {len(self._monitor_tasks)} 个持仓的实时监控")
|
||||
|
||||
async def stop_all_position_monitoring(self):
|
||||
"""
|
||||
|
|
@ -3656,7 +3656,7 @@ class PositionManager:
|
|||
try:
|
||||
task = asyncio.create_task(self._monitor_position_price(symbol))
|
||||
self._monitor_tasks[symbol] = task
|
||||
logger.info(f"✓ 启动 {symbol} WebSocket实时价格监控")
|
||||
logger.info(f"[账号{self.account_id}] ✓ 启动 {symbol} WebSocket实时价格监控")
|
||||
except Exception as e:
|
||||
logger.error(f"启动 {symbol} 监控失败: {e}")
|
||||
|
||||
|
|
@ -3693,7 +3693,7 @@ class PositionManager:
|
|||
while retry_count < max_retries:
|
||||
try:
|
||||
if symbol not in self.active_positions:
|
||||
logger.info(f"{symbol} 持仓已不存在,停止监控")
|
||||
logger.info(f"[账号{self.account_id}] {symbol} 持仓已不存在,停止监控")
|
||||
break
|
||||
|
||||
# 使用WebSocket订阅价格流
|
||||
|
|
@ -3709,7 +3709,7 @@ class PositionManager:
|
|||
|
||||
async for msg in ws:
|
||||
if symbol not in self.active_positions:
|
||||
logger.info(f"{symbol} 持仓已不存在,停止监控")
|
||||
logger.info(f"[账号{self.account_id}] {symbol} 持仓已不存在,停止监控")
|
||||
break
|
||||
|
||||
if msg.type == aiohttp.WSMsgType.TEXT:
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user