Hey,
I am using websocket-client library to stream price data from Binance instantly.
When I first established the connection, all the data and values of seconds candles were being retrieved correctly. Then I stopped running the script multiple times to change somethings in my code.
THEN all the closing values of all the candles are wrong!!
Did I affect the connection (because I closed it by stopping the script) or is Binance sending wrong seconds Kline data?
note: it’s a seconds interval candle so all the candles are closed and there values can’t change.
Sometimes it retrieves back the right values and sometimes it receives wrong values. what is the reason for this inconsistency?
If you compare the retrieved data for every second with what is being displayed on Binance trading chart. You will notice that the open, high, low prices are all the same. But the close price is different between the two.
the code:
import websocket
import json
import datetime
def on_message(ws, message):
data = json.loads(message)
print(datetime.datetime.fromtimestamp(data['k']['t'] / 1000), ' ', datetime.datetime.now())
print('open: ', data['k']['o'], ' high: ', data['k']['h'], ' low: ', data['k']['l'], ' close: ', data['k']['c'])
def on_error(ws, error):
print(error)
def on_close(ws, close_status_code, close_msg):
print(close_msg)
def Get_Share_Data(symbol, interval):
socket = f'wss://stream.binance.com:443/ws/{symbol}@kline_{interval}'
ws = websocket.WebSocketApp(socket, on_message=on_message, on_error=on_error, on_close=on_close)
ws.run_forever()
Get_Share_Data('btcusdt', '1s')