I used youtube tutorial to write a simple code to download price of btc from binance. However if I run it it doesn’t give true value from the website and do not refresh fast. Do you know what can be wrong with that script?
<code>import websocket
import json
import pandas as pd
endpoint = 'wss://stream.binance.us:9443/ws'
our_msg = json.dumps({'method':'SUBSCRIBE',
'params':['btcusdt@ticker'],'id':1})
df = pd.DataFrame()
def on_open(ws):
ws.send(our_msg)
def on_message(ws,message):
global df, in_position, buyorders, sellorders
out = json.loads(message)
out = pd.DataFrame({'price':float(out['c'])}, index =[pd.to_datetime(out['E'], unit='ms')])
df = pd.concat([df,out],axis=0)
print(df)
ws = websocket.WebSocketApp(endpoint, on_message=on_message, on_open=on_open)
ws.run_forever()
</code>
<code>import websocket
import json
import pandas as pd
endpoint = 'wss://stream.binance.us:9443/ws'
our_msg = json.dumps({'method':'SUBSCRIBE',
'params':['btcusdt@ticker'],'id':1})
df = pd.DataFrame()
def on_open(ws):
ws.send(our_msg)
def on_message(ws,message):
global df, in_position, buyorders, sellorders
out = json.loads(message)
out = pd.DataFrame({'price':float(out['c'])}, index =[pd.to_datetime(out['E'], unit='ms')])
df = pd.concat([df,out],axis=0)
print(df)
ws = websocket.WebSocketApp(endpoint, on_message=on_message, on_open=on_open)
ws.run_forever()
</code>
import websocket
import json
import pandas as pd
endpoint = 'wss://stream.binance.us:9443/ws'
our_msg = json.dumps({'method':'SUBSCRIBE',
'params':['btcusdt@ticker'],'id':1})
df = pd.DataFrame()
def on_open(ws):
ws.send(our_msg)
def on_message(ws,message):
global df, in_position, buyorders, sellorders
out = json.loads(message)
out = pd.DataFrame({'price':float(out['c'])}, index =[pd.to_datetime(out['E'], unit='ms')])
df = pd.concat([df,out],axis=0)
print(df)
ws = websocket.WebSocketApp(endpoint, on_message=on_message, on_open=on_open)
ws.run_forever()
New contributor
lol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.