'Get Futures Asset Balance Binance
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
Solution 1:[1]
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
Solution 2:[2]
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
Solution 3:[3]
This should give you the USDT balance
from the data structure if you are targetting USDT
. You can loop through as answered previously if not.
acc_balance = float(client.futures_account_balance()[6]['balance'])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | BrokenBenchmark |
Solution 2 | Varadharajan Raghavendran |
Solution 3 | Javad Nikbakht |