[英]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.