[英]How to query binance margin account for unique symbol balance?
I am currently able to query my margin account successfully using this code from python-binance v0.7.5:我目前可以使用 python-binance v0.7.5 中的代码成功查询我的保证金账户:
client.get_margin_account()
It returns this list of lists of margin balances and information which lists all symbols on the exchange, starting from highest to lowest balance:它返回此保证金余额列表和信息,其中列出了交易所上的所有交易品种,从最高余额到最低余额:
{'tradeEnabled': True,
'transferEnabled': True,
'borrowEnabled': True,
'marginLevel': '999.00000000',
'totalAssetOfBtc': '0.00000000',
'totalLiabilityOfBtc': '0.00000000',
'totalNetAssetOfBtc': '0.00000000',
'userAssets': [{'asset': 'BTC',
'free': '0.00000000',
'locked': '0.00000000',
'borrowed': '0.00000000',
'interest': '0.00000000',
'netAsset': '0.00000000'},
{'asset': 'ETH',
'free': '0.00000000',
'locked': '0.00000000',
'borrowed': '0.00000000',
'interest': '0.00000000',
'netAsset': '0.00000000'}]}
I can query specific assets as follows:我可以查询特定资产如下:
client.get_margin_account()['userAssets'][0]['free']
But when I use this code:但是当我使用这段代码时:
client.get_margin_account()['userAssets']['btc']['free']
I get this error:我收到此错误:
TypeError: list indices must be integers or slices, not str
How am I able to query specific symbols in my account without specifying index location?如何在不指定索引位置的情况下查询我账户中的特定交易品种?
Option 1: use next()
:选项 1:使用next()
:
data = {'tradeEnabled': True,
'transferEnabled': True,
'borrowEnabled': True,
'marginLevel': '999.00000000',
'totalAssetOfBtc': '0.00000000',
'totalLiabilityOfBtc': '0.00000000',
'totalNetAssetOfBtc': '0.00000000',
'userAssets': [{'asset': 'BTC',
'free': '0.00000000',
'locked': '0.00000000',
'borrowed': '0.00000000',
'interest': '0.00000000',
'netAsset': '0.00000000'},
{'asset': 'ETH',
'free': '0.00000000',
'locked': '0.00000000',
'borrowed': '0.00000000',
'interest': '0.00000000',
'netAsset': '0.00000000'}]}
print( next(d for d in data['userAssets'] if d['asset'] == 'BTC')['free'] )
Option 2: Convert userAssets to dictionary:选项 2:将 userAssets 转换为字典:
ua = {d['asset']: d for d in data['userAssets']}
print(ua['BTC']['free'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.