如何解决将 binance websocket 消息传递给 Python 中的事件队列
我正在编写一个 python 脚本,通过 websocket 模块从 binace 获取刻度数据。我的数据流类是这样的:
<stream.py>
from datetime import datetime
import json,websocket,time
from decimal import Decimal,getcontext,ROUND_HALF_DOWN
from event import TickEvent
class StreamingForexPrices:
def __init__(
self,events_queue
):
self.events_queue = events_queue
self.prices = dict()
self.websocket = websocket
self.socket = f'wss://stream.binance.com:9443/ws/btcusdt@ticker/ethbtc@ticker/bnbbtc@ticker/wavesbtc@ticker/stratbtc@ticker/ethup@ticker/yfiup@ticker/xrpup@ticker'
self.websocket.enableTrace(False)
self.ws = self.websocket.WebSocketApp(
self.socket,on_message=self.on_message,on_close=self.on_close)
def on_close(self,ws,message):
print("bang")
def on_message(self,message):
data = json.loads(message)
self.timestamp = datetime.utcfromtimestamp(data['E']/1000).strftime('%Y-%m-%d %H:%M:%s')
self.instrument = data['s']
open = data['o']
high = data['h']
low = data['l']
close = data['c']
volume = data['v']
Trade = data['n'] #No. of Trades
# print("testing")
self.prices[self.instrument]["open"] = open
self.prices[self.instrument]["high"] = high
self.prices[self.instrument]["low"] = low
self.prices[self.instrument]["close"] = close
self.prices[self.instrument]["volume"] = volume
self.prices[self.instrument]["Trade"] = Trade
tev = TickEvent(self.instrument,self.timestamp,open,high,low,close,volume,Trade)
self.events_queue.put(tev)
self.pp = f'tick :timestamp: {self.timestamp} :symbol: {self.instrument} :close_price: {self.prices[self.instrument]["close"]} :volume: {self.prices[self.instrument]["volume"]} :open_price:{self.prices[self.instrument]["open"]}:high_price: {self.prices[self.instrument]["high"]}:low_price: {self.prices[self.instrument]["low"]}:Trade_qyt: {self.prices[self.instrument]["Trade"]}'
<event.py>
class TickEvent(Event):
def __init__(self,instrument,time,Trade):
self.type = 'TICK'
self.instrument = instrument
self.time = time
self.open = open
self.high = high
self.low = low
self.close = close
self.high = high
self.volume = volume
self.Trade = Trade
# print(self.type,self.instrument,self.open,self.close,self.high)
def __str__(self):
return "Type: %s,Instrument: %s,Time: %s,open: %s,high: %s,low: %s,close: %s,volume: %s,Trade: %s" % (
str(self.type),str(self.instrument),str(self.time),str(self.open),str(self.high),str(self.low),str(self.close),str(self.volume),str(self.Trade)
)
def __repr__(self):
return str(self)
<main.py>
from stream import StreamingForexPrices as st
import threading
import time
import queue
events = queue.Queue()
prices = st(events)
wst = threading.Thread(target=prices.ws.run_forever,args=[])
wst.daemon = True
wst.start()
# conn_timeout = 15
while not prices.ws.sock.connected:
time.sleep(1)
print(prices.ws.sock)
# conn_timeout -= 1
while prices.ws.sock is not None:
# print(prices.ws.sock)
print(prices.events_queue.get())
print(self.pp)
time.sleep(10)
我正在尝试将接收到的刻度传递到 Tickevent 队列,但似乎我的代码没有通过事件队列打印/生成/发送接收到的刻度到主文件。它只是在无限循环中运行并不断生成空列表而不会显示任何错误。 请帮助我解决这个问题,任何见解都会有所帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。