python-binance: 'Timestamp for this request is not valid'
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.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 21 (2 by maintainers)
Commits related to this issue
- Merge pull request #2 from sammchardy/master merge bugfix — committed to sammchardy/python-binance by flashnuke 3 years ago
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.
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.
For me it’s something like this
Where the difference would be account for in response times.
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
@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.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!
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.”
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