binance-trade-bot icon indicating copy to clipboard operation
binance-trade-bot copied to clipboard

APIError(code=-1021): Timestamp for this request was 1000ms ahead of the server's time.

Open ghost opened this issue 3 years ago • 8 comments

This issue is caused by the machine the bot is running on being outside the recvWindow. Meaning the time on the bot computer is too far off from the binance server's time. This is an issue for any signed API request. The simple solution to solve this is when the bot starts (and perhaps every 4 hours of running) you make a request to /api/v3/time. you subtract the local time (time on computer bot is running on) from the value returned by this API call. This is the serverOffset. Whenever you make a signed request, you subtract this amount from the timestamp used to generate the sha256 hmac signature and the timestamp passed in the POST request.

ghost avatar Mar 18 '21 14:03 ghost

Where is /api/v3/time in project, please?

nguyennhunai avatar Mar 19 '21 02:03 nguyennhunai

I don't know if it is in the code. I would imagine it isnt.

https://github.com/binance/binance-spot-api-docs/blob/master/rest-api.md#timing-security and https://github.com/binance/binance-spot-api-docs/blob/master/rest-api.md#check-server-time

ghost avatar Mar 19 '21 02:03 ghost

Thank you.

nguyennhunai avatar Mar 19 '21 03:03 nguyennhunai

Hi @centrex, we are using an external library to handle communications with Binance, so we naturally don't sign our own requests (let alone craft POST headers). Do you have a fix for this within our code base? Perhaps we should use the library differently, in your opinion?

rel-s avatar Mar 22 '21 19:03 rel-s

Unfortunately, I am not a python developer, so am I of no use when it comes to this codebase. But this needs to be fixed at the signature creation. I would look at the lib you're using and possibly fix it there and submit your PR to whomever is running that.

ghost avatar Mar 22 '21 19:03 ghost

even if you are using someone else's binance lib, they should have this accounted in it. read their documentation. it may be as simple as just calling a method they provide to get the server time and then it's handled automatically.

ghost avatar Mar 22 '21 19:03 ghost

I have been encountering this last night. It kept happening after a certain period of time. binance.exceptions.BinanceAPIException: APIError(code=-1021): Timestamp for this request is outside of the recvWindow

Shah-imran avatar Apr 17 '21 05:04 Shah-imran

@centrex before trying to fix the error that is not there, could you please check your PC time against the value you receive from /api/v3/time and see how different they are?

I bet the problem is on your side and once you set up the NTP sync for your PC time you'll never see this problem again.

lazydroid avatar Jun 24 '21 03:06 lazydroid