Python-binance: 'Timestamp for this request is not valid'

Created on 23 Aug 2017  路  15Comments  路  Source: sammchardy/python-binance

Using Python 3.6.1 |Anaconda 4.4.0 (64-bit)

from binance.client import Client
api_key = '...'
api_secret = '...'
client = Client(api_key, api_secret)
time_res = client.get_server_time()
orders = client.get_all_orders(symbol='HSRBTC', limit=10)
Traceback (most recent call last):
  File "E:/PyCharm_Project/Binance/main", line 6, in <module>
    orders = client.get_all_orders(symbol='HSRBTC', limit=10)
  File "D:\Anaconda3\lib\site-packages\binance\client.py", line 222, in get_all_orders
    return self._get('allOrders', True, data=params)
  File "D:\Anaconda3\lib\site-packages\binance\client.py", line 75, in _get
    return self._request('get', path, signed, **kwargs)
  File "D:\Anaconda3\lib\site-packages\binance\client.py", line 63, in _request
    return self._handle_response(response)
  File "D:\Anaconda3\lib\site-packages\binance\client.py", line 71, in _handle_response
    raise BinanceAPIException(response)
binance.exceptions.BinanceAPIException: APIError(code=-1021): Timestamp for this request is not valid.

Most helpful comment

Can you confirm if this happens every time? The info I have about this from the Binance Slack is that it occurs in 2 different cases.
1) The timestamp sent is outside of the serverTime - recvWindow value
2) The timestamp sent is more than 1000ms ahead of the server time

This may occur if your computer time is not correct.

You could run this test and see if there are major differences in the server time and your local time.

from binance.client import Client
import time

api_key = 'xxx'
api_secret = 'xxx'
client = Client(api_key, api_secret)

for i in range(1, 10):
    local_time1 = int(time.time() * 1000)
    server_time = client.get_server_time()
    diff1 = server_time['serverTime'] - local_time1
    local_time2 = int(time.time() * 1000)
    diff2 = local_time2 - server_time['serverTime']
    print("local1: %s server:%s local2: %s diff1:%s diff2:%s" % (local_time1, server_time['serverTime'], local_time2, diff1, diff2))
    time.sleep(2)

For me it's something like this

local1: 1503655899589 server:1503655899741 local2: 1503655899963 diff1:152 diff2:222
local1: 1503655901967 server:1503655902110 local2: 1503655902420 diff1:143 diff2:310
local1: 1503655904425 server:1503655904574 local2: 1503655904785 diff1:149 diff2:211
local1: 1503655906788 server:1503655906938 local2: 1503655907150 diff1:150 diff2:212
local1: 1503655909154 server:1503655909304 local2: 1503655909794 diff1:150 diff2:490
local1: 1503655911796 server:1503655911944 local2: 1503655912156 diff1:148 diff2:212
local1: 1503655914156 server:1503655914305 local2: 1503655914543 diff1:149 diff2:238
local1: 1503655916545 server:1503655916697 local2: 1503655916910 diff1:152 diff2:213
local1: 1503655918911 server:1503655919061 local2: 1503655919274 diff1:150 diff2:213

Where the difference would be account for in response times.

All 15 comments

Can you confirm if this happens every time? The info I have about this from the Binance Slack is that it occurs in 2 different cases.
1) The timestamp sent is outside of the serverTime - recvWindow value
2) The timestamp sent is more than 1000ms ahead of the server time

This may occur if your computer time is not correct.

You could run this test and see if there are major differences in the server time and your local time.

from binance.client import Client
import time

api_key = 'xxx'
api_secret = 'xxx'
client = Client(api_key, api_secret)

for i in range(1, 10):
    local_time1 = int(time.time() * 1000)
    server_time = client.get_server_time()
    diff1 = server_time['serverTime'] - local_time1
    local_time2 = int(time.time() * 1000)
    diff2 = local_time2 - server_time['serverTime']
    print("local1: %s server:%s local2: %s diff1:%s diff2:%s" % (local_time1, server_time['serverTime'], local_time2, diff1, diff2))
    time.sleep(2)

For me it's something like this

local1: 1503655899589 server:1503655899741 local2: 1503655899963 diff1:152 diff2:222
local1: 1503655901967 server:1503655902110 local2: 1503655902420 diff1:143 diff2:310
local1: 1503655904425 server:1503655904574 local2: 1503655904785 diff1:149 diff2:211
local1: 1503655906788 server:1503655906938 local2: 1503655907150 diff1:150 diff2:212
local1: 1503655909154 server:1503655909304 local2: 1503655909794 diff1:150 diff2:490
local1: 1503655911796 server:1503655911944 local2: 1503655912156 diff1:148 diff2:212
local1: 1503655914156 server:1503655914305 local2: 1503655914543 diff1:149 diff2:238
local1: 1503655916545 server:1503655916697 local2: 1503655916910 diff1:152 diff2:213
local1: 1503655918911 server:1503655919061 local2: 1503655919274 diff1:150 diff2:213

Where the difference would be account for in response times.

Thank you. It seems to be caused by the system time error, after reconfiguring the internet time everything is ok now.

local1: 1504969109794 server:1504969113649 local2: 1504969109946 diff1:3855 diff2:-3703
local1: 1504969111946 server:1504969115801 local2: 1504969112097 diff1:3855 diff2:-3704
local1: 1504969114098 server:1504969117958 local2: 1504969114257 diff1:3860 diff2:-3701
local1: 1504969116258 server:1504969120113 local2: 1504969116967 diff1:3855 diff2:-3146
local1: 1504969118967 server:1504969122827 local2: 1504969120553 diff1:3860 diff2:-2274
local1: 1504969122553 server:1504969126413 local2: 1504969122712 diff1:3860 diff2:-3701
local1: 1504969124713 server:1504969128578 local2: 1504969124877 diff1:3865 diff2:-3701
local1: 1504969126878 server:1504969130733 local2: 1504969127039 diff1:3855 diff2:-3694
local1: 1504969129040 server:1504969132899 local2: 1504969129381 diff1:3859 diff2:-3518

i change my windows10 time setting,sync internet time,fix!! thank!

Hi!

To those who might encounter it: an alternative solution to this problem comes from Binance doc, as described here:

An additional parameter, recvWindow, may be sent to specify the number of milliseconds after timestamp the request is valid for. If recvWindow is not sent, it defaults to 5000 milliseconds.

Even if your system clock is out of synch with their server time or with your local time, you can still try passing a larger recvWindow of 10000 or even 20000 milliseconds, though, delays that long can result in a not-very-responsive communication.

Refs:

https://github.com/ccxt-dev/ccxt/issues/300
https://stackoverflow.com/questions/45964187/nonce-override-in-python-ccxt

Hi all, I am encountering the same problem but do not now how to reset/reconfigure my system time - can anyone offer advice? I am running windows 10 - have checked my time settings, they appear to be accurate and synced to the internet.
When I run the code sammchardy posted to check server/local timestamp differences, I get:
local1: 1513695959843 server:1513695958611 local2: 1513695960564 diff1:-1232 diff2:1953 local1: 1513695962565 server:1513695960801 local2: 1513695962714 diff1:-1764 diff2:1913 local1: 1513695964715 server:1513695962954 local2: 1513695964876 diff1:-1761 diff2:1922 local1: 1513695966876 server:1513695965116 local2: 1513695967038 diff1:-1760 diff2:1922 local1: 1513695969038 server:1513695967287 local2: 1513695969207 diff1:-1751 diff2:1920 local1: 1513695971210 server:1513695969450 local2: 1513695971366 diff1:-1760 diff2:1916 local1: 1513695973367 server:1513695971613 local2: 1513695973552 diff1:-1754 diff2:1939 local1: 1513695975552 server:1513695973797 local2: 1513695975718 diff1:-1755 diff2:1921 local1: 1513695977718 server:1513695975980 local2: 1513695977901 diff1:-1738 diff2:1921

@ccscroggin I am having this issue as well. To resync your system time (and potentially try different sync sources) - follow the first answer here: https://answers.microsoft.com/en-us/windows/forum/windows_10-other_settings-winpc/how-to-force-windows-10-time-to-synch-with-a-time/20f3b546-af38-42fb-a2d0-d4df13cc8f43?auth=1

That didn't work for me and I consistently have a ~2000 ms delta. I've also tried increasing the "recvWindow" parameter, e.g.:
client.get_my_trades(symbol=s, recvWindow=200000)

That didn't work either! So now I've hacked something together for the interim to keep me moving... I changed line 137 of client.py to:
kwargs['data']['timestamp'] = int(time.time() * 1000 - 1000)
The alteration I made was just adding the -1000 offset to forge a different time stamp.

I have not used this package yet, but I have encountered this timestamp problem too.
I've tested it with the following code, a few hours ago I got a positive value, and I just got a negative value. Of course, I tried time sync on Windows 10, but the problem happened.

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"]) 

@kroitor Thanks for the reply. I have a question. Does the error "timestamp more than 1000ms ahead of server time" on your link occur even if the timestamp is bigger(later) than the server time? My problem is that the timestamp is bigger than the server time.

Even before yesterday's question, the sync interval was one hour, and the sync was done manually. Today I saw your link and lowered the sync interval to 10 minutes, but still the above code returns a value of about 300.

There is also the problem that the return value of the above code is negative value at some time.(maybe, at night?)

Does the error "timestamp more than 1000ms ahead of server time" on your link occur even if the timestamp is bigger(later) than the server time? My problem is that the timestamp is bigger than the server time.

I replied with solutions to both of those errors, therefore there are 2 links, one for "more than 1000ms" error, the other is for "outside of recvWindow" error.

Even before yesterday's question, the sync interval was one hour, and the sync was done manually. Today I saw your link and lowered the sync interval to 10 minutes, but still the above code returns a value of about 300.
There is also the problem that the return value of the above code is negative value at some time.(maybe, at night?)

If you still have problems after setting up the proper time synch, then try comparing the time you get from Binance to another (neutral) time server.

@FingerDrill an alternative hack would be to subtract the time difference on client-side: https://github.com/ccxt/ccxt/issues/773#issuecomment-349830875

If some one running in mac and docker container, time drift after hibernate could cause this issue. see https://github.com/docker/for-mac/issues/1260, workaround:
docker run --rm --privileged alpine hwclock -s

This is my solution for timestamps issue.
The code made equal windows time to binance server time.

from binance.client import Client
client = Client('api_key', 'api_secret')
import time
import win32api
gt = client.get_server_time()
aa = str(gt)
bb = aa.replace("{'serverTime': ","")
aa = bb.replace("}","")
gg=int(aa)
ff=gg-10799260
uu=ff/1000
yy=int(uu)
tt=time.localtime(yy)
win32api.SetSystemTime(tt[0],tt[1],0,tt[2],tt[3],tt[4],tt[5],0)

"The integer 10799260 for adjusting time zone and fine tuning for miliseconds."

My OS is Windows 7, I change server internet time and resync time and problem solved :)

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

echel0n picture echel0n  路  4Comments

15chrjef picture 15chrjef  路  6Comments

terenceljz picture terenceljz  路  3Comments

bilgecetin picture bilgecetin  路  6Comments

ayy1337 picture ayy1337  路  6Comments