Home > database >  Can I access specific parts of this dictionary where the keys are the same?
Can I access specific parts of this dictionary where the keys are the same?

Time:06-22

I'm accessing an API and retrieving some data from the API as a dictionary where the keys are all the same. Code below.

order_book = session.orderbook(symbol="ETHUSDT")
print(order_book)

This returns the below dictionary. A Range of prices, the symbol name (which is always the same), how many contracts are available at the price and the side available as buy or sell.

{'ret_code': 0, 'ret_msg': 'OK', 'ext_code': '', 'ext_info': '', 'result': [{'symbol': 'ETHUSDT', 'price': '1125.55', 'size': 99.62, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1125.5', 'size': 4.93, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1125.45', 'size': 6.45, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1125.4', 'size': 6.39, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1125.35', 'size': 32.81, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1125.3', 'size': 17.89, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1125.25', 'size': 4.41, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1125.2', 'size': 26.39, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1125.15', 'size': 15.35, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1125.1', 'size': 23.01, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1125.05', 'size': 37.36, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1125', 'size': 62.45, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1124.95', 'size': 0.01, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1124.9', 'size': 6.25, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1124.85', 'size': 12.03, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1124.8', 'size': 48.02, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1124.75', 'size': 10, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1124.7', 'size': 0.01, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1124.65', 'size': 4.6, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1124.6', 'size': 4.17, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1124.55', 'size': 24.44, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1124.5', 'size': 16.72, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1124.45', 'size': 2.68, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1124.4', 'size': 8.87, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1124.35', 'size': 135.25, 'side': 'Buy'}, {'symbol': 'ETHUSDT', 'price': '1125.6', 'size': 21.38, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1125.7', 'size': 0.02, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1125.75', 'size': 9.46, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1125.8', 'size': 0.01, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1125.85', 'size': 29.1, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1125.9', 'size': 4.26, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1125.95', 'size': 14.53, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126', 'size': 13.9, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126.05', 'size': 21.66, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126.1', 'size': 39.36, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126.15', 'size': 24.7, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126.2', 'size': 9.07, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126.25', 'size': 4.08, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126.3', 'size': 49.79, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126.35', 'size': 9.66, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126.4', 'size': 17.13, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126.45', 'size': 11.72, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126.5', 'size': 4.55, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126.55', 'size': 4.58, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126.6', 'size': 124.11, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126.65', 'size': 1.86, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126.75', 'size': 18.47, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126.8', 'size': 3.42, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126.85', 'size': 18.46, 'side': 'Sell'}, {'symbol': 'ETHUSDT', 'price': '1126.9', 'size': 27.71, 'side': 'Sell'}], 'time_now': '1655855941.823052'}

So I am trying to figure out how I can access specific contract size for specific prices when the keys are the same? For example, the first 5 buy side and the first 5 sell side contract size, or even individual contract sizes based on specific prices.

I have tried googling this but I'm not even sure what to google to be honest.

Any help would be vey much appreciated. Even some idea on what I could search to figure it out myself?

Thanks in advance for any help.

Edit:

okay so I'm trying to use the code posted below to find the largest value contract size of the buy side

transactions = [trans for trans in order_book['result'] if trans['size'] == max and trans['side'] == 'Sell']print(transactions)

I'm receiving the following error

TypeError                                 Traceback (most recent call last)~\AppData\Local\Temp/ipykernel_15204/2875214528.py in <module>
 12 order_book = [session.orderbook(symbol="ETHUSDT")]
 13 ---> 14 transactions = [trans for trans in order_book['result'] if trans['size'] == max and trans['side'] == 'Sell']
 15 print(transactions)TypeError: list indices must be integers or slices, not str

again any help is very much appreciated :)

CodePudding user response:

You can use the same approach as example below:

def find_contract_size(price):
    return [result['size'] for result in order_book['result']  if result['price'] == price]

>>> print(find_contract_size('1125.45'))
>>> [6.45]

CodePudding user response:

I assume you mean result, which is a list of dictionaries, rather than a dictionary.

The lazy way would be with a list comprehension

transactions = [trans for trans in order_book['result'] if trans['size'] > 30]
transactions = [trans for trans in order_book['result'] if trans['side'] == 'Buy']
transactions = [trans for trans in order_book['result'] if float(trans['price']) < 1125.4 and trans['side'] == 'Sell']
  • Related