gpt4 book ai didi

python - Binance python api查询全局账户余额

转载 作者:行者123 更新时间:2023-12-05 01:07:08 85 4
gpt4 key购买 nike

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

client.get_account()

不幸的是,不包括质押 Assets (收入余额),因此我无法呈现整个情况。有什么方法可以通过 api 查询我在 Binance 上的所有 Assets 吗?我是否应该将赚取/储蓄账户余额与现货账户余额分开查询?谢谢

最佳答案

在为自己寻找答案时,我遇到了 this post on Reddit ,我对其进行了最低限度的重新格式化以适合我的屏幕,并添加了 get_all_earn_products 函数:

##  From https://www.reddit.com/r/binance/comments/k6b1r7/
## accessing_earn_with_api/gu21o15/?utm_source=share
## &utm_medium=web2x&context=3

import hmac
import hashlib
import requests
import json
import time

uri = "https://api.binance.com"

## Fill in your Binance API key and Secret keys:
binance_api_key = ""
binance_api_secret = ""

def get_timestamp_offset():
url = "{}/api/v3/time".format(uri)
payload = {}
headers = {"Content-Type": "application/json"}
response = requests.request("GET", url, headers=headers, data=payload)
result = json.loads(response.text)["serverTime"] - int(time.time() * 1000)

return result


def generate_signature(query_string):
m = hmac.new(binance_api_secret.encode("utf-8"),
query_string.encode("utf-8"),
hashlib.sha256)
return m.hexdigest()

def get_flexible_savings_balance(asset):
""" Get your balance in Bincance Earn :: Flexible Savings """
timestamp = int(time.time() * 1000 + get_timestamp_offset())
query_string = "asset={}&timestamp={}".format(asset, timestamp)
signature = generate_signature(query_string)

url = "{}/sapi/v1/lending/daily/token/position?{}&signature={}".format(
uri, query_string, signature)

payload = {}
headers = {"Content-Type": "application/json",
"X-MBX-APIKEY": binance_api_key}

result = json.loads(requests.request("GET", url, headers=headers, data=payload).text)

return result

def get_locked_savings_balance(asset, project_id):
""" Get your balance in Bincance Earn :: Locked Savings """
timestamp = int(time.time() * 1000 + get_timestamp_offset())
query_string = "asset={}&projectId={}&status=HOLDING&timestamp={}".format(
asset, project_id, timestamp)
signature = generate_signature(query_string)

url = "{}/sapi/v1/lending/project/position/list?{}&signature={}".format(
uri, query_string, signature)

payload = {}
headers = {"Content-Type": "application/json",
"X-MBX-APIKEY": binance_api_key}

result = json.loads(requests.request("GET", url, headers=headers, data=payload).text)

return result

def get_all_earn_products():
""" Gets all savings products from Binance """
def get_earn_products(current_page=1):
""" Gets 50 savings products in "current" page ...modified from source:
https://binance-docs.github.io/apidocs/spot/en/#savings-endpoints """
timestamp = int(time.time() * 1000 + get_timestamp_offset())
query_string = "&current={}&status=SUBSCRIBABLE&timestamp={}".format(
current_page, timestamp)
signature = generate_signature(query_string)

url = "{}/sapi/v1/lending/daily/product/list?{}&signature={}".format(
uri, query_string, signature)

payload = {}
headers = {"Content-Type": "application/json",
"X-MBX-APIKEY": binance_api_key}

result = json.loads(requests.request("GET", url, headers=headers, data=payload).text)

return result

all_products = []
more_products = True
current_page = 0

while more_products:
current_page += 1
prod = get_earn_products(current_page=current_page)
all_products.extend(prod)
if len(prod)==50:
more_products = True
else:
more_products = False

return all_products

if __name__=="__main__":
# flex = get_flexible_savings_balance("BTC")
# print (flex)
# print ('')

# lock = get_locked_savings_balance("FUN", "CFUN14DAYSS001")
# print (json.dumps(lock, indent=2))
# print ('')

earn_products = get_all_earn_products()
print ("There are", len(earn_products), "subscribable earn products.\n")
print ("The frist two of them are:\n\n",
json.dumps(earn_products[0:2], indent=2))

得到这样的东西:

There are 140 subscribable earn products.
The frist two of them are:

[
{
"productId": "AVAX001",
"asset": "AVAX",
"status": "PURCHASING",
"canPurchase": true,
"canRedeem": true,
"featured": true,
"avgAnnualInterestRate": "0.01214355",
"purchasedAmount": "8042.32212732",
"upLimit": "10000000",
"upLimitPerUser": "10000000",
"minPurchaseAmount": "0.1",
"tierAnnualInterestRate": {
">5AVAX": "0.00200020",
"0-5AVAX": "0.03000300"
}
},
{
"productId": "LUNA001",
"asset": "LUNA",
"status": "PURCHASING",
"canPurchase": true,
"canRedeem": true,
"featured": true,
"avgAnnualInterestRate": "0.01214355",
"purchasedAmount": "21092.73974528",
"upLimit": "10000000",
"upLimitPerUser": "10000000",
"minPurchaseAmount": "0.1",
"tierAnnualInterestRate": {
"0-5LUNA": "0.03000300",
">5LUNA": "0.00200020"
}
}
]

附录

  1. 为了获得一些锁定储蓄的详细信息(我几天前所做的),我取消了这两行的注释
lock = get_locked_savings_balance("FUN", "CFUN14DAYSS001")
print (json.dumps(lock, indent=2))

在上述代码的末尾...得到这样的东西--

[
{
"asset": "FUN",
"canTransfer": true,
"createTimestamp": 1640453123000,
"duration": 14,
"endTime": 1641686300000,
"interest": "3.45",
"interestRate": "0.44999286",
"lot": 20,
"positionId": 1438863,
"principal": "200",
"projectId": "CFUN14DAYSS001",
"projectName": "FUN",
"purchaseTime": 1640453154000,
"redeemDate": "2022-01-09",
"startTime": 1640476700000,
"status": "HOLDING",
"type": "CUSTOMIZED_FIXED"
}
]
  1. 可以使用 https://binance-docs.github.io/apidocs/spot/en/#savings-endpoints 中定义的 Binance API 端点构建更多此类功能/查询.

关于python - Binance python api查询全局账户余额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67800541/

85 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com