I tried searching for answers, but to no avail.
How do I get the balance of specific asset for a Futures Asset, for example, USDT?
[{'accountAlias': 'xx', 'asset': 'DOT', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}, {'accountAlias': 'xx', 'asset': 'BTC', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}, {'accountAlias': 'xx', 'asset': 'SOL', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}, {'accountAlias': 'xx', 'asset': 'BNB', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}, {'accountAlias': 'xx', 'asset': 'ETH', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}, {'accountAlias': 'xx', 'asset': 'ADA', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}, {'accountAlias': 'xx', 'asset': 'USDT', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}, {'accountAlias': 'xx', 'asset': 'XRP', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}, {'accountAlias': 'xx', 'asset': 'BUSD', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}]
My code:
acc_balance = client.futures_account_balance()
print(acc_balance)
for check_balance in acc_balance["accountAlias"]:
if check_balance["asset"] == "USDT":
usdt_balance = check_balance["balance"]
Error:
for check_balance in acc_balance["accountAlias"]:
TypeError: list indices must be integers or slices, not str
What's the mistake here? Thanks
CodePudding user response:
acc_balance
is a list, not a dictionary. You shouldn't index into it using a string.
Fixing this, we get the following:
for check_balance in acc_balance:
if check_balance["asset"] == "USDT":
usdt_balance = check_balance["balance"]
print(usdt_balance) # Prints 0.0000
CodePudding user response:
This means you cannot access your acc_balance list by a str index like "accountAlias". Usually for lists you can only refer to the index by int. However in your case you are using a dictionary in a list
[{'accountAlias': 'xx', 'asset': 'DOT', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}, {'accountAlias': 'xx', 'asset': 'BTC', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}, {'accountAlias': 'xx', 'asset': 'SOL', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}, {'accountAlias': 'xx', 'asset': 'BNB', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}, {'accountAlias': 'xx', 'asset': 'ETH', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}, {'accountAlias': 'xx', 'asset': 'ADA', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}, {'accountAlias': 'xx', 'asset': 'USDT', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}, {'accountAlias': 'xx', 'asset': 'XRP', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}, {'accountAlias': 'xx', 'asset': 'BUSD', 'balance': '0.00000000', 'withdrawAvailable': '0.00000000', 'updateTime': 0}]
So you can make this,
acc_balance = client.futures_account_balance()
as
acc_balance = client.futures_account_balance()[0]
such that your acc_balance is a dictionary and not a list anymore