diff --git a/trading_system/market_scanner.py b/trading_system/market_scanner.py index 9d52606..575f5c5 100644 --- a/trading_system/market_scanner.py +++ b/trading_system/market_scanner.py @@ -281,10 +281,13 @@ class MarketScanner: # 使用标准WebSocket try: if self.client.socket_manager: - async with self.client.socket_manager.futures_ticker_socket(symbol.lower()) as stream: + # python-binance 中,futures_socket() 需要传递 WebSocket 路径 + # 单个交易对的 ticker 流路径格式:f"{symbol.lower()}@ticker" + ws_path = f"{symbol.lower()}@ticker" + async with self.client.socket_manager.futures_socket(ws_path) as stream: async for msg in stream: try: - # futures_ticker_socket 返回的数据格式:{'e': '24hrTicker', 's': 'BTCUSDT', 'c': '50000.00', ...} + # WebSocket 返回的数据格式:{'e': '24hrTicker', 's': 'BTCUSDT', 'c': '50000.00', ...} if 'c' in msg: # 'c' 是当前价格 price = float(msg['c']) await callback(symbol, price) diff --git a/trading_system/position_manager.py b/trading_system/position_manager.py index be8a695..e450bf0 100644 --- a/trading_system/position_manager.py +++ b/trading_system/position_manager.py @@ -872,8 +872,11 @@ class PositionManager: logger.info(f"{symbol} 持仓已不存在,停止监控") break - # 使用WebSocket订阅价格流(使用futures_ticker_socket订阅单个交易对) - async with self.client.socket_manager.futures_ticker_socket(symbol.lower()) as stream: + # 使用WebSocket订阅价格流 + # python-binance 中,futures_socket() 需要传递 WebSocket 路径 + # 单个交易对的 ticker 流路径格式:f"{symbol.lower()}@ticker" + ws_path = f"{symbol.lower()}@ticker" + async with self.client.socket_manager.futures_socket(ws_path) as stream: logger.debug(f"{symbol} WebSocket连接已建立,开始接收价格更新") retry_count = 0 # 连接成功,重置重试计数 @@ -883,15 +886,16 @@ class PositionManager: break try: - # futures_ticker_socket 返回的数据格式:{'e': '24hrTicker', 's': 'BTCUSDT', 'c': '50000.00', ...} + # WebSocket 返回的数据格式:{'e': '24hrTicker', 's': 'BTCUSDT', 'c': '50000.00', ...} if 'c' in msg: # 'c' 是当前价格 current_price = float(msg['c']) # 立即检查止损止盈 await self._check_single_position(symbol, current_price) - elif 'data' in msg and 'c' in msg['data']: - # 兼容其他可能的数据格式 - current_price = float(msg['data']['c']) - await self._check_single_position(symbol, current_price) + elif 'data' in msg: + # 兼容嵌套的数据格式 + if 'c' in msg['data']: + current_price = float(msg['data']['c']) + await self._check_single_position(symbol, current_price) except (KeyError, ValueError, TypeError) as e: logger.debug(f"{symbol} 解析价格数据失败: {e}, 消息: {msg}") continue