簡體   English   中英

不能 stream 與 python-binance

[英]can't stream with python-binance

我嘗試在某些行上做一個簡單的 stream 但我收到一條錯誤消息。 我可以注意到,根據版本的不同,websocket 調用是不同的。 我的版本是 Python-binance 1.0.15

你可以幫幫我嗎?

# Importing libraries
from binance.client import Client
import configparser
from binance.streams import ThreadedWebsocketManager


# Loading keys from config file
config = configparser.ConfigParser()
config.read_file(open('secret.cfg'))
actual_api_key = config.get('BINANCE', 'ACTUAL_API_KEY')
actual_secret_key = config.get('BINANCE', 'ACTUAL_SECRET_KEY')

client = Client(actual_api_key, actual_secret_key, tld="com")


def stream_data(msg):
    """
    Function to process the received messages
    param msg: input message
    """
    print(f"message type: {msg['e']}")
    print(f"close price: {msg['c']}")
    print(f"best ask price: {msg['a']}")
    print(f"best bid price: {msg['b']}")
    print("---------------------------")


# Starting the WebSocket
twm = ThreadedWebsocketManager()
twm.start()

#Subscribe to the stream
twm.start_symbol_miniticker_socket(callback=stream_data, symbol="BTCUSDT")

# Stopping websocket
twm.stop()

這里的錯誤信息:

取消讀取循環

進程以退出代碼 0 結束

應解決以下部分:

  • ThreadedWebsocketManager 應使用 API Key/Secret 初始化
  • 在這種情況下不需要客戶端調用
  • 開始流式傳輸后,您應該加入發生流式傳輸的 twm

# Importing libraries
from binance.client import Client
import configparser
from binance.streams import ThreadedWebsocketManager

# Loading keys from config file
config = configparser.ConfigParser()
config.read_file(open('secret.cfg'))
actual_api_key = config.get('BINANCE', 'ACTUAL_API_KEY')
actual_secret_key = config.get('BINANCE', 'ACTUAL_SECRET_KEY')

# This is not REQUIRED
# client = Client(actual_api_key, actual_secret_key, tld="com")


def stream_data(msg):
    """
    Function to process the received messages
    param msg: input message
    """
    print(f"message type: {msg['e']}")
    print(f"close price: {msg['c']}")
    print(f"best ask price: {msg['a']}")
    print(f"best bid price: {msg['b']}")
    print("---------------------------")


# Starting the WebSocket
twm = ThreadedWebsocketManager(api_key=actual_api_key, api_secret=actual_secret_key)
twm.start()

# Define a stream or two
streams = ['ethusdt@kline_1m', 'btc@kline_1m']

# Starting Binance multiplex_socket on ThreadedWebsocketManager
twm.start_multiplex_socket(callback=stream_data, streams=streams)

timeout = 0.1 # seconds
while True and twm.is_alive():
    twm.join(timeout=timeout) # Give twm a call to process the streaming
    # Do other stuff here

# Stopping websocket
twm.stop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM