【发布时间】:2022-06-27 00:58:03
【问题描述】:
我正在编写一些 Python 代码来使用 Binance API 创建订单:
from binance.client import Client
client = Client(API_KEY, SECRET_KEY)
client.create_order(symbol='BTCUSDT',
recvWindow=59999, #The value can't be greater than 60K
side='BUY',
type='MARKET',
quantity = 0.004)
很遗憾,我收到以下错误消息:
"BinanceAPIException: APIError(code=-1021): Timestamp for this request was 1000ms ahead of the server's time."
我已经检查了币安服务器时间和我的本地时间之间的差异(以毫秒为单位):
import time
import requests
import json
url = "https://api.binance.com/api/v1/time"
t = time.time()*1000
r = requests.get(url)
result = json.loads(r.content)
print(int(t)-result["serverTime"])
OUTPUT: 6997
看来60000的recvWindow还是不够用(但可能不会超过60K)。我仍然得到同样的错误。 有人知道我该如何解决这个问题吗?
非常感谢!
【问题讨论】:
-
始终将完整的错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图,不是指向外部门户的链接)(不是在 cmets 中)。还有其他有用的信息。
-
也许您的问题是您计算机中的日期和时间。你的第二个代码给了我负值
-250,但你有正值 -
documentation 显示
if (timestamp < (serverTime + 1000) && (serverTime - timestamp) <= recvWindow) {可以重写为(serverTime - recvWindow) <= timestamp < (serverTime + 1000)并且可能您的timestamp满足(serverTime - recvWindow) <= timestamp但不满足timestamp < (serverTime + 1000)- 这可能是您的错误Timestamp for this request was 1000ms ahead of the server's time -
您可以将
timestamp < (serverTime + 1000)写为timestamp - serverTime < 1000,这与您的int(t)-result["serverTime"]相同,但您得到6997,这不满足6997 < 1000至于我,您必须更正时钟/系统中的时间。或者您可能需要更快的连接。
标签: python datetime cryptoapi binance-api-client