我可以获取Binance Futures订购书籍的历史数据

发布于 2025-01-25 15:15:06 字数 5276 浏览 1 评论 0 原文

我正在尝试使用API​​获取binance Futures订购历史记录数据。因此,我询问了Binance的数据,得到答案:“您的历史期货订单订单数据已批准,请遵循我们的GitHub指南,以使用您的白名单帐户API密钥访问”,并且我已经设置了API。

我已经修改了启用符号白名单:

”在此处输入图像说明“

下一步,我遵循GitHub指导: https://github.com/binance/binance/binance-public-data/tree/master/master/futures/futures/futures/futures_order_order_order_order_order_order_order_order_book_download

哪些具有以下样本代码:

"""
This example python script shows how to download the Historical Future Order Book level 2 Data via API.
The data download API is part of the Binance API (https://binance-docs.github.io/apidocs/spot/en/#general-api-information).
For how to use it, you may find info there with more examples, especially SIGNED Endpoint security as in https://binance-docs.github.io/apidocs/spot/en/#signed-trade-user_data-and-margin-endpoint-security
Before executing this file, please note:
    - The API account needs to have a Futures account to access Futures data.
    - The API key has been whitelisted to access the data.
    - Read the comments section in this file to know where you should specify your request values.
"""

# Install the following required packages
import requests
import time
import hashlib
import hmac
from urllib.parse import urlencode

S_URL_V1 = "https://api.binance.com/sapi/v1"

# Specify the api_key and secret_key with your API Key and secret_key
api_key = "your_api_key"
secret_key = "your_secret_key "

# Specify the four input parameters below:
symbol = "ADAUSDT"  # specify the symbol name
startTime = 1635561504914  # specify the starttime
endTime = 1635561604914  # specify the endtime
dataType = "T_DEPTH"  # specify the dataType to be downloaded


# Function to generate the signature
def _sign(params={}):
    data = params.copy()
    ts = str(int(1000 * time.time()))
    data.update({"timestamp": ts})
    h = urlencode(data)
    h = h.replace("%40", "@")

    b = bytearray()
    b.extend(secret_key.encode())

    signature = hmac.new(b, msg=h.encode("utf-8"), digestmod=hashlib.sha256).hexdigest()
    sig = {"signature": signature}

    return data, sig


# Function to generate the download ID
def post(path, params={}):
    sign = _sign(params)
    query = urlencode(sign[0]) + "&" + urlencode(sign[1])
    url = "%s?%s" % (path, query)
    header = {"X-MBX-APIKEY": api_key}
    resultPostFunction = requests.post(url, headers=header, timeout=30, verify=True)
    return resultPostFunction


# Function to generate the download link
def get(path, params):
    sign = _sign(params)
    query = urlencode(sign[0]) + "&" + urlencode(sign[1])
    url = "%s?%s" % (path, query)
    header = {"X-MBX-APIKEY": api_key}
    resultGetFunction = requests.get(url, headers=header, timeout=30, verify=True)
    return resultGetFunction


"""
Beginning of the execution.
The final output will be:
- A link to download the specific data you requested with the specific parameters.
Sample output will be like the following: {'expirationTime': 1635825806, 'link': 'https://bin-prod-user-rebate-bucket.s3.amazonaws.com/future-data-download/XXX'
Copy the link to the browser and download the data. The link would expire after the expirationTime (usually 24 hours).
- A message reminding you to re-run the code and download the data hours later.
Sample output will be like the following: {'link': 'Link is preparing; please request later. Notice: when date range is very large (across months), we may need hours to generate.'}
"""

timestamp = str(
    int(1000 * time.time())
)  # current timestamp which serves as an input for the params variable
paramsToObtainDownloadID = {
    "symbol": symbol,
    "startTime": startTime,
    "endTime": endTime,
    "dataType": dataType,
    "timestamp": timestamp,
}

# Calls the "post" function to obtain the download ID for the specified symbol, dataType and time range combination
path = "%s/futuresHistDataId" % S_URL_V1
resultDownloadID = post(path, paramsToObtainDownloadID)
print(resultDownloadID)
downloadID = resultDownloadID.json()["id"]
print(downloadID)  # prints the download ID, example: {'id': 324225}


# Calls the "get" function to obtain the download link for the specified symbol, dataType and time range combination
paramsToObtainDownloadLink = {"downloadId": downloadID, "timestamp": timestamp}
pathToObtainDownloadLink = "%s/downloadLink" % S_URL_V1
resultToBeDownloaded = get(pathToObtainDownloadLink, paramsToObtainDownloadLink)
print(resultToBeDownloaded)
print(resultToBeDownloaded.json())

我已经修改了 api_key secret_key 到我自己的密钥,这就是我得到的结果。

您能告诉我在哪里犯了一个错误吗?事先感谢您的答案。

I'm trying to get binance Futures order history data using API. So I asked for data from binance, got the answer "Your application for historical futures order book data has been approved, please follow our Github guidance to access with your whitelisted account API key" and I have set up the API as follows.

enter image description here

And I have modified the Enable Symbol Whitelist like this:

enter image description here

The next step, I followed Github guidance: https://github.com/binance/binance-public-data/tree/master/Futures_Order_Book_Download

which has the following sample code:

"""
This example python script shows how to download the Historical Future Order Book level 2 Data via API.
The data download API is part of the Binance API (https://binance-docs.github.io/apidocs/spot/en/#general-api-information).
For how to use it, you may find info there with more examples, especially SIGNED Endpoint security as in https://binance-docs.github.io/apidocs/spot/en/#signed-trade-user_data-and-margin-endpoint-security
Before executing this file, please note:
    - The API account needs to have a Futures account to access Futures data.
    - The API key has been whitelisted to access the data.
    - Read the comments section in this file to know where you should specify your request values.
"""

# Install the following required packages
import requests
import time
import hashlib
import hmac
from urllib.parse import urlencode

S_URL_V1 = "https://api.binance.com/sapi/v1"

# Specify the api_key and secret_key with your API Key and secret_key
api_key = "your_api_key"
secret_key = "your_secret_key "

# Specify the four input parameters below:
symbol = "ADAUSDT"  # specify the symbol name
startTime = 1635561504914  # specify the starttime
endTime = 1635561604914  # specify the endtime
dataType = "T_DEPTH"  # specify the dataType to be downloaded


# Function to generate the signature
def _sign(params={}):
    data = params.copy()
    ts = str(int(1000 * time.time()))
    data.update({"timestamp": ts})
    h = urlencode(data)
    h = h.replace("%40", "@")

    b = bytearray()
    b.extend(secret_key.encode())

    signature = hmac.new(b, msg=h.encode("utf-8"), digestmod=hashlib.sha256).hexdigest()
    sig = {"signature": signature}

    return data, sig


# Function to generate the download ID
def post(path, params={}):
    sign = _sign(params)
    query = urlencode(sign[0]) + "&" + urlencode(sign[1])
    url = "%s?%s" % (path, query)
    header = {"X-MBX-APIKEY": api_key}
    resultPostFunction = requests.post(url, headers=header, timeout=30, verify=True)
    return resultPostFunction


# Function to generate the download link
def get(path, params):
    sign = _sign(params)
    query = urlencode(sign[0]) + "&" + urlencode(sign[1])
    url = "%s?%s" % (path, query)
    header = {"X-MBX-APIKEY": api_key}
    resultGetFunction = requests.get(url, headers=header, timeout=30, verify=True)
    return resultGetFunction


"""
Beginning of the execution.
The final output will be:
- A link to download the specific data you requested with the specific parameters.
Sample output will be like the following: {'expirationTime': 1635825806, 'link': 'https://bin-prod-user-rebate-bucket.s3.amazonaws.com/future-data-download/XXX'
Copy the link to the browser and download the data. The link would expire after the expirationTime (usually 24 hours).
- A message reminding you to re-run the code and download the data hours later.
Sample output will be like the following: {'link': 'Link is preparing; please request later. Notice: when date range is very large (across months), we may need hours to generate.'}
"""

timestamp = str(
    int(1000 * time.time())
)  # current timestamp which serves as an input for the params variable
paramsToObtainDownloadID = {
    "symbol": symbol,
    "startTime": startTime,
    "endTime": endTime,
    "dataType": dataType,
    "timestamp": timestamp,
}

# Calls the "post" function to obtain the download ID for the specified symbol, dataType and time range combination
path = "%s/futuresHistDataId" % S_URL_V1
resultDownloadID = post(path, paramsToObtainDownloadID)
print(resultDownloadID)
downloadID = resultDownloadID.json()["id"]
print(downloadID)  # prints the download ID, example: {'id': 324225}


# Calls the "get" function to obtain the download link for the specified symbol, dataType and time range combination
paramsToObtainDownloadLink = {"downloadId": downloadID, "timestamp": timestamp}
pathToObtainDownloadLink = "%s/downloadLink" % S_URL_V1
resultToBeDownloaded = get(pathToObtainDownloadLink, paramsToObtainDownloadLink)
print(resultToBeDownloaded)
print(resultToBeDownloaded.json())

I have modified api_key and secret_key to my own keys and this is the result I got.
enter image description here

Can you tell me where I made a mistake? Thanks in advance for the answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您的好友蓝忘机已上羡 2025-02-01 15:15:06

查看 https://wwwww.binance.com/en-ng/landing/data < /a>。

期货订单订单数据仅在二元期货上可用。它需要
期货帐户首先要白色,只能通过API下载。
订购手册快照(S_DEPTH):自2020年1月以来,仅在BTC/USDT上
象征。刻度级订单(T_DEPTH):自2020年1月以来
符号

该页面说您应该在此处使用二元表格以在期货部分中列入白名单:

第二件事 - 您对期货感兴趣,而不是景点,因此URL应为 api.binance.com/fapi 而不是 api.binance.binance.com/sapi

第三件事 - 订单簿的API端点是
get/fapi/v1/depth

Look at https://www.binance.com/en-NG/landing/data.

Futures Order Book Data Available only on Binance Futures. It requires
futures account be whitelisted first and can only be download via API.
Orderbook snapshot (S_Depth): Since January 2020, only on BTC/USDT
symbol. Tick-level orderbook (T_Depth): Since January 2020, on all
symbols

The page says you should to apply the Binance form to be whitelisted in futures section here:
https://docs.google.com/forms/d/e/1FAIpQLSexCgyvZEMI1pw1Xj6gwKtfQTYUbH5HrUQ0gwgPZtM9FaM2Hw/viewform

Second thing - you are interested in futures, not spots, so the url should be api.binance.com/fapi instead of api.binance.com/sapi

Third thing - API endpoint for order book is
GET /fapi/v1/depth

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文