Binance python api查询全球账户余额

问题描述

您好,我正在将 Binance api 与 python 一起使用,我所能拥有的只是我的 SPOT 帐户余额。 我正在使用这个:

client.get_account()

不幸的是,不包括抵押资产(赚取余额),所以我无法呈现整个情况。 有什么办法可以用apis查询我在Binance上的所有资产吗?我是否应该将收入/储蓄账户余额与现货账户余额分开查询? 谢谢

解决方法

我已经实现了以下代码片段。它给你:

  • 整体 Future 余额应用 unRealizedProfit

  • 整体 spot 余额,转换所有资产的 BTC 价值并将它们相加。

可能还有改进的余地,但它确实有效。

#!/usr/bin/env python3


class ClientHelper:
    def __init__(self,client):
        self.client = client

    def _format(self,value,decimal=2):
        return format(float(value),".2f")

    def transfer_futures_to_spot(self,amount):
        self.client.futures_account_transfer(asset="USDT",amount=float(amount),type="2")

    def transfer_spot_to_futures(self,type="1")

    def transfer_spot_to_margin(self,amount):
        self.client.transfer_spot_to_margin(asset="USDT",type="1")

    def get_balance_margin_USDT(self):
        try:
            _len = len(self.client.get_margin_account()["userAssets"])
            for x in range(_len):
                if self.client.get_margin_account()["userAssets"][x]["asset"] == "USDT":
                    balance_USDT = self.lient.get_margin_account()["userAssets"][x]["free"]
                    return float(balance_USDT)
        except:
            pass

        return 0

    def spot_balance(self):
        sum_btc = 0.0
        balances = self.client.get_account()
        for _balance in balances["balances"]:
            asset = _balance["asset"]
            if float(_balance["free"]) != 0.0 or float(_balance["locked"]) != 0.0:
                try:
                    btc_quantity = float(_balance["free"]) + float(_balance["locked"])
                    if asset == "BTC":
                        sum_btc += btc_quantity
                    else:
                        _price = self.client.get_symbol_ticker(symbol=asset + "BTC")
                        sum_btc += btc_quantity * float(_price["price"])
                except:
                    pass

        current_btc_price_USD = self.client.get_symbol_ticker(symbol="BTCUSDT")["price"]
        own_usd = sum_btc * float(current_btc_price_USD)
        print(" * Spot => %.8f BTC == " % sum_btc,end="")
        print("%.8f USDT" % own_usd)

    def get_futures_usdt(self,is_both=True) -> float:
        futures_usd = 0.0
        for asset in self.client.futures_account_balance():
            name = asset["asset"]
            balance = float(asset["balance"])
            if name == "USDT":
                futures_usd += balance

            if name == "BNB" and is_both:
                current_bnb_price_USD = self.client.get_symbol_ticker(symbol="BNBUSDT")["price"]
                futures_usd += balance * float(current_bnb_price_USD)

        return float(futures_usd)

    def _get_futures_usdt(self):
        """USDT in Futures,unRealizedProfit is also included"""
        futures_usd = self.get_futures_usdt(is_both=False)
        futures = self.client.futures_position_information()
        for future in futures:
            if future["positionAmt"] != "0" and float(future["unRealizedProfit"]) != 0.00000000:
                futures_usd += float(future["unRealizedProfit"])

        return format(futures_usd,".2f")


def main(client_helper):
    for balance in balances["balances"]:
        if balance["asset"] == "USDT":
            usdt_balance = balance["free"]
            break

    margin_usdt = client_helper.get_balance_margin_USDT()
    futures_usd = client_helper._get_futures_usdt()
    futures_usd = client_helper._get_futures_usdt()
    print(f" * Futures={futures_usd} USD | SPOT={client_helper._format(usdt_balance)} USD | MARGIN={margin_usdt} ")

    client_helper.spot_balance()


if __name__ == "__main__":
    client = Client(api_key,api_secret)
    client_helper = ClientHelper(client)
    main(client_helper)

示例输出:

./get_balance.py                                                                                  
 * Futures=72.36 USD | SPOT=0.00 USD | MARGIN=0
 * Spot => 0.01591034 BTC == 562.09649436 USDT

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...