This commit is contained in:
薇薇安 2026-01-14 20:28:02 +08:00
parent bbc11b1bc9
commit 47a8c45a0b
2 changed files with 16 additions and 9 deletions

View File

@ -281,10 +281,13 @@ class MarketScanner:
# 使用标准WebSocket # 使用标准WebSocket
try: try:
if self.client.socket_manager: 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: async for msg in stream:
try: try:
# futures_ticker_socket 返回的数据格式:{'e': '24hrTicker', 's': 'BTCUSDT', 'c': '50000.00', ...} # WebSocket 返回的数据格式:{'e': '24hrTicker', 's': 'BTCUSDT', 'c': '50000.00', ...}
if 'c' in msg: # 'c' 是当前价格 if 'c' in msg: # 'c' 是当前价格
price = float(msg['c']) price = float(msg['c'])
await callback(symbol, price) await callback(symbol, price)

View File

@ -872,8 +872,11 @@ class PositionManager:
logger.info(f"{symbol} 持仓已不存在,停止监控") logger.info(f"{symbol} 持仓已不存在,停止监控")
break break
# 使用WebSocket订阅价格流使用futures_ticker_socket订阅单个交易对 # 使用WebSocket订阅价格流
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:
logger.debug(f"{symbol} WebSocket连接已建立开始接收价格更新") logger.debug(f"{symbol} WebSocket连接已建立开始接收价格更新")
retry_count = 0 # 连接成功,重置重试计数 retry_count = 0 # 连接成功,重置重试计数
@ -883,15 +886,16 @@ class PositionManager:
break break
try: try:
# futures_ticker_socket 返回的数据格式:{'e': '24hrTicker', 's': 'BTCUSDT', 'c': '50000.00', ...} # WebSocket 返回的数据格式:{'e': '24hrTicker', 's': 'BTCUSDT', 'c': '50000.00', ...}
if 'c' in msg: # 'c' 是当前价格 if 'c' in msg: # 'c' 是当前价格
current_price = float(msg['c']) current_price = float(msg['c'])
# 立即检查止损止盈 # 立即检查止损止盈
await self._check_single_position(symbol, current_price) await self._check_single_position(symbol, current_price)
elif 'data' in msg and 'c' in msg['data']: elif 'data' in msg:
# 兼容其他可能的数据格式 # 兼容嵌套的数据格式
current_price = float(msg['data']['c']) if 'c' in msg['data']:
await self._check_single_position(symbol, current_price) current_price = float(msg['data']['c'])
await self._check_single_position(symbol, current_price)
except (KeyError, ValueError, TypeError) as e: except (KeyError, ValueError, TypeError) as e:
logger.debug(f"{symbol} 解析价格数据失败: {e}, 消息: {msg}") logger.debug(f"{symbol} 解析价格数据失败: {e}, 消息: {msg}")
continue continue