-
Notifications
You must be signed in to change notification settings - Fork 647
Description
I followed the official Binance documentation strictly:
Binance Documentation (Mark Price Kline Streams)
However, when I subscribe to @markprice@1s, I can receive data without any problem.
But when I subscribe to @markPriceKline_, there is no output at all.
Could you please clarify why this is happening? Is there a missing step or a bug in the SDK?
@markPriceKline_
https://developers.binance.com/docs/zh-CN/derivatives/coin-margined-futures/websocket-market-streams/Mark-Price-Kline-Candlestick-Streams
`import os
import json
import asyncio
import websockets
from dotenv import load_dotenv
dotenv_file = ".env.prod"
load_dotenv(dotenv_file)
API_KEY = os.getenv("BINANCE_API_KEY")
API_SECRET = os.getenv("BINANCE_API_SECRET")
async def mark_price_stream(symbol="bnbusdt", stream_type="markPriceKline_1m"):
"""
"""
url = f"wss://fstream.binance.com/ws/{symbol}@{stream_type}"
print(f"✅ 已连接到 {url}")
async with websockets.connect(url) as ws:
while True:
try:
message = await asyncio.wait_for(ws.recv(), timeout=60)
data = json.loads(message)
print(f"[{symbol.upper()}] 收到消息类型: {stream_type}")
# === 如果是K线流 ===
if "k" in data:
k = data["k"]
print(
f"🕒 [{symbol.upper()}] 开:{k['o']} 高:{k['h']} 低:{k['l']} 收:{k['c']} 收线:{k['x']}"
)
# === 如果是标记价格1s流 ===
elif "p" in data and "i" in data:
print(f"💰 [{symbol.upper()}] 标记价: {data['p']} 时间: {data['E']}")
else:
print(f"[{symbol.upper()}] 收到其他消息: {data}")
except asyncio.TimeoutError:
print(f"[{symbol.upper()}] ⏳ 等待中... 暂无新数据")
async def main():
await asyncio.gather(
# 每秒标记价格流
# mark_price_stream("btcusdt", "markPrice@1s"),
# 每分钟标记价格K线流
mark_price_stream("ethusdt", "markPriceKline_1m"),
mark_price_stream("ethusdt", "markPriceKline_1h"),
mark_price_stream("bnbusdt", "markPriceKline_1s"),
mark_price_stream("bnbusdt", "markPriceKline_15m"),
mark_price_stream("bnbusdt", "markPriceKline_15m"),
# 你可以添加更多币种
# mark_price_stream("bnbusdt", "markPrice@1s"),
)
if name == "main":
asyncio.run(main())
`