我正在尝试使用Web套接字实时流化Binance交易所上所有现货交易对的每笔交易。我已经搜索了一个websocket端点,但是没有找到一个。 我尝试拨打“ 交易所信息 ”端点,并获取所有交易对。然后从那里过滤掉那些交易状态为暂停的交易对和那些不允许现货交易的交易对。然后,我通过了这些交易对,将它们转换为小写并以适当的“ @trade”缩写,以表示我想要交易信息。当我将此请求发送到服务器时,Binance没有响应。我在下面留了我的代码供您重现我的问题:
import ssl
import websocket
import _thread as thread
import re
import ast
import pandas as pd
import requests
import json
def on_message(ws, message):
print(message)
message = json.loads(message)
message = message.get('data')
try:
symbol = message.get("s")
timestamp = message.get("T")
price = message.get("p")
quantity = message.get("q")
is_buyer_maker = message.get("m")
if is_buyer_maker:
taker = "sell"
else:
taker = "buy"
data = {"symbol": symbol,
"timestamp": timestamp,
"price": price,
"quantity": quantity,
"taker": taker}
print(data)
except:
pass
def on_error(ws, error):
print(error)
def on_close(ws):
print("### Binance Closed ###")
def on_open(ws):
def run(*args):
print(logon_msg)
ws.send(logon_msg)
thread.start_new_thread(run, ())
if __name__ == "__main__":
content = requests.get("https://api.binance.com/api/v3/exchangeInfo").json()
content = content.get("symbols")
symbols = []
for thing in content:
if thing.get("status") == "TRADING" and thing.get("isSpotTradingAllowed") == True:
symbols.append(thing.get("symbol"))
symbols = [i.lower() + "@trade" for i in symbols]
logon_msg = {"method": "SUBSCRIBE", "params": symbols,"id": 1}
logon_msg = json.dumps(logon_msg)
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://stream.binance.com:9443/stream?streams=trade",
on_message=on_message,
on_error=on_error,
on_close=on_close,
on_open=on_open)
ws.on_open = on_open
ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})