[英]How to query binance margin account for unique symbol balance?
我目前可以使用 python-binance v0.7.5 中的代码成功查询我的保证金账户:
client.get_margin_account()
它返回此保证金余额列表和信息,其中列出了交易所上的所有交易品种,从最高余额到最低余额:
{'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'}]}
我可以查询特定资产如下:
client.get_margin_account()['userAssets'][0]['free']
但是当我使用这段代码时:
client.get_margin_account()['userAssets']['btc']['free']
我收到此错误:
TypeError: list indices must be integers or slices, not str
如何在不指定索引位置的情况下查询我账户中的特定交易品种?
选项 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'] )
选项 2:将 userAssets 转换为字典:
ua = {d['asset']: d for d in data['userAssets']}
print(ua['BTC']['free'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.