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
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)

View File

@ -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,13 +886,14 @@ 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']:
# 兼容其他可能的数据格式
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: