ccxt: Binance error: Timestamp for this request was 1000ms ahead of the server's time.

Trying to use Binance, I get this error trying to call fetch_total_balance (probably other methods also but I didn’t test any others yet):

ccxt.base.errors.ExchangeNotAvailable: binance GET https://api.binance.com/api/v3/account?timestamp=1513233650395329&signature=.... 400 {"code":-1021,"msg":"Timestamp for this request was 1000ms ahead of the server's time."}

I already double checked that my system is syncing to internet time. Could it be a timezone handling issue in CCXT?

I found this thread which shows an example using the python-binance library to check the time delay and I don’t see an issue with that library which is why I am guessing it could be a timezone issue in CCXT. When I run:

>>> import time
>>> from binance.client import Client
>>> client = Client(api_key, api_secret)
>>> int(time.time() * 1000) - client.get_server_time()['serverTime']

I consistently get a result around 260ms.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 6
  • Comments: 51 (16 by maintainers)

Most upvoted comments

@jasonmellone

I just synched my clock

Unfortunately, just synching the clock does not work. You have to set up frequent time synchronization with your system (each 10 minutes). Make sure your timezone is not DST. Unfortunately, without seeing your code and without having the info about your system it’s hard to tell anything exactly. I mean these details: https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-submit-an-issue

If that does not help, you can try solutions from this issue: https://github.com/ccxt/ccxt/pull/1477

Namely, for Binance, you can update to most recent version and do this upon instantiation:

import ccxt
b = ccxt.binance({ 'options': { 'adjustForTimeDifference': True }})

It will adjust for the clock unsynchronization itself, but this is less secure.

from control panel > date and time > internet time change the server to >>>> time.nist.gov

For Windows, try this: https://steemit.com/crypto/@biyi/how-to-resolve-binance-s-timestamp-ahead-of-server-s-time-challenge net stop w32time w32tm /unregister w32tm /register net start w32time w32tm /resync

In Windows 7, manually synchronising with internet time fixed it for me. Go to date and time settings, Internet Time tab > Change settings > update now. The requests to Binance should work and no longer throw the 1021 error.

Windows 7 defaults to synchronising with internet time every week. You can manually change the sync time to daily by editing the registry. Instructions can be found here: https://superuser.com/questions/529367/automatically-sync-windows-time-more-often-than-default?#comment1385466_821537

Running 1.10.445 and still have this issue

Cheers, and thanks for your hard work on this project, its really awesome! It has saved me lots of time.

If my projects every become profitable, I will send you some coin. 😃

Just to clarify for any reader of this, the original change from Dec 2017, which changed the behaviour of binance to not use the nonce, but to always use the milliseconds, was effectively reverted in https://github.com/ccxt/ccxt/commit/36105a438375d599d52f2e057d0484b1ff223a4f - so setting {nonce: somethingCustom} on a binance instance will cause this error.

From Binance:

Igor Kroitor, [23.12.17 21:24]
Hi, again! We're receiving reports of multiple timestamp-related errors from users who have time synch enabled, and who didn't have this problem with the same code previously. Most of the time "timestamp more than 1000ms ahead of server time" and "timestamp outside of recvWindow"... the 2nd one can probably be tackled by choosing a greater recvWindow, however, the first one is making us subtract a thousand from an otherwise-synchronized time... Is there a better way to solve these issues?

Ninj0r, [24.12.17 11:59]
Timestamp related issues on are on the users end. Synch your clocks (and keep them in sync) and you'll not have issues. Synching your clock is not a one time thing; clocks drift for a host of reasons and need to be repeatedly synced. This is why the default on ntpd is to sync clocks at least every 1024 s (17mins 4 seconds). If the user can't prove they synced their clock within that timeframe, I'm not going to investigate it.

Ninj0r, [24.12.17 12:01]
I'll look into this.

Igor Kroitor, [24.12.17 12:07]
they mostly set default time synch in windows 10

Igor Kroitor, [24.12.17 12:08]
(always on)

Ninj0r, [24.12.17 12:08]
Yeah, which typically does something like a time synch once a day if even that.

Guys, from the above, I guess, it follows, that you need to change the frequency of your time synch updates (set smaller intervals for time polling). You can either do this by googling for it or with 3rd party software. Mac users can change their ntpd settings to alter the time synch params. Hope it helps.

“Sync now” button in the Settings fixed the issue. (Windows 11)

@jeffryhartanto

It works by adding ‘options’: { ‘adjustForTimeDifference’: true }, but you said that it’s less secure… Can you elaborate more on why it’s so?

It is so formally or theoretically, but, most likely, for you it practically means little or it is of very little significance for common user applications.

Basically, the exchange requires the timestamps of your requests to be synced with their clock. This is a security requirement, because the synced timestamp is used during authentication. If the timestamp of a request is out of order or is too late, then the exchange can drop it as “malicious”. It is an additional layer of protection, or, in other words, a layer of security. If you enable adjustment for time difference between you and their server then some late packets may be accepted by the exchange. Thus, by enabling the adjustment you violate that exchange’s security requirement. And, in general, when you violate any security requirement – this is less secure. That’s theory.

However, in practice, this mostly means nothing to you, as nobody will really be able to steal your money, gain access to your account, private data, do damage or anything. What it really means is that if a hacker had enough energy he would crack your secret key a little faster with adjustForTimeDifference. But because nobody is even close to having that much energy on Earth and nobody has the technology to overcome those limitations yet, your security is ok, and it’s practically safe to enable the time adjustment.

One side-effect of using adjustForTimeDifference instead of setting up a proper regular/frequent timesync is that working with the same API key from different unsynched computers may throw nonce exceptions from time to time. Apart from that, the level of security mostly remains the same.

So I tried multiple approaches. For my case what worked was a reboot. lol

Rebooting also worked for me. I use a Mac OS.

But its okay, I can just work around it on my side also…

No, I think you’re right, I’ve changed that from nonce = this.nonce() to nonce = this.milliseconds() in the implementation of Binance. Should be available in 5 minutes in the next version.

Rather I would definitely consider this a bug in CCXT.

I’m not quite getting… you redefine the nonce for microseconds for binance, you don’t want to do it any other way, despite that Binance rejects that, and you consider this a bug in ccxt, despite that inside ccxt the default nonce is seconds or milliseconds (not microseconds) for most exchanges?

Moreover, this is documented in the Manual very thoroughly (at least, I thought so, until I read your previous post).

It should not send a microsecond int nonce as a timestamp to Binance, it should always send a real timestamp.

It sends whatever is returned from nonce(). If it is a seconds-timestamp, it will send seconds. If it is a milliseconds-timestamp (Binance does not accept anything else except milliseconds), it will send those. If you set it to microseconds, it will send microseconds and Binance will not like it. This is not a bug, obviously. If you redefine it, it will send your value. Otherwise it will use the value that best-fits a particular exchange by default.

I just tried below and start working fine for me in windows 10 with Python 3.9

binance.nonce = lambda: binance.fetch_time() data = binance.fetch_tickers(symbols)

“Sync now” button in the Settings fixed the issue. (Windows 11)

The sync now button in win 11 fixed the issue but after syncing the system time didn’t change.

Have that error message when my MSI laptop does not reboot after 3-4days. Ran Igors’ code https://github.com/ccxt/ccxt/issues/850#issuecomment-351660975 Had average of 1050ms ahead. Tried this https://github.com/ccxt/ccxt/issues/850#issuecomment-497863347, didn’t resolve my issue. This did resolve issue: https://github.com/ccxt/ccxt/issues/850#issuecomment-783156082

@jorisw nope, since each trade is timestamped.