sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
5.88k stars 2.2k forks source link

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

Closed luk0y closed 7 months ago

luk0y commented 7 months ago

OS: Ubuntu 20.04 Python: v3.8.10 python-binance: v1.0.19

The same code worked fine, but after updating to the newest version, I am getting this issue repeatedly.

I checked if there's something wrong with My Server clock using this code :

from binance.client import Client
import time
import requests

for i in range(1, 10):
    local_time1 = int(time.time() * 1000)
    server_time = requests.get("https://fapi.binance.com/fapi/v1/time")
    diff1 = server_time.json()['serverTime'] - local_time1
    local_time2 = int(time.time() * 1000)
    diff2 = local_time2 - server_time.json()['serverTime']
    print("local1: %s server:%s local2: %s diff1:%s diff2:%s" % (local_time1, server_time.json()['serverTime'], local_time2, diff1, diff2))
    time.sleep(2)

I ran this above code at the time I was getting the errors on tmux

local1: 1701960239748 server:1701960239776 local2: 1701960239779 diff1:28 diff2:3
local1: 1701960241780 server:1701960241818 local2: 1701960241821 diff1:38 diff2:3
local1: 1701960243823 server:1701960243847 local2: 1701960243860 diff1:24 diff2:13
local1: 1701960245862 server:1701960245883 local2: 1701960245892 diff1:21 diff2:9
local1: 1701960247894 server:1701960247913 local2: 1701960247916 diff1:19 diff2:3
local1: 1701960249918 server:1701960249936 local2: 1701960249938 diff1:18 diff2:2
local1: 1701960251941 server:1701960251966 local2: 1701960251971 diff1:25 diff2:5
local1: 1701960253973 server:1701960253991 local2: 1701960253994 diff1:18 diff2:3
local1: 1701960255997 server:1701960256024 local2: 1701960256027 diff1:27 diff2:3

The code I am using

from binance import AsyncClient
import asyncio

async def binance_live(bot):

        await bot.futures_account_balance()

async def main():
        bot1 = await AsyncClient.create(xxx, xxx)
        bot2 = await AsyncClient.create(xxx, xxx)

        while True:
            loop.call_soon(asyncio.create_task,binance_live(bot1))
            loop.call_soon(asyncio.create_task,binance_live(bot2))
            await asyncio.sleep(10)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Task exception was never retrieved                                                                                                                                  
future: <Task finished name='Task-2512' coro=<binance_live() done, defined at bedit_prod.py:46> exception=BinanceAPIException(<ClientResponse(https://fapi.binance.co
m/fapi/v2/balance?timestamp=1701958984031&signature=xxxxxxx) [400 Bad Request]>         
<CIMultiDictProxy('Date': 'Thu, 07 Dec 2023 14:23:02 GMT', 'Content-Type': 'application/json', 'Content-Length': '88', 'Connection': 'keep-alive', 'Server': 'Tengine
', 'x-mbx-used-weight-1m': '2', 'x-response-time': '0ms', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS')>    
, 400, '{"code":-1021,"msg":"Timestamp for this request was 1000ms ahead of the server\'s time."}')>         
Traceback (most recent call last):                                                                                                                                  
  File "test_timestamp.py", line 48, in binance_live                                                                                               
    await bot.futures_account_balance()                                                                                               
  File "/usr/local/lib/python3.8/dist-packages/binance/client.py", line 8970, in futures_account_balance         
    return await self._request_futures_api('get', 'balance', True, version=2, data=params)                                                                           
  File "/usr/local/lib/python3.8/dist-packages/binance/client.py", line 7921, in _request_futures_api         
    return await self._request(method, uri, signed, True, **kwargs)                                                                                               
  File "/usr/local/lib/python3.8/dist-packages/binance/client.py", line 7899, in _request                                                                           
    return await self._handle_response(response)                                                                                               
  File "/usr/local/lib/python3.8/dist-packages/binance/client.py", line 7907, in _handle_response         
    raise BinanceAPIException(response, response.status, await response.text())                                                                           
binance.exceptions.BinanceAPIException: APIError(code=-1021): Timestamp for this request was 1000ms ahead of the server's time.         
Task exception was never retrieved.  

I shouldn't get this issue if the time is synchronized, right? Mysteriously, the code works fine if I just rerun it.

luk0y commented 7 months ago

I put this code on start up .. seems like it's the issue. unrelated to python-binance