How can I store data coming from Binance Websocket?

Viewed 1201

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

2 Answers

I think what you might want is a global variable dictionary containing dictionary values that come in from the ticker. You will need something unique for the keys of the global dictionary.

For example, you could use a string datetime:

timestamp_key = datetime.datetime.now().isoformat()

global_dict[timestamp_key] = miniticker["{0}".format(item['s'])] = {'low': [],'high': [], 'open': [], 'close':[],'timestamp':[], 'symbol':[] }
global_dict[timestamp_key][item['s']]['close'].append(msg[msg.index(item)]['c'])

The global dict would end up something like this:

{
  "2020-03-25T17:14:19.382748": {
    "your_data_key1": { "more": "data" }
  },
  "2021-03-25T17:15:19.249148": {
    "your_data_key1": { "more": "data_from_another_update" }
  }
}

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

Related