2

问题

在使用 Bitstamp HTTP API 时,我发现了一些非常奇怪的东西。
每当我使用以下请求询问最​​新的 OHLC 数据时:

https://www.bitstamp.net/api/v2/ohlc/{currency_pair}/

所需的参数写在Bitstamp 的文档页面中

我只得到 1 个数据点。这是错误的,因为limit参数设置为2. 当我走得更远并一次又一次地发送以下请求时,事情变得有价值。我发现这只发生在step我使用的任何(间隔)蜡烛关闭的前 40~45 秒。

例子

step参数设置为60时,服务器必须返回一个 1-minute-candles 并传递数字2for limit,前 40~45 秒的响应包含 1 个数据点:

{"data": {"pair": "BTC/USD", "ohlc": [{"high": "19131.67", "timestamp": "1607194800", "volume": "0.00000000", "low": "19131.67", "close": "19131.67", "open": "19131.67"}]}}

但随着时间的推移,我们通过前 40~45 秒,响应包含 2 个数据点:

{"data": {"pair": "BTC/USD", "ohlc": [{"high": "19127.87", "timestamp": "1607194860", "volume": "0.49121478", "low": "19104.91", "close": "19104.91", "open": "19127.87"}, {"high": "19111.41", "timestamp": "1607194920", "volume": "0.09581116", "low": "19104.45", "close": "19111.41", "open": "19104.67"}]}}

对于 .的其他有效值也会发生同样的情况step。设置step必须300包含 5 分钟蜡烛,在任何 5 分钟间隔的前 40~45 秒请求时响应无效(例如 12:00-UTC、12:05-UTC 等) .

无论参数step设置为任何有效数字,服务器在任何时间范围开始的前 40~45 秒返回错误数据。

我也尝试过传递startend可选参数。

我曾与其他交易所和经纪人的 API 合作过,到目前为止,我没有遇到任何错误或错误响应,除了使用Bitstamp HTTP API

注意:值40~45 秒不准确,因为我无法准确测量它。

python中的代码示例和日志

python中的代码

import logging
import requests

from datetime import datetime
from time import sleep


base_url_v2 = "https://www.bitstamp.net/api/v2/"
logging.basicConfig(filename='app.log',
                    filemode='w',
                    format='[%(asctime)s UTC][%(name)s][%(levelname)s]'
                           ':%(message)s',
                    level=logging.DEBUG,
                    datefmt='%M/%d/%y %H:%M:%S')


def get_ohlc_data(pair: str, step, limit):
    url = f"ohlc/{pair}/"
    request_url = base_url_v2 + url

    params = {
        "step": step,
        "limit": limit
    }
    try:
        logging.info("Sending request...")
        response = requests.get(request_url, params=params)
        logging.debug(f"Request response: {response.text}")

    except Exception as exp:
        logging.error(exp)


def main():
    logging.info("Initialized.")

    # Wait until xx:xx:00 UTC
    while int(datetime.now().strftime("%S")) != 0:
        sleep(0.5)

    # request OHLC data every 5 seconds
    for r in range(12):

        # Wait until xx:xx:x5[or 0] UTC
        while int(datetime.now().strftime("%S")) % 5 != 0:
            sleep(1)

        get_ohlc_data(pair="btcusd",
                      step=60,
                      limit=2)

        sleep(1)

    logging.info("Ended.")


main()

日志文件

[04/16/20 18:04:16 UTC][root][INFO]:Initialized.
[05/16/20 18:05:00 UTC][root][INFO]:Sending request...
[05/16/20 18:05:00 UTC][urllib3.connectionpool][DEBUG]:Starting new HTTPS connection (1): www.bitstamp.net:443
[05/16/20 18:05:00 UTC][urllib3.connectionpool][DEBUG]:https://www.bitstamp.net:443 "GET /api/v2/ohlc/btcusd/?step=60&limit=2 HTTP/1.1" 200 None
[05/16/20 18:05:00 UTC][root][DEBUG]:Request response: {"data": {"pair": "BTC/USD", "ohlc": [{"high": "20690.59", "timestamp": "1608141840", "volume": "13.32120988", "low": "20677.18", "close": "20677.18", "open": "20690.59"}]}}
[05/16/20 18:05:05 UTC][root][INFO]:Sending request...
[05/16/20 18:05:05 UTC][urllib3.connectionpool][DEBUG]:Starting new HTTPS connection (1): www.bitstamp.net:443
[05/16/20 18:05:06 UTC][urllib3.connectionpool][DEBUG]:https://www.bitstamp.net:443 "GET /api/v2/ohlc/btcusd/?step=60&limit=2 HTTP/1.1" 200 None
[05/16/20 18:05:06 UTC][root][DEBUG]:Request response: {"data": {"pair": "BTC/USD", "ohlc": [{"high": "20681.91", "timestamp": "1608141900", "volume": "0.02455454", "low": "20681.91", "close": "20681.91", "open": "20681.91"}]}}
[05/16/20 18:05:10 UTC][root][INFO]:Sending request...
[05/16/20 18:05:10 UTC][urllib3.connectionpool][DEBUG]:Starting new HTTPS connection (1): www.bitstamp.net:443
[05/16/20 18:05:10 UTC][urllib3.connectionpool][DEBUG]:https://www.bitstamp.net:443 "GET /api/v2/ohlc/btcusd/?step=60&limit=2 HTTP/1.1" 200 None
[05/16/20 18:05:10 UTC][root][DEBUG]:Request response: {"data": {"pair": "BTC/USD", "ohlc": [{"high": "20681.91", "timestamp": "1608141900", "volume": "0.30755454", "low": "20671.52", "close": "20671.52", "open": "20681.91"}]}}
[05/16/20 18:05:15 UTC][root][INFO]:Sending request...
[05/16/20 18:05:15 UTC][urllib3.connectionpool][DEBUG]:Starting new HTTPS connection (1): www.bitstamp.net:443
[05/16/20 18:05:16 UTC][urllib3.connectionpool][DEBUG]:https://www.bitstamp.net:443 "GET /api/v2/ohlc/btcusd/?step=60&limit=2 HTTP/1.1" 200 None
[05/16/20 18:05:16 UTC][root][DEBUG]:Request response: {"data": {"pair": "BTC/USD", "ohlc": [{"high": "20681.91", "timestamp": "1608141900", "volume": "1.43352856", "low": "20671.52", "close": "20674.94", "open": "20681.91"}]}}
[05/16/20 18:05:20 UTC][root][INFO]:Sending request...
[05/16/20 18:05:20 UTC][urllib3.connectionpool][DEBUG]:Starting new HTTPS connection (1): www.bitstamp.net:443
[05/16/20 18:05:20 UTC][urllib3.connectionpool][DEBUG]:https://www.bitstamp.net:443 "GET /api/v2/ohlc/btcusd/?step=60&limit=2 HTTP/1.1" 200 None
[05/16/20 18:05:20 UTC][root][DEBUG]:Request response: {"data": {"pair": "BTC/USD", "ohlc": [{"high": "20681.91", "timestamp": "1608141900", "volume": "1.43352856", "low": "20671.52", "close": "20674.94", "open": "20681.91"}]}}
[05/16/20 18:05:25 UTC][root][INFO]:Sending request...
[05/16/20 18:05:25 UTC][urllib3.connectionpool][DEBUG]:Starting new HTTPS connection (1): www.bitstamp.net:443
[05/16/20 18:05:25 UTC][urllib3.connectionpool][DEBUG]:https://www.bitstamp.net:443 "GET /api/v2/ohlc/btcusd/?step=60&limit=2 HTTP/1.1" 200 None
[05/16/20 18:05:25 UTC][root][DEBUG]:Request response: {"data": {"pair": "BTC/USD", "ohlc": [{"high": "20681.91", "timestamp": "1608141900", "volume": "1.43352856", "low": "20671.52", "close": "20674.94", "open": "20681.91"}]}}
[05/16/20 18:05:30 UTC][root][INFO]:Sending request...
[05/16/20 18:05:30 UTC][urllib3.connectionpool][DEBUG]:Starting new HTTPS connection (1): www.bitstamp.net:443
[05/16/20 18:05:31 UTC][urllib3.connectionpool][DEBUG]:https://www.bitstamp.net:443 "GET /api/v2/ohlc/btcusd/?step=60&limit=2 HTTP/1.1" 200 None
[05/16/20 18:05:31 UTC][root][DEBUG]:Request response: {"data": {"pair": "BTC/USD", "ohlc": [{"high": "20687.00", "timestamp": "1608141900", "volume": "1.65890659", "low": "20671.52", "close": "20676.56", "open": "20681.91"}]}}
[05/16/20 18:05:35 UTC][root][INFO]:Sending request...
[05/16/20 18:05:35 UTC][urllib3.connectionpool][DEBUG]:Starting new HTTPS connection (1): www.bitstamp.net:443
[05/16/20 18:05:36 UTC][urllib3.connectionpool][DEBUG]:https://www.bitstamp.net:443 "GET /api/v2/ohlc/btcusd/?step=60&limit=2 HTTP/1.1" 200 None
[05/16/20 18:05:36 UTC][root][DEBUG]:Request response: {"data": {"pair": "BTC/USD", "ohlc": [{"high": "20687.00", "timestamp": "1608141900", "volume": "1.65890659", "low": "20671.52", "close": "20676.56", "open": "20681.91"}]}}
[05/16/20 18:05:40 UTC][root][INFO]:Sending request...
[05/16/20 18:05:40 UTC][urllib3.connectionpool][DEBUG]:Starting new HTTPS connection (1): www.bitstamp.net:443
[05/16/20 18:05:40 UTC][urllib3.connectionpool][DEBUG]:https://www.bitstamp.net:443 "GET /api/v2/ohlc/btcusd/?step=60&limit=2 HTTP/1.1" 200 None
[05/16/20 18:05:40 UTC][root][DEBUG]:Request response: {"data": {"pair": "BTC/USD", "ohlc": [{"high": "20687.00", "timestamp": "1608141900", "volume": "2.55967640", "low": "20671.52", "close": "20675.47", "open": "20681.91"}]}}
[05/16/20 18:05:45 UTC][root][INFO]:Sending request...
[05/16/20 18:05:45 UTC][urllib3.connectionpool][DEBUG]:Starting new HTTPS connection (1): www.bitstamp.net:443
[05/16/20 18:05:45 UTC][urllib3.connectionpool][DEBUG]:https://www.bitstamp.net:443 "GET /api/v2/ohlc/btcusd/?step=60&limit=2 HTTP/1.1" 200 None
[05/16/20 18:05:45 UTC][root][DEBUG]:Request response: {"data": {"pair": "BTC/USD", "ohlc": [{"high": "20690.59", "timestamp": "1608141840", "volume": "13.32120988", "low": "20677.18", "close": "20677.18", "open": "20690.59"}, {"high": "20687.00", "timestamp": "1608141900", "volume": "3.10476012", "low": "20671.52", "close": "20686.60", "open": "20681.91"}]}}
[05/16/20 18:05:50 UTC][root][INFO]:Sending request...
[05/16/20 18:05:50 UTC][urllib3.connectionpool][DEBUG]:Starting new HTTPS connection (1): www.bitstamp.net:443
[05/16/20 18:05:51 UTC][urllib3.connectionpool][DEBUG]:https://www.bitstamp.net:443 "GET /api/v2/ohlc/btcusd/?step=60&limit=2 HTTP/1.1" 200 None
[05/16/20 18:05:51 UTC][root][DEBUG]:Request response: {"data": {"pair": "BTC/USD", "ohlc": [{"high": "20690.59", "timestamp": "1608141840", "volume": "13.32120988", "low": "20677.18", "close": "20677.18", "open": "20690.59"}, {"high": "20687.00", "timestamp": "1608141900", "volume": "3.55776012", "low": "20671.52", "close": "20686.54", "open": "20681.91"}]}}
[05/16/20 18:05:55 UTC][root][INFO]:Sending request...
[05/16/20 18:05:55 UTC][urllib3.connectionpool][DEBUG]:Starting new HTTPS connection (1): www.bitstamp.net:443
[05/16/20 18:05:55 UTC][urllib3.connectionpool][DEBUG]:https://www.bitstamp.net:443 "GET /api/v2/ohlc/btcusd/?step=60&limit=2 HTTP/1.1" 200 None
[05/16/20 18:05:56 UTC][root][DEBUG]:Request response: {"data": {"pair": "BTC/USD", "ohlc": [{"high": "20690.59", "timestamp": "1608141840", "volume": "13.32120988", "low": "20677.18", "close": "20677.18", "open": "20690.59"}, {"high": "20694.99", "timestamp": "1608141900", "volume": "3.77376012", "low": "20671.52", "close": "20694.99", "open": "20681.91"}]}}
[05/16/20 18:05:57 UTC][root][INFO]:Ended.

要求

  1. 我该如何解决这个问题?
  2. 是否有任何社区、讨论组,我可以加入 Bitstamp API ussies?
4

1 回答 1

0

首先,非常精确的问题描述,抬头。

它有助于将 unix 时间转换为可读时间,您将看到问题的答案。
与来自交易所/供应商的其他 REST API 一样,您也distribution受到平台的限制。因此,这不是您的请求限制,而是返回的数据量。
因此,通过增加限制变量,您可以及时增加回溯。
因此,对于 step=60 和 limit=2,您将收到最后 2 分钟的 OHLC 数据。对于limit=3,它将是最后3 分钟。对于 step=300 和 limit=2,它将是最后 5 分钟的 2。

下面是一些代码来翻译 API 答案以更好地理解它:

# amending your get ohlc data function a bit:
import pandas as pd
import json
def get_ohlc_data(pair: str, step, limit):
    url = f"ohlc/{pair}/"
    request_url = base_url_v2 + url

    params = {
        "step": step,
        "limit": limit
    }
    try:
        logging.info("Sending request...")
        response = requests.get(request_url, params=params)
        # including the json.dumps() function
        response = json.dumps(response.text)
        return response["data"]["ohlc"]

        #logging.debug(f"Request response: {response.text}")

    except Exception as exp:
        logging.error(exp)


# now your main function just without the waitings:
def main():
    req = get_ohlc_data(pair="btcusd", step=60, limit=8)

    for i in range(len(req)):
        df = pd.DataFrame.from_dict(req[i], orient="index").T
        df["timestamp"] = pd.to_datetime(df["timestamp"], unit="s")
        print(df)

main()

这最终将为您返回以下内容:

        high           timestamp      volume       low     close      open
0  50558.85 2021-03-07 18:51:00  1.44848029  50515.63  50546.20  50558.85
        high           timestamp      volume       low     close      open
0  50567.90 2021-03-07 18:52:00  0.80101510  50540.16  50554.20  50558.10
        high           timestamp      volume       low     close      open
0  50545.71 2021-03-07 18:53:00  3.49063548  50490.38  50512.13  50545.71
        high           timestamp      volume       low     close      open
0  50573.18 2021-03-07 18:54:00  1.29261148  50508.50  50519.68  50508.50
        high           timestamp      volume       low     close      open
0  50553.27 2021-03-07 18:55:00  1.45939999  50465.08  50553.27  50526.51
        high           timestamp       volume       low     close      open
0  50539.70 2021-03-07 18:56:00  13.61910047  50440.12  50457.87  50539.70
        high           timestamp       volume       low     close      open
0  50467.67 2021-03-07 18:57:00  10.23238701  50403.76  50428.09  50467.36
        high           timestamp      volume       low     close      open
0  50438.21 2021-03-07 18:58:00  1.50492458  50376.29  50401.85  50418.65

所以最后,API 和它的流都没有错。它流式传输的unix时间看起来有点误导。此外,如果您想传递一个startend变量,您需要将它们作为 unix 时间发送。由于 API 只接受 unix 时间。

这可以通过以下方式完成:

from datetime import datetime
datetime.utcnow().timestamp()
于 2021-03-07T19:17:15.380 回答