CyberPunkMetalHead
CyberPunkMetalHead

Reputation: 103

How can I store data coming from Binance Websocket?

I am currently trying to store some data streaming from a Binance Miniticker Websocket, but I can't figure out a way to do so. I would like to append the data to an existing dictionary, so that I can access it as historical data.

def miniticker_socket(msg):
    ''' define how to process incoming WebSocket messages '''
    if msg[0]['e'] != 'error':

        for item in msg:
            miniticker["{0}".format(item['s'])] = {'low': [],'high': [], 'open': [], 'close':[],'timestamp':[], 'symbol':[] }

            miniticker[item['s']]['close'].append(msg[msg.index(item)]['c'])

        print(miniticker[item['s']])
    else:
        print('there has been an issue')

bsm = BinanceSocketManager(client)
#ticker_key = bsm.start_symbol_ticker_socket(crypto, ticker_socket)
miniticker_key = bsm.start_miniticker_socket(miniticker_socket)
bsm.start()

The issue I'm having in the code above is that the data does not get appened, because every time the Websocket calls back the function, it also defines the dictionary as empty. I can't define the dictionary outside the Websocket because the name of the dictionary is given by the item['s'] element inside the socket.

I also tried returning the whole data and calling the callback function in another function but this generates another error saying "msg is not defined."

I would appreciate your feedback on this! Thanks

Upvotes: 2

Views: 1769

Answers (1)

Roman Zh.
Roman Zh.

Reputation: 1049

Also, you can try to check if a key already exists in the dictionary miniticker:

key = "{0}".format(item['s'])
if key not in miniticker.keys():
    miniticker["{0}".format(item['s'])] = {...}


So you will not redefine it as an empty dictionary each time

Upvotes: 1

Related Questions