Binance order: Timestamp for this request was 1000ms ahead of the server's time

Question:

I am writing some Python code to create an order with the Binance API:

from binance.client import Client

client = Client(API_KEY, SECRET_KEY)

client.create_order(symbol='BTCUSDT',
                    recvWindow=59999, #The value can't be greater than 60K
                    side='BUY',
                    type='MARKET',
                    quantity = 0.004)

Unfortunately I get the following error message:

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

I already checked the difference (in miliseconds) between the Binance server time and my local time:

import time
import requests
import json
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"]) 

OUTPUT: 6997

It seems that the recvWindow of 60000 is still not sufficient (but it may not exceed 60K). I still get the same error.
Does anybody know how I can solve this issue?

Many thanks in advance!

Asked By: Barry

||

Answers:

Manually set your clock back 1 second, ensure that ALL time updates are off. Daylights savings, autosync etc.

Answered By: Jay

Probably the PC’s time is out of sync.

You can do it using Windows -> Setting-> Time & Language -> Date & Time -> ‘Sync Now’.

Screenshot:

I share the picture as well

Answered By: oguzk

Binance server time is behind your server time, because Binance servers do not sync often with ntp servers.

Workaround:

binance.setTimeOffset(-1000); // -1 sec

its if you use: npm binance

Answered By: jmp

I actually used the accepted solution as it is desirable to have the correct windows time in any event.

Nevertheless, here is an alternative code solution (which makes a Binance class and computes the time offset) for the same:

import time
from binance.client import Client

class Binance:
    def __init__(self, public_key = '', secret_key = '', sync = False):
        self.time_offset = 0
        self.b = Client(public_key, secret_key)

        if sync:
            self.time_offset = self._get_time_offset()

    def _get_time_offset(self):
        res = self.b.get_server_time()
        return res['serverTime'] - int(time.time() * 1000)

    def synced(self, fn_name, **args):
        args['timestamp'] = int(time.time() - self.time_offset)
        return getattr(self.b, fn_name)(**args)

and then call function like this:

binance = Binance(public_key = 'my_pub_key', secret_key = 'my_secret_key', sync=True)
binance.synced('order_market_buy',  symbol='BNBBTC', quantity=10)

The link to the full thread is here: https://github.com/sammchardy/python-binance/issues/249

Answered By: D.L

For C#, I created this method to run command line to resync time on windows before running an API on Binance:

public static void ResyncWindowsTime()
{
    var commands = new List<string>() 
    { 
        "net stop w32time",
        "w32tm /unregister",
        "w32tm /register",
        "net start w32time",
        "w32tm /resync"
    };

    var process = new Process();
    var startInfo = new ProcessStartInfo();
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.FileName = "cmd.exe";
    process.StartInfo = startInfo;

    foreach (var command in commands)
    {
        startInfo.Arguments = "/C " + command;
        process.Start();
        process.WaitForExit();
    }
}
Answered By: Desolator