I need to return a list of symbols only if they have 'position_value' > 0. From there I want to get more data corresponding to the symbol like side, unrealised_pnl etc, possibly into some easily readable format like a pandas dataframe.
So far I use this to get the whole JSON response, but the moment I move beyond 'result' by adding ['data'], I encounter the error "TypeError: list indices must be integers or slices, not str"
def get_positions():
res = session_auth_unlocked.my_position()['result']
return res
print(get_positions())
I understand I must use a for loop and have tried like so but I am getting stuck.
def get_positions():
res = session_auth_unlocked.my_position()
for item in res['result']['data']:
if value > 0:
positions[item['symbol']] = value
return positions
print(get_positions())
This is the JSON API response for just one symbol but there are many more in the whole response.
{
"ret_code": 0,
"ret_msg": "OK",
"ext_code": "",
"ext_info": "",
"result": [
{
"data": {
"user_id": 533285,
"symbol": "10000NFTUSDT",
"side": "Sell",
"size": 0,
"position_value": 0,
"entry_price": 0,
"liq_price": 0,
"bust_price": 0,
"leverage": 10,
"auto_add_margin": 0,
"is_isolated": false,
"position_margin": 0,
"occ_closing_fee": 0,
"realised_pnl": 0,
"cum_realised_pnl": 0,
"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"
},
"is_valid": true
},
...
{
"data": {
"user_id": 533285,
"symbol": "10000NFTUSDT",
"side": "Buy",
"size": 0,
"position_value": 0,
"entry_price": 0,
"liq_price": 0,
"bust_price": 0,
"leverage": 10,
"auto_add_margin": 0,
"is_isolated": false,
"position_margin": 0,
"occ_closing_fee": 0,
"realised_pnl": 0,
"cum_realised_pnl": 0,
"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"
},
"is_valid": true
}
],
"time_now": "1604302080.356538",
"rate_limit_status": 119,
"rate_limit_reset_ms": 1604302080353,
"rate_limit": 120}