I’m trying to get some parameters of my orders from Python-Binance API. In this case, I would like to get only the ‘status’ of an order and save it into a variable.
I used this to get my last order :
JavaScript
x
2
1
orders = client.get_all_orders(symbol=symbol, limit=1)
2
And I get this result:
JavaScript
1
19
19
1
[{'clientOrderId': 'HzHxjogJf5rXrzD2uFnTyMn',
2
'cummulativeQuoteQty': '12.06757100',
3
'executedQty': '0.00030000',
4
'icebergQty': '0.00000000',
5
'isWorking': True,
6
'orderId': 88978639302,
7
'orderListId': -1,
8
'origQty': '0.00030000',
9
'origQuoteOrderQty': '0.00000000',
10
'price': '31558.57000000',
11
'side': 'BUY',
12
'status': 'FILLED',
13
'stopPrice': '31592.06000000',
14
'symbol': 'BTCUSDT',
15
'time': 1612653434918,
16
'timeInForce': 'GTC',
17
'type': 'STOP_LOSS_LIMIT',
18
'updateTime': 1612109872451}]
19
Tried to print just the status:
JavaScript
1
2
1
pprint.pprint(orders['status'])
2
But it returns an error:
JavaScript
1
2
1
TypeError: list indices must be integers or slices, not str
2
Advertisement
Answer
Try this:
pprint.pprint(orders[0]['status'])
order
variable is a list that contains dictionary. To get access to first element of the list use [0]
. Now you only get value from dictionary. You can do this using [dictionary_key] in this example ['status']
. Combining this you can print status.
If you are not sure which type is your variable use simply print(type(variable_name))