This commit is contained in:
薇薇安 2026-01-14 20:11:49 +08:00
parent d0d40b8f83
commit bbc11b1bc9
2 changed files with 25 additions and 12 deletions

View File

@ -281,11 +281,19 @@ class MarketScanner:
# 使用标准WebSocket
try:
if self.client.socket_manager:
async with self.client.socket_manager.futures_socket(symbol.lower()) as stream:
async with self.client.socket_manager.futures_ticker_socket(symbol.lower()) as stream:
async for msg in stream:
if 'data' in msg:
price = float(msg['data']['c']) # 最新价格
await callback(symbol, price)
try:
# futures_ticker_socket 返回的数据格式:{'e': '24hrTicker', 's': 'BTCUSDT', 'c': '50000.00', ...}
if 'c' in msg: # 'c' 是当前价格
price = float(msg['c'])
await callback(symbol, price)
elif 'data' in msg and 'c' in msg['data']:
price = float(msg['data']['c'])
await callback(symbol, price)
except (KeyError, ValueError, TypeError) as e:
logger.debug(f"解析 {symbol} 价格数据失败: {e}")
continue
except Exception as e:
logger.error(f"监控 {symbol} 价格失败: {e}")

View File

@ -872,8 +872,8 @@ class PositionManager:
logger.info(f"{symbol} 持仓已不存在,停止监控")
break
# 使用WebSocket订阅价格流
async with self.client.socket_manager.futures_socket(symbol.lower()) as stream:
# 使用WebSocket订阅价格流使用futures_ticker_socket订阅单个交易对
async with self.client.socket_manager.futures_ticker_socket(symbol.lower()) as stream:
logger.debug(f"{symbol} WebSocket连接已建立开始接收价格更新")
retry_count = 0 # 连接成功,重置重试计数
@ -882,14 +882,19 @@ class PositionManager:
logger.info(f"{symbol} 持仓已不存在,停止监控")
break
if 'data' in msg:
try:
current_price = float(msg['data']['c']) # 最新价格
try:
# futures_ticker_socket 返回的数据格式:{'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)
except (KeyError, ValueError, TypeError) as e:
logger.debug(f"{symbol} 解析价格数据失败: {e}")
continue
elif 'data' in msg and '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
except asyncio.CancelledError:
logger.info(f"{symbol} 监控任务已取消")