Bitstamp API在询问最新OHLC时返回错误的Kline数[已解决] 问题
在使用Bitstamp HTTP API时,我发现了一些非常奇怪的事情。Bitstamp API在询问最新OHLC时返回错误的Kline数[已解决] 问题,api,cryptography,finance,ohlc,Api,Cryptography,Finance,Ohlc,在使用Bitstamp HTTP API时,我发现了一些非常奇怪的事情。 每当我使用以下请求请求最新OHLC数据时: https://www.bitstamp.net/api/v2/ohlc/{currency_pair}/ 所需参数写在中 我只得到一个数据点。这是错误的,因为limit参数设置为2。当我进一步发送下面的请求时,事情变得有价值了。我发现这只发生在我使用的任何步骤(间隔)蜡烛关闭的前40~45秒 例子 当步骤参数设置为60时,服务器必须返回1分钟的时间,并传递限制的数字2,在前4
每当我使用以下请求请求最新OHLC数据时:
https://www.bitstamp.net/api/v2/ohlc/{currency_pair}/
所需参数写在中
我只得到一个数据点。这是错误的,因为limit
参数设置为2
。当我进一步发送下面的请求时,事情变得有价值了。我发现这只发生在我使用的任何步骤(间隔)蜡烛关闭的前40~45秒
例子
当步骤
参数设置为60
时,服务器必须返回1分钟的时间,并传递限制
的数字2
,在前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"}]}}
对于步骤
的其他有效值也会发生同样的情况。将步骤设置为300
,该步骤必须包含5分钟烛光,在任何5分钟间隔的前40~45秒内(例如12:00-UTC、12:05-UTC等)请求时,响应无效
无论参数step
设置为任何有效数字,服务器都会在开始任何时间帧的前40~45秒返回错误数据
我还尝试传递了start
和end
可选参数
我使用过其他exchange和broker的API,到目前为止,除了使用Bitstamp HTTP API之外,我没有遇到任何错误或错误的响应
注意:40~45秒的值不准确,因为我无法准确测量
python和日志中的代码示例
python代码
日志文件
要求
如何解决此问题
是否有任何社区、讨论组,我可以参加Bitstamp API ussies
首先,非常精确的问题描述,抬头
它有助于将unix时间转换为可读时间,您将看到问题的答案。
与来自Exchange/供应商的其他REST API一样,您也有来自平台的发行版
限制。因此,这不是您的请求限制,而是返回的数据量。
因此,通过增加limit变量,可以增加时间回溯。
因此,对于步骤=60和限制=2,您将收到最后2分钟的OHLC数据。对于极限=3,则为最后3分钟。对于步长=300且极限=2的情况,这将是最后5分钟的2次
下面是一些翻译API答案的代码,以便更好地理解它:
#稍微修改一下get ohlc数据函数:
作为pd进口熊猫
导入json
def get_ohlc_数据(对:str、step、limit):
url=f“ohlc/{pair}/”
请求url=base\u url\u v2+url
参数={
“步骤”:步骤,
“限制”:限制
}
尝试:
logging.info(“发送请求…”)
response=requests.get(请求url,参数=params)
#包括json.dumps()函数
response=json.dumps(response.text)
返回响应[“数据”][“ohlc”]
#debug(f“请求-响应:{response.text}”)
例外情况除外:
logging.error(exp)
#现在,您的主要功能不需要等待:
def main():
req=获取ohlc数据(pair=“btcusd”,步骤=60,限制=8)
对于范围内的i(len(req)):
df=pd.DataFrame.from_dict(请求[i],orient=“索引”).T
df[“timestamp”]=pd.to_datetime(df[“timestamp”],unit=“s”)
打印(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
高时间戳卷低关闭打开
0 50567.90 2021-03-07 18:52:00 0.80101510 50540.16 50554.20 50558.10
高时间戳卷低关闭打开
0 50545.71 2021-03-07 18:53:00 3.49063548 50490.38 50512.13 50545.71
高时间戳卷低关闭打开
0 50573.18 2021-03-07 18:54:00 1.29261148 50508.50 50519.68 50508.50
高时间戳卷低关闭打开
0 50553.27 2021-03-07 18:55:00 1.45939999 50465.08 50553.27 50526.51
高时间戳卷低关闭打开
0 50539.70 2021-03-07 18:56:00 13.61910047 50440.12 50457.87 50539.70
高时间戳卷低关闭打开
0 50467.67 2021-03-07 18:57:00 10.23238701 50403.76 50428.09 50467.36
高时间戳卷低关闭打开
0 50438.21 2021-03-07 18:58:00 1.50492458 50376.29 50401.85 50418.65
因此,最终,API及其流没有任何问题。只是看起来有点误导unix的时间流。此外,如果要传递start
和end
变量,则需要将它们作为unix时间发送。因为API只接受unix时间
这可以通过以下方式实现:
从日期时间导入日期时间
datetime.utcnow().timestamp()
非常感谢您的解释。我必须提到,这个问题与客户端的东西无关。在与Bitstamp支持团队通信数周后,他们确认存在上述问题。一个月后终于解决了这个问题。现在一切正常
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.