New library for market data streaming in python

Viewed 18

so im receiving data in a dictionary format through this code:

import websocket
import unicorn_binance_websocket_api as uni
from unicorn_binance_websocket_api import BinanceWebSocketApiManager as ufi
import unicorn_fy
from unicorn_fy import UnicornFy as u
def process_new_receives(stream_data, stream_buffer_name=False):
    import pprint
    import time
    import json
    j = json.loads(stream_data)
    j = dict(j)
    print(j)
    # global buffer_btc
    


ubwa = ufi(exchange="binance.com")
ubwa.create_stream(['kline_1m'], ['btcusdt'], process_stream_data=process_new_receives)

and the output is a dictionary like this:

{'stream': 'btcusdt@kline_1m', 'data': {'e': 'kline', 'E': 1663187432811, 's': 'BTCUSDT', 'k': {'t': 1663187400000, 'T': 1663187459999, 's': 'BTCUSDT', 'i': '1m', 'f': 1816737629, 'L': 1816739685, 'o': '19937.12000000', 'c': '19921.89000000', 'h': '19938.21000000', 'l': '19921.30000000', 'v': '107.80139000', 'n': 2057, 'x': False, 'q': '2148406.68642610', 'V': '32.02372000', 'Q': '638222.57046930', 'B': '0'}}}

and i want the data only of the the 'data' key of j

so my new code change into :

def process_new_receives(stream_data, stream_buffer_name=False):
    import pprint
    import time
    import json
    j = json.loads(stream_data)
    j = dict(j)
    print(j['data'])

but its not able to get the dictionary key and the error is:

File "C:\Users\a****\AppData\Local\Temp/ipykernel_5092/158826288.py", line 12, in process_new_receives KeyError: 'data'

what should i change?

0 Answers
Related