-2

I've reviewed this post: How to key into deeply nested dictionary and list structure and attempted to break down this list structure, however I continue to fail as this example has the final "me" in [] while my problem does not :(

session.my_position(symbol="BTCUSDT")

{'ret_code': 0, 'ret_msg': 'OK', 'ext_code': '', 'ext_info': '', 'result': [{'user_id': 3424844, 'symbol': 'BTCUSDT', 'side': 'Buy', 'size': 0, 'position_value': 0, 'entry_price': 0, 'liq_price': 0, 'bust_price': 0, 'leverage': 1, 'auto_add_margin': 0, 'is_isolated': True, 'position_margin': 0, 'occ_closing_fee': 0, 'realised_pnl': -0.2655184, 'cum_realised_pnl': -973.98619164, 'free_qty': 0, 'tp_sl_mode': 'Full', 'unrealised_pnl': 0, 'deleverage_indicator': 0, 'risk_id': 1, 'stop_loss': 0, 'take_profit': 0, 'trailing_stop': 0, 'position_idx': 1, 'mode': 'BothSide'}, {'user_id': 3424844, 'symbol': 'BTCUSDT', 'side': 'Sell', 'size': 0, 'position_value': 0, 'entry_price': 0, 'liq_price': 0, 'bust_price': 0, 'leverage': 1, 'auto_add_margin': 0, 'is_isolated': True, 'position_margin': 0, 'occ_closing_fee': 0, 'realised_pnl': -0.3662503, 'cum_realised_pnl': -1121.1239092, 'free_qty': 0, 'tp_sl_mode': 'Full', 'unrealised_pnl': 0, 'deleverage_indicator': 0, 'risk_id': 1, 'stop_loss': 0, 'take_profit': 0, 'trailing_stop': 0, 'position_idx': 2, 'mode': 'BothSide'}], 'time_now': '1655352862.461627', 'rate_limit_status': 116, 'rate_limit_reset_ms': 1655352862459, 'rate_limit': 120}

I believe it should be:

session.my_position(symbol="BTCUSDT")['result']['entry_price']

however it returns:

Traceback (most recent call last): File "", line 1, in TypeError: list indices must be integers or slices, not str

I've also tried:

session.my_position(symbol="BTCUSDT")['result'][2][0]
session.my_position(symbol="BTCUSDT")['result'][1][0]
session.my_position(symbol="BTCUSDT")['result'][0][0]
session.my_position(symbol="BTCUSDT")['result'][0][2]

I just can't figure out how to get down to that next level below result :( Please help.

Doug
  • 1
  • 3
    Do it one step at a time, and check what the type is at each step: `session.my_position(symbol="BTCUSDT")` gives you a `dict`, `session.my_position(symbol="BTCUSDT")['result']` is a `list`, `session.my_position(symbol="BTCUSDT")['result'][0]` is the first element of that list, and a `dict` again, etc. – Grismar Jun 16 '22 at 06:00
  • Why do you think "believing" a solution would get the code to do what you want? You need to figure out exactly what data you are dealing with at every level down, just print out one value at a time as Grismar did. The error also clearly told you that the value was a list, and it must be provided with an integer. When in doubt, print each step of the way like what the other answer did. – metatoaster Jun 16 '22 at 06:25
  • I never tried the ['result'][0] to see the first element of the list, and then seeing that ['result'][1] is the 2nd element - now I can see why the 3rd "level" is a dict again that can use string. I feel like I tried every other variation and continued to read and try to assign my array to int, etc. due to the error message and other google searches. Thank you very much for taking the time to explain Grismar and Kilian - two answers that enabled me to get the ah-ha moment I spent hours looking for last night. (and without being snarky like other basement dwellers :) – Doug Jun 16 '22 at 13:21

1 Answers1

0

It looks like the key result corresponds to an array of dict. In that case, you would have to access one of the array fields, e.g. the first one using: session.my_position(symbol="BTCUSDT")['result'][0] This would be one of the dictionaries contained in the result array.

What's left is for you to access any keys just like before, e.g. user_id: session.my_position(symbol="BTCUSDT")['result'][0]['user_id']

The trick here is to distinguish between dict and array.

Kilian
  • 1