Ccxt: Binance: Timestamp for this request is outside of the recvWindow

Created on 22 Dec 2017  ·  35Comments  ·  Source: ccxt/ccxt

I modified returned responses in Exchange.php to get more information. This is what i get:

Error when loading details: binance GET https://api.binance.com/api/v3/account?timestamp=1513948454747&signature=xxxxxxxxxxxx

400 not accessible from this location at the moment

{"code":-1021,"msg":"Timestamp for this request is outside of the recvWindow."}

duplicate question

Most helpful comment

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/Linux/FreeBSD/*nix users can change their ntpd settings to alter time synch frequency. Hope it helps.

All 35 comments

Hi! Do you have internet time synchronization enabled in your system clock settings?

I'm also seeing this error on my end. I'm running on an ubuntu VM and yes, time sync is enabled.

MacOS here and I have the same problem. My time is sync as well

Yes, my time is sync as well.

Does this work for you, guys:

// JavaScript
;(async () => {
    binance.verbose = true
    balance = await binance.fetchBalance ({'recvWindow': 10000000})
    console.log (balance)
}) ();
# Python
binance.verbose = True
balance = binance.fetch_balance({'recvWindow': 10000000})
print(balance)
// PHP
$binance->verbose = true;
$balance = $binance->fetch_balance ({'recvWindow': 10000000});
var_dump ($balance);

?

Yes this works. It's strange that you need such a huge window for their API. Something's strange going on there...

@paroxysm apparently, they have time synch issues within their system themselves. The ccxt code for Binance hasn't changed recently, so if they stopped working suddenly because of timing issues, it means their service is experiencing trouble.

I notified their team, they usually respond quickly. I guess, they are aware and are working on this already. If you don't mind, I would close this issue for now, because the reason is now mostly obvious.

I will update you here, when we have a followup on this from Binance. For now, we can't workaround this issue, if their own time-synch is unstable. Let me know if I can help with anything else.

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/Linux/FreeBSD/*nix users can change their ntpd settings to alter time synch frequency. Hope it helps.

ok

This helped me. i set sync to each 12 minutes, it work s now

I added the following to my ntp.conf:
maxpoll 3
minpoll 3

But I still get the same recvWindow error. Is this the right way to increase the syncing frequency on OSX?

I get the same error.

What can be a correct value for timestamp? I know the format, but not when..

example:

['recvWindow' => 10000000, 'timestamp' => time() + 10000000 ]

right?

What can be a correct value for timestamp?

You don't need to set it by hand at all. All you need is to create a new key and set up proper time synchronization on your machine to sync time once every 10 minutes.

['recvWindow' => 10000000, 'timestamp' => time() + 10000000 ]
right?

Nope. The best solution would be to set up time synch and not override their defaults at all.

// JavaScript
;(async () => {
    binance.verbose = true
    balance = await binance.fetchBalance ({'recvWindow': 10000000}) // no override for timestamp
    console.log (balance)
}) ();

An alternative way would be to enable adjustments for the time difference:

// JavaScript
let binance = new ccxt.binance ({
    'options': {
        'adjustForTimeDifference': true,
        'verbose': true, // if needed, not mandatory
        'recvWindow': 10000000, // not really needed
    },
})
;(async () => {
    balance = await binance.fetchBalance ()
    console.log (balance)
}) ();

Hope it helps.

Hello,

I am experiencing the same error. Can anyone please explain me how to synchronize my machine with the server ?
Thanks

@pepsouille it is recommended that you set up proper time synch in your system, follow links from this answer: https://github.com/ccxt/ccxt/issues/936#issuecomment-353774479 or just google for windows time synch frequency for your system.

Alternatively, as was written above you just add the adjustForTimeDifference option along with enableRateLimit and that should resolve the issue for you:

binance = ccxt.binance ({
    'options': {
        'adjustForTimeDifference': True,  # ←---- resolves the timestamp
    },
    'enableRateLimit': True,  # ←---------- required as described in the Manual
    'apiKey': 'YOUR_API_KEY',
    'apiKey': 'YOUR_SECRET',
})
pprint(binance.fetch_balance ())

Let us know if that does not help.

I am running on Ubuntu 16.04 by the way

I am running on Ubuntu 16.04 by the way

screen shot 2018-09-28 at 00 22 50

I am running on Ubuntu 16.04 by the way

Then you should have ntpd (network time protocol daemon), configured, started and running and set it to synch your system time once every 10-15 minutes.

https://www.linuxquestions.org/questions/linux-software-2/how-often-does-ntpd-sync-time-with-its-servers-829538/

@pepsouille the nptd daemon is often included in a Linux distribution, Ubuntu should have it installed by default, you just want to configure and enable it:

https://help.ubuntu.com/lts/serverguide/NTP.html.en

Adding the adjustForTimeDifference may be easier, though.

Hi, I'm running linux mint (ubuntu) and after running ntpstat in terminal it shows time accurate within 87ms. Polling server every 1024 s. Surely this is accurate enough? With reference to "error 1021. Timestamp out of recwindow'

it shows time accurate within 87ms.

Within 87ms from what? From Binance time? ;)

Surely this is accurate enough?

Accurate enough for what? ) You could have the most precise atomic clock on Earth but that doesn't necessarily mean that it's in sync with Binance. They have their own timer which may be different from yours and you have to adjust to it. The adjustment is done like shown here: https://github.com/ccxt/ccxt/issues/936#issuecomment-425247573

Had the same issue, when running here in Italy it does not work, running the same code on a server in Virginia it works. Using the adjustForTimeDifference flag worked everywhere.

working decision is to add parameter
recvWindow=60000 to the http request
(it's easy to edit lines with http request in API code, find them and add parametr recWindow to the request)
https://api.binance.com/api/v3/account?recvWindow=60000&timestamp=1513948454747&signature=xxxxxxxxxxxx

I modified returned responses in Exchange.php to get more information. This is what i get:

Error when loading details: binance GET https://api.binance.com/api/v3/account?timestamp=1513948454747&signature=xxxxxxxxxxxx
400 not accessible from this location at the moment
{"code":-1021,"msg":"Timestamp for this request is outside of the recvWindow."}

Hello i buyed a website project from codecanyon but owner of project didnt reply me. I have just 1 problem about binance api. When i put my binance key and binance secret i got this error.

binance {"code":-1021,"msg":"Timestamp for this request is outside of the recvWindow."}

I cant see any balance on my website and I have a meeting 2 hours later about this website. I can give cpanel informations. I cant find where should i put your codes. Please someone help me this is very important meeting for me. Thank you

@seroberto hi! Unfortunately, we can't support the code that we don't write or maintain, especially, without seeing the code itself. CCXT works in that regard and it is free for the users.

So just can you tell me which code can fix it and where should i put that code?

binance-server-time.js
php-binance-api.php
etc.

@seroberto we are not the authors of the code you mentioned, so we can't physically guess how to fix it. It's pretty much like asking any random guy how to fix any random code.

const binance = new ccxt['binance'] ()
const recvWindow = binance.security.recvWindow
const aheadWindow = 1000


$opt = [
"symbol" => $symbol,
"side" => $side,
"type" => $type,
"quantity" => $quantity,
"recvWindow" => 60000,
];


'recvWindow' => 5 * 1000, // 5 sec, binance default

'timestamp' => $this->nonce (),
'recvWindow' => $this->options['recvWindow'],


Sorry for my questions but its really important for me. Should i fix anything one of the this parts?

@seroberto it's hard to help you without seeing the code, and we are not in charge of it, sorry.

Try changing this line:

const binance = new ccxt['binance'] ()

const binance = new ccxt['binance'] ({ enableRateLimit: true, options: { adjustForTimeDifference: true }})

If that does not help – we won't be able to fix it for you, unfortunately.

@seroberto it can happen if you used the same key on anither timezone, I read it also in the ccxt docs. For example I have two api keys on binance, one is used on the servers and one is used locally during development, otherwise if you use only one key the nonce could miss its main requirement that is to be an increasing sequence. In particular this happens if the timezones are different.

So probably your code works but the issue is on the api key nonce, you probably should use one key for development and one for production.

good luck!

@kroitor

// PHP
$binance->verbose = true;
$balance = $binance->fetch_balance ({'recvWindow': 10000000});
var_dump ($balance);

when i use this code i got this error for this line

$balance = $binance->fetch_balance ({'recvWindow': 10000000});

Syntax error, uxpected '{'

@seroberto it can happen if you used the same key on anither timezone, I read it also in the ccxt docs. For example I have two api keys on binance, one is used on the servers and one is used locally during development, otherwise if you use only one key the nonce could miss its main requirement that is to be an increasing sequence. In particular this happens if the timezones are different.

So probably your code works but the issue is on the api key nonce, you probably should use one key for development and one for production.

good luck!

Hello. How did you get 2 api from binance? When i try to create i have only one choose for api on binance.

@seroberto Change it like so:

$balance = $binance->fetch_balance();

However, if you are not familiar with the programming languages, you might run into another problem right away, since using this lib requires programming skills.

@seroberto it can happen if you used the same key on anither timezone, I read it also in the ccxt docs. For example I have two api keys on binance, one is used on the servers and one is used locally during development, otherwise if you use only one key the nonce could miss its main requirement that is to be an increasing sequence. In particular this happens if the timezones are different.
So probably your code works but the issue is on the api key nonce, you probably should use one key for development and one for production.
good luck!

Hello. How did you get 2 api from binance? When i try to create i have only one choose for api on binance.

Hi,

for example I have two keys now, to add another one I see something like this, I just need to add a label and click create

Screenshot 2020-11-25 at 11 54 25

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stabilus picture stabilus  ·  52Comments

Fcl69 picture Fcl69  ·  45Comments

hexeraction picture hexeraction  ·  40Comments

JovianMoon picture JovianMoon  ·  44Comments

marinsokol5 picture marinsokol5  ·  60Comments