我正在编写一些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."
我已经检查了Binance服务器时间和本地时间之间的差异(毫秒):
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)。我还是会犯同样的错误。有人知道我怎么能解决这个问题吗?
事先非常感谢!
发布于 2022-06-26 16:58:03
可能PC的时间不同步了。
您可以使用Windows ->设置-> Time & Language -> Date和Time -> 'Sync Now‘来完成此操作。
截图:
发布于 2022-03-30 18:43:10
手动将时钟设置为1秒,确保所有时间更新都关闭。日光灯储蓄、自动调节系统等。
发布于 2022-10-26 11:18:35
我实际上使用了接受的解决方案,因为在任何情况下都希望有正确的窗口时间。
然而,这里有一个替代的代码解决方案(它生成Binance类并计算时间偏移):
import time
from binance.client import Client
class Binance:
def __init__(self, public_key = '', secret_key = '', sync = False):
self.time_offset = 0
self.b = Client(public_key, secret_key)
if sync:
self.time_offset = self._get_time_offset()
def _get_time_offset(self):
res = self.b.get_server_time()
return res['serverTime'] - int(time.time() * 1000)
def synced(self, fn_name, **args):
args['timestamp'] = int(time.time() - self.time_offset)
return getattr(self.b, fn_name)(**args)
然后调用这样的函数:
binance = Binance(public_key = 'my_pub_key', secret_key = 'my_secret_key', sync=True)
binance.synced('order_market_buy', symbol='BNBBTC', quantity=10)
指向完整线程的链接如下:https://github.com/sammchardy/python-binance/issues/249
https://stackoverflow.com/questions/71530764
复制相似问题