本文主要是介绍3.1 Binance_interface APP U本位合约交易账户,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Binance_interface APP U本位合约交易账户
- Github地址
- PyTed量化交易研究院
量化交易研究群(VX) = py_ted
目录
- Binance_interface APP U本位合约交易账户
- 1. APP U本位合约交易账户函数总览
- 2. 模型实例化
- 3. 获取账户信息 get_account
- 4. 获取单个产品余额 get_balance
- 5. 获取全部产品余额(列表格式) get_balances
- 6. 获取全部产品余额(字典格式)
- 7. 调整开仓杠杆 set_leverage
- 8. 更改持仓模式 set_marginType
- 9. 获取单个产品的持仓信息 get_position
- 10. 获取全部产品的持仓信息(列表格式) get_positions
- 11. 获取全部产品的持仓信息(字典格式) get_positionsMap
1. APP U本位合约交易账户函数总览
方法 | 解释 |
---|---|
get_account | 获取账户信息 |
get_balance | 获取单个产品余额 |
get_balances | 获取全部产品余额(列表格式) |
get_balancesMap | 获取全部产品余额(字典格式) |
set_leverage | 调整开仓杠杆 |
set_marginType | 更改持仓模式 |
get_position | 获取单个产品的持仓信息 |
get_positions | 获取全部产品的持仓信息(列表格式) |
get_positionsMap | 获取全部产品的持仓信息字典(字典格式) |
2. 模型实例化
from binance_interface.app import BinanceUM
from binance_interface.app.utils import eprint
# 转发:需搭建转发服务器,可参考:https://github.com/pyted/binance_resender
proxy_host = None
key = 'xxxx'
secret = 'xxxx'binanceUM = BinanceUM(key=key, secret=secret,proxy_host=proxy_host
)
account = binanceUM.account
3. 获取账户信息 get_account
account_result = account.get_account()
eprint(account_result)
输出:
>>> {'code': 200,
>>> 'data': {'feeTier': 0,
>>> 'canTrade': True,
>>> 'canDeposit': True,
>>> 'canWithdraw': True,
>>> 'tradeGroupId': -1,
>>> 'updateTime': 0,
>>> 'multiAssetsMargin': False,
>>> 'totalInitialMargin': '74.07260000',
>>> 'totalMaintMargin': '0.21261825',
>>> 'totalWalletBalance': '100.09787528',
>>> 'totalUnrealizedProfit': '-1.35838677',
>>> 'totalMarginBalance': '98.73948851',
>>> 'totalPositionInitialMargin': '38.17260000',
>>> 'totalOpenOrderInitialMargin': '35.90000000',
>>> 'totalCrossWalletBalance': '60.58480282',
>>> 'totalCrossUnPnl': '0.00000000',
>>> 'availableBalance': '24.68480282',
>>> 'maxWithdrawAmount': '24.68480282',
>>> 'assets': [{'asset': 'BTC',
>>> 'walletBalance': '0.00000000',
>>> 'unrealizedProfit': '0.00000000',
>>> 'marginBalance': '0.00000000',
>>> 'maintMargin': '0.00000000',
>>> '...': '......'},
>>> {'asset': 'XRP',
>>> 'walletBalance': '0.00000000',
>>> 'unrealizedProfit': '0.00000000',
>>> 'marginBalance': '0.00000000',
>>> 'maintMargin': '0.00000000',
>>> '...': '......'},
>>> {'asset': 'TUSD',
>>> 'walletBalance': '0.00000000',
>>> 'unrealizedProfit': '0.00000000',
>>> 'marginBalance': '0.00000000',
>>> 'maintMargin': '0.00000000',
>>> '...': '......'},
>>> {'asset': 'BNB',
>>> 'walletBalance': '0.00000000',
>>> 'unrealizedProfit': '0.00000000',
>>> 'marginBalance': '0.00000000',
>>> 'maintMargin': '0.00000000',
>>> '...': '......'},
>>> {'asset': 'ETH',
>>> 'walletBalance': '0.00000000',
>>> 'unrealizedProfit': '0.00000000',
>>> 'marginBalance': '0.00000000',
>>> 'maintMargin': '0.00000000',
>>> '...': '......'},
>>> '......'],
>>> 'positions': [{'symbol': 'SNTUSDT',
>>> 'initialMargin': '0',
>>> 'maintMargin': '0',
>>> 'unrealizedProfit': '0.00000000',
>>> 'positionInitialMargin': '0',
>>> '...': '......'},
>>> {'symbol': 'SNTUSDT',
>>> 'initialMargin': '0',
>>> 'maintMargin': '0',
>>> 'unrealizedProfit': '0.00000000',
>>> 'positionInitialMargin': '0',
>>> '...': '......'},
>>> {'symbol': 'SUSHIUSDT',
>>> 'initialMargin': '0',
>>> 'maintMargin': '0',
>>> 'unrealizedProfit': '0.00000000',
>>> 'positionInitialMargin': '0',
>>> '...': '......'},
>>> {'symbol': 'SUSHIUSDT',
>>> 'initialMargin': '0',
>>> 'maintMargin': '0',
>>> 'unrealizedProfit': '0.00000000',
>>> 'positionInitialMargin': '0',
>>> '...': '......'},
>>> {'symbol': 'BTSUSDT',
>>> 'initialMargin': '0',
>>> 'maintMargin': '0',
>>> 'unrealizedProfit': '0.00000000',
>>> 'positionInitialMargin': '0',
>>> '...': '......'},
>>> '......']},
>>> 'msg': ''}
4. 获取单个产品余额 get_balance
# 等价于下面代码:account.get_balance(symbol='BTCUSDT',base_asset='USDT')
balance_result = account.get_balance(asset='BTC')
eprint(balance_result)
输出:
>>> {'code': 200,
>>> 'data': {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'BTC',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> 'availableBalance': '0.00000000',
>>> 'maxWithdrawAmount': '0.00000000',
>>> 'marginAvailable': True,
>>> 'updateTime': 0},
>>> 'msg': ''}
5. 获取全部产品余额(列表格式) get_balances
# 参数assets默认为[],表示全部货币
balances_result = account.get_balances()
eprint(balances_result)
输出:
>>> {'code': 200,
>>> 'data': [{'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'BTC',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'},
>>> {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'XRP',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'},
>>> {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'TUSD',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'},
>>> {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'BNB',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'},
>>> {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'ETH',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'},
>>> {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'USDT',
>>> 'balance': '100.09787528',
>>> 'crossWalletBalance': '60.58480282',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'},
>>> {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'USDP',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'},
>>> {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'USDC',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'}],
>>> 'msg': ''}
# 指定多个货币
balances_result = account.get_balances(assets=['BTC', 'ETH'])
eprint(balances_result)
输出:
>>> {'code': 200,
>>> 'data': [{'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'BTC',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'},
>>> {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'ETH',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'}],
>>> 'msg': ''}
6. 获取全部产品余额(字典格式)
# 参数assets默认为[],表示全部货币
balancesMap_result = account.get_balancesMap()
eprint(balancesMap_result)
输出:
>>> {'code': 200,
>>> 'data': {'BTC': {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'BTC',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'},
>>> 'XRP': {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'XRP',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'},
>>> 'TUSD': {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'TUSD',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'},
>>> 'BNB': {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'BNB',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'},
>>> 'ETH': {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'ETH',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'},
>>> 'USDT': {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'USDT',
>>> 'balance': '100.09787528',
>>> 'crossWalletBalance': '60.58480282',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'},
>>> 'USDP': {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'USDP',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'},
>>> 'USDC': {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'USDC',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'}},
>>> 'msg': ''}
# 指定多个货币
balancesMap_result = account.get_balancesMap(assets=['BTC', 'ETH'])
eprint(balancesMap_result)
输出:
>>> {'code': 200,
>>> 'data': {'BTC': {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'BTC',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'},
>>> 'ETH': {'accountAlias': 'SguXuXmYSgTiSgfW',
>>> 'asset': 'ETH',
>>> 'balance': '0.00000000',
>>> 'crossWalletBalance': '0.00000000',
>>> 'crossUnPnl': '0.00000000',
>>> '...': '......'}},
>>> 'msg': ''}
7. 调整开仓杠杆 set_leverage
leverage_result = account.set_leverage(symbol='BTCUSDT', leverage=2)
eprint(leverage_result)
输出:
>>> {'code': 200, 'data': {'symbol': 'BTCUSDT', 'leverage': 2, 'maxNotionalValue': '800000000'}, 'msg': ''}
8. 更改持仓模式 set_marginType
marginType_result = account.set_marginType(symbol='BTCUSDT', marginType='CROSSED')
eprint(marginType_result)
输出:
>>> {'code': 200, 'msg': 'success'}
marginType_result = account.set_marginType(symbol='BTCUSDT', marginType='ISOLATED')
eprint(marginType_result)
输出:
>>> {'code': 200, 'msg': 'success'}
9. 获取单个产品的持仓信息 get_position
position_result = account.get_position(symbol='MANAUSDT')
eprint(position_result)
输出:
>>> {'code': 200,
>>> 'data': {'CROSSED': {'LONG': {}, 'SHORT': {}},
>>> 'ISOLATED': {'LONG': {'symbol': 'MANAUSDT',
>>> 'initialMargin': '40.50029305',
>>> 'maintMargin': '0.09425190',
>>> 'unrealizedProfit': '-1.37959371',
>>> 'positionInitialMargin': '14.50029306',
>>> 'openOrderInitialMargin': '25.99999999',
>>> 'leverage': '1',
>>> 'isolated': True,
>>> 'entryPrice': '0.4812086956521',
>>> 'breakEvenPrice': '0.4771861363636',
>>> 'maxNotional': '1.0E7',
>>> 'positionSide': 'LONG',
>>> 'positionAmt': '33',
>>> 'notional': '14.50029306',
>>> 'isolatedWallet': '15.86670268',
>>> 'updateTime': 1706112001852,
>>> 'bidNotional': '26',
>>> 'askNotional': '5.34800000'},
>>> 'SHORT': {}}},
>>> 'msg': ''}
10. 获取全部产品的持仓信息(列表格式) get_positions
# 参数symbols默认为[],表示全部产品
positions_result = account.get_positions()
eprint(positions_result)
输出:
>>> {'code': 200,
>>> 'data': {'CROSSED': {'LONG': [], 'SHORT': []},
>>> 'ISOLATED': {'LONG': [{'symbol': 'BNBUSDT',
>>> 'initialMargin': '11.74520151',
>>> 'maintMargin': '0.05872600',
>>> 'unrealizedProfit': '0.02160151',
>>> 'positionInitialMargin': '11.74520151',
>>> 'openOrderInitialMargin': '0',
>>> 'leverage': '1',
>>> 'isolated': True,
>>> 'entryPrice': '293.09',
>>> 'breakEvenPrice': '293.148618',
>>> 'maxNotional': '5.0E7',
>>> 'positionSide': 'LONG',
>>> 'positionAmt': '0.04',
>>> 'notional': '11.74520151',
>>> 'isolatedWallet': '11.72125528',
>>> 'updateTime': 1706117890515,
>>> 'bidNotional': '0',
>>> 'askNotional': '0'},
>>> {'symbol': 'MANAUSDT',
>>> 'initialMargin': '40.50020000',
>>> 'maintMargin': '0.09425130',
>>> 'unrealizedProfit': '-1.37968677',
>>> 'positionInitialMargin': '14.50020000',
>>> 'openOrderInitialMargin': '26',
>>> 'leverage': '1',
>>> 'isolated': True,
>>> 'entryPrice': '0.4812086956521',
>>> 'breakEvenPrice': '0.4771861363636',
>>> 'maxNotional': '1.0E7',
>>> 'positionSide': 'LONG',
>>> 'positionAmt': '33',
>>> 'notional': '14.50020000',
>>> 'isolatedWallet': '15.86670268',
>>> 'updateTime': 1706112001852,
>>> 'bidNotional': '26',
>>> 'askNotional': '5.34800000'},
>>> {'symbol': 'ADAUSDT',
>>> 'initialMargin': '11.92500000',
>>> 'maintMargin': '0.05962500',
>>> 'unrealizedProfit': '-0.00250000',
>>> 'positionInitialMargin': '11.92500000',
>>> 'openOrderInitialMargin': '0',
>>> 'leverage': '1',
>>> 'isolated': True,
>>> 'entryPrice': '0.4771',
>>> 'breakEvenPrice': '0.47719542',
>>> 'maxNotional': '5.0E7',
>>> 'positionSide': 'LONG',
>>> 'positionAmt': '25',
>>> 'notional': '11.92500000',
>>> 'isolatedWallet': '11.92511450',
>>> 'updateTime': 1706118204364,
>>> 'bidNotional': '0',
>>> 'askNotional': '0'}],
>>> 'SHORT': []}},
>>> 'msg': ''}
# 指定多个产品
positions_result = account.get_positions(symbols=['MANAUSDT', 'BNBUSDT'])
eprint(positions_result)
输出:
>>> {'code': 200,
>>> 'data': {'CROSSED': {'LONG': [], 'SHORT': []},
>>> 'ISOLATED': {'LONG': [{'symbol': 'BNBUSDT',
>>> 'initialMargin': '11.74275896',
>>> 'maintMargin': '0.05871379',
>>> 'unrealizedProfit': '0.01915896',
>>> 'positionInitialMargin': '11.74275896',
>>> 'openOrderInitialMargin': '0',
>>> 'leverage': '1',
>>> 'isolated': True,
>>> 'entryPrice': '293.09',
>>> 'breakEvenPrice': '293.148618',
>>> 'maxNotional': '5.0E7',
>>> 'positionSide': 'LONG',
>>> 'positionAmt': '0.04',
>>> 'notional': '11.74275896',
>>> 'isolatedWallet': '11.72125528',
>>> 'updateTime': 1706117890515,
>>> 'bidNotional': '0',
>>> 'askNotional': '0'},
>>> {'symbol': 'MANAUSDT',
>>> 'initialMargin': '40.50191963',
>>> 'maintMargin': '0.09426247',
>>> 'unrealizedProfit': '-1.37796714',
>>> 'positionInitialMargin': '14.50191963',
>>> 'openOrderInitialMargin': '26',
>>> 'leverage': '1',
>>> 'isolated': True,
>>> 'entryPrice': '0.4812086956521',
>>> 'breakEvenPrice': '0.4771861363636',
>>> 'maxNotional': '1.0E7',
>>> 'positionSide': 'LONG',
>>> 'positionAmt': '33',
>>> 'notional': '14.50191963',
>>> 'isolatedWallet': '15.86670268',
>>> 'updateTime': 1706112001852,
>>> 'bidNotional': '26',
>>> 'askNotional': '5.34800000'}],
>>> 'SHORT': []}},
>>> 'msg': ''}
11. 获取全部产品的持仓信息(字典格式) get_positionsMap
# 参数symbols默认为[],表示全部产品
positionsMap_result = account.get_positionsMap()
eprint(positionsMap_result)
输出:
>>> {'code': 200,
>>> 'data': {'CROSSED': {'LONG': {}, 'SHORT': {}},
>>> 'ISOLATED': {'LONG': {'BNBUSDT': {'symbol': 'BNBUSDT',
>>> 'initialMargin': '11.74000000',
>>> 'maintMargin': '0.05870000',
>>> 'unrealizedProfit': '0.01640000',
>>> 'positionInitialMargin': '11.74000000',
>>> 'openOrderInitialMargin': '0',
>>> 'leverage': '1',
>>> 'isolated': True,
>>> 'entryPrice': '293.09',
>>> 'breakEvenPrice': '293.148618',
>>> 'maxNotional': '5.0E7',
>>> 'positionSide': 'LONG',
>>> 'positionAmt': '0.04',
>>> 'notional': '11.74000000',
>>> 'isolatedWallet': '11.72125528',
>>> 'updateTime': 1706117890515,
>>> 'bidNotional': '0',
>>> 'askNotional': '0'},
>>> 'MANAUSDT': {'symbol': 'MANAUSDT',
>>> 'initialMargin': '40.50350000',
>>> 'maintMargin': '0.09427275',
>>> 'unrealizedProfit': '-1.37638677',
>>> 'positionInitialMargin': '14.50350000',
>>> 'openOrderInitialMargin': '26',
>>> 'leverage': '1',
>>> 'isolated': True,
>>> 'entryPrice': '0.4812086956521',
>>> 'breakEvenPrice': '0.4771861363636',
>>> 'maxNotional': '1.0E7',
>>> 'positionSide': 'LONG',
>>> 'positionAmt': '33',
>>> 'notional': '14.50350000',
>>> 'isolatedWallet': '15.86670268',
>>> 'updateTime': 1706112001852,
>>> 'bidNotional': '26',
>>> 'askNotional': '5.34800000'},
>>> 'ADAUSDT': {'symbol': 'ADAUSDT',
>>> 'initialMargin': '11.92750000',
>>> 'maintMargin': '0.05963750',
>>> 'unrealizedProfit': '0.00000000',
>>> 'positionInitialMargin': '11.92750000',
>>> 'openOrderInitialMargin': '0',
>>> 'leverage': '1',
>>> 'isolated': True,
>>> 'entryPrice': '0.4771',
>>> 'breakEvenPrice': '0.47719542',
>>> 'maxNotional': '5.0E7',
>>> 'positionSide': 'LONG',
>>> 'positionAmt': '25',
>>> 'notional': '11.92750000',
>>> 'isolatedWallet': '11.92511450',
>>> 'updateTime': 1706118204364,
>>> 'bidNotional': '0',
>>> 'askNotional': '0'}},
>>> 'SHORT': {}}},
>>> 'msg': ''}
# 指定多个产品
positionsMap_result = account.get_positionsMap(symbols=['MANAUSDT', 'BNBUSDT'])
eprint(positionsMap_result)
输出:
>>> {'code': 200,
>>> 'data': {'CROSSED': {'LONG': {}, 'SHORT': {}},
>>> 'ISOLATED': {'LONG': {'BNBUSDT': {'symbol': 'BNBUSDT',
>>> 'initialMargin': '11.74153010',
>>> 'maintMargin': '0.05870765',
>>> 'unrealizedProfit': '0.01793010',
>>> 'positionInitialMargin': '11.74153010',
>>> 'openOrderInitialMargin': '0',
>>> 'leverage': '1',
>>> 'isolated': True,
>>> 'entryPrice': '293.09',
>>> 'breakEvenPrice': '293.148618',
>>> 'maxNotional': '5.0E7',
>>> 'positionSide': 'LONG',
>>> 'positionAmt': '0.04',
>>> 'notional': '11.74153010',
>>> 'isolatedWallet': '11.72125528',
>>> 'updateTime': 1706117890515,
>>> 'bidNotional': '0',
>>> 'askNotional': '0'},
>>> 'MANAUSDT': {'symbol': 'MANAUSDT',
>>> 'initialMargin': '40.50639245',
>>> 'maintMargin': '0.09429155',
>>> 'unrealizedProfit': '-1.37349432',
>>> 'positionInitialMargin': '14.50639245',
>>> 'openOrderInitialMargin': '26',
>>> 'leverage': '1',
>>> 'isolated': True,
>>> 'entryPrice': '0.4812086956521',
>>> 'breakEvenPrice': '0.4771861363636',
>>> 'maxNotional': '1.0E7',
>>> 'positionSide': 'LONG',
>>> 'positionAmt': '33',
>>> 'notional': '14.50639245',
>>> 'isolatedWallet': '15.86670268',
>>> 'updateTime': 1706112001852,
>>> 'bidNotional': '26',
>>> 'askNotional': '5.34800000'}},
>>> 'SHORT': {}}},
>>> 'msg': ''}
这篇关于3.1 Binance_interface APP U本位合约交易账户的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!