How to change Binance server time using Python?

Viewed 5697

Question

How can I change the server time of Binance, one of the biggest cryptocurrency exchange, using the programming language Python?

Details

  1. I am using the python library `python-binance' to interact with Binance.
  2. When I typed the code below, you get the local time based on your location.
from binance.client import Client
from datetime import datetime

# client = Client('api_key' , 'api_secret')  # Binance API key

raw_server_time = client.get_server_time()
server_time = datetime.fromtimestamp(raw_server_time['serverTime']/1000.0)
server_time

Since I am in France, the result is as follows:

>>> datetime.datetime(2020, 5, 23, 12, 49, 10, 13000)
  1. Even though I am in France, I would like to make Binance think that I am in the Eastern part of the USA (e.g. New York). So I have tried the requests library, supported in the python-binance library as below.
proxies = {
    'http': 'http://168.169.146.12:8080'
}

# in the Client instantiation
client = Client(api_key, api_secret, {'proxies': proxies})

# or on an individual call
client.get_all_orders(symbol='BTCUSDT', requests_params={'proxies': proxies})

But I get the following error message.

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

You can find more detailed information about the code above in the documentation page of the python-binance library. The http server was found in the website free-proxy.cz. A screenshot of the list of New-York based proxies are also provided below.

enter image description here

4 Answers

If you are running Windows 10, or Windows 11, do the following:

  1. Start -> search date and time
  2. Make sure you have set Set time automatically to On
  3. In the opened window you have an option Synchronize your clock, and press Sync now.

I observed this problem sometimes after my PC goes to sleep.

import time
import win32api

client = Client(api_key, api_secret)
server_time = client.get_server_time()
gmtime=time.gmtime(int((server_time["serverTime"])/1000))
win32api.SetSystemTime(gmtime[0],gmtime[1],0,gmtime[2],gmtime[3],gmtime[4],gmtime[5],0) // Needed Administrator Permission

I got this error too, while writing python code in Microsoft Visual Code(MVC). When i kept my Bot running, this also happens over time while bot was active. So i needed the Bot to alter my computer time. I found the following sollution:

How to adjust my computer time:

important to run program as 'Administrator', and have WindowsTime active. Search in 'services' for WindowsTime and make sure this is running/active. Auto-run on startup if needed

At this line of code on startup of the Bot:

import os

os.system('w32tm/resync')

Keeping Bot active over time

After i synced the time at the start, i am monitoring error responses from binance for the TimeStamp error. When the timestamp message is found it will resync my computer time again.

 try:
    ###put for example buy code here###

 except Exception as e:
    print(e)
    substring = 'Timestamp for this request was 1000ms ahead of the server'
    if substring in e.message:
       os.system('w32tm/resync')       

Hopes this helps you all.

Related