I am attempting to convert these nested dictionaries into DataFrames. I have scoured the web for more information on this but cannot find much on this particular situation. The issue I continue to run into is a "TypeError: string indices must be integers" error after attempting to convert using json.loads().
This is the coroutine I have to receive multiple sockets data so far.
import asyncio
import pandas as pd
from binance import AsyncClient, BinanceSocketManager
async def main():
client = await AsyncClient.create()
bm = BinanceSocketManager(client)
# start any sockets here, i.e a trade socket
ms = bm.multiplex_socket(['ltcusdt@kline_1m', 'btcusdt@kline_1m'])
# then start receiving messages
async with ms as mscm:
while True:
res = await mscm.recv()
print(res)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Output comes as nested dictionaries in which I would like to acquire only kline pricing data (nested dictionary as "k") and format it to pandas dataframes and store in HDF5 file as well as directly to Postgres Database where I currently have OHLCV tables created.
{'stream': 'btcusdt@kline_1m', 'data': {'e': 'kline', 'E': 1627406952750, 's': 'BTCUSDT', 'k': {'t': 1627406940000, 'T': 1627406999999, 's': 'BTCUSDT', 'i': '1m', 'f': 972731799, 'L': 972733284, 'o': '37761.23000000', 'c': '37753.06000000', 'h': '37761.24000000', 'l': '37745.74000000', 'v': '5.29347800', 'n': 1486, 'x': False, 'q': '199846.18941298', 'V': '1.87072500', 'Q': '70625.81836987', 'B': '0'}}}
{'stream': 'ltcusdt@kline_1m', 'data': {'e': 'kline', 'E': 1627406953532, 's': 'LTCUSDT', 'k': {'t': 1627406940000, 'T': 1627406999999, 's': 'LTCUSDT', 'i': '1m', 'f': 185284307, 'L': 185284315, 'o': '132.45000000', 'c': '132.41000000', 'h': '132.45000000', 'l': '132.40000000', 'v': '22.23228000', 'n': 9, 'x': False, 'q': '2944.17206090', 'V': '3.11654000', 'Q': '412.66106140', 'B': '0'}}}