I'm trying to run an example code for binance websocket api using python-binance and following this tutorial: https://livedataframe.com/live-cryptocurrency-data-python-tutorial/
Unfortunately i can't make it work. The websocket just doesn't respond what results in empty terminal without anything printed out.
I'm using python 3.7.8, PyCharm IDE, Windows 10
Please help, thanks!
import time
from binance.client import Client # Import the Binance Client
from binance.websockets import BinanceSocketManager # Import the Binance Socket Manager
_API_KEY = "mykey"
_API_SECRET = "mykey"
client = Client(_API_KEY, _API_SECRET)
# Instantiate a BinanceSocketManager, passing in the client that you instantiated
bm = BinanceSocketManager(client)
# This is our callback function. For now, it just prints messages as they come.
def handle_message(msg):
print(msg)
# Start trade socket with 'ETHBTC' and use handle_message to.. handle the message.
conn_key = bm.start_trade_socket('ETHBTC', handle_message)
# then start the socket manager
bm.start()
# let some data flow..
time.sleep(10)
# stop the socket manager
bm.stop_socket(conn_key)
In addition i can run this code, but websocket api doesn't seem to work for me. Regards.
from binance.client import Client
_API_KEY = "key"
_API_SECRET = "key"
client = Client(_API_KEY, _API_SECRET)
btc_price = client.get_symbol_ticker(symbol="BTCUSDT")
# print full output (dictionary)
print(btc_price)
I have removed erroneous information that I had unintentionally posted here. The rest of the post unfortunately no longer makes as much sense as before :/
Rest of the post without erroneous information:
To do that you can use unicorn-binance-websocket-api:
pip install unicorn-binance-websocket-api
Create a websocket connection to Binance:
from unicorn_binance_websocket_api.unicorn_binance_websocket_api_manager import BinanceWebSocketApiManager
binance_websocket_api_manager = BinanceWebSocketApiManager(exchange="binance.com")
binance_com_websocket_api_manager.create_stream('arr', '!userData', api_key=binance_com_api_key, api_secret=binance_com_api_secret)
And 4 more lines to print the received data records:
while True:
oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
if oldest_stream_data_from_stream_buffer:
print(oldest_stream_data_from_stream_buffer)
Just try this example: https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_userdata_stream.py