I am writing some Python code to create an order with the 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)
Unfortunately I get the following error message:
"BinanceAPIException: APIError(code=-1021): Timestamp for this request was 1000ms ahead of the server's time."
I already checked the difference (in miliseconds) between the Binance server time and my local 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
It seems that the recvWindow of 60000 is still not sufficient (but it may not exceed 60K). I still get the same error. Does anybody know how I can solve this issue?
Many thanks in advance!
Probably the PC's time is out of sync.
You can do it using Windows -> Setting-> Time & Language -> Date & Time -> 'Sync Now'.
Screenshot:

I actually used the accepted solution as it is desirable to have the correct windows time in any event.
Nevertheless, here is an alternative code solution (which makes a Binance class and computes the time offset) for the same:
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)
and then call function like this:
binance = Binance(public_key = 'my_pub_key', secret_key = 'my_secret_key', sync=True)
binance.synced('order_market_buy', symbol='BNBBTC', quantity=10)
The link to the full thread is here: https://github.com/sammchardy/python-binance/issues/249
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With