bmoscon / cryptostore

A scalable storage service for cryptocurrency data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong Closing Values of klines (candle data) from Websocket-client library with Binance Stream!

Zain19900 opened this issue · comments

Hey,
I just started learning about websockets and wanted to use it in getting data from Binance.
At the beginning all the data and values of candles were being retrieved correctly. then I closed the connection multiple times to change somethings in my code. AND all of a sudden the closing values of all the candles are wrong!! I was using Binance REST API to get this data before and changed to Websocket-client just to get more accurate data but for some reason the closing values are wrong!!
I figured you guys can help based on previous raised questions here.
I want to understand what is happening and why am i getting wrong close values?? 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?

here is the code I wrote:


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(close_msg):
    print(close_msg)


def Get_Share_Data(symbol, interval):

    socket = f'wss://stream.binance.com:9443/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')

I would appreciate your guidance guys..

seems wholly not related to this project.