问题
在使用 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 并传递数字2
for 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 秒返回错误数据。
我也尝试过传递start
和end
可选参数。
我曾与其他交易所和经纪人的 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.
要求
- 我该如何解决这个问题?
- 是否有任何社区、讨论组,我可以加入 Bitstamp API ussies?