bybit-exchange / pybit

Official Python3 API connector for Bybit's HTTP and WebSockets APIs.
Other
340 stars 113 forks source link

Сheck your server timestamp or recv_window param #191

Closed it-viktor closed 4 months ago

it-viktor commented 4 months ago

Hi,

https://api.bybit.com:443

invalid request, please check your server timestamp or recv_window param. req_timestamp[1706972576658],server_timestamp[1706972587026],recv_window[5000] (ErrCode: 10002). Added 2.5 seconds to recv_window. 2 retries remain.

I sometimes encounter this error, why? My application works well, making trades every day, but I've noticed that I get this error during volatility. How should I write the program to prevent its occurrence in the future?

Regards, Viktor

Kamsby commented 4 months ago

It says that your request reached to Bybit server after 5 seconds. So you still wants to execute the order ? if yes then check the recv_window value and adjust it to more than 5 seconds.

it-viktor commented 4 months ago

It says that your request reached to Bybit server after 5 seconds. So you still wants to execute the order ? if yes then check the recv_window value and adjust it to more than 5 seconds.

# Thank you for your message! You got me thinking, and I figured out how it works. It turns out there are three attempts

class _V5HTTPManager:
    max_retries: bool = field(default=3)

retries_attempted = self.max_retries
while True:
    retries_attempted -= 1

# With each attempt, recv_window increases
if s_json[ret_code] == 10002:
    recv_window += 2500

# If attempts run out, I should get an error:
if retries_attempted < 0:
    raise FailedRequestError(message="Bad Request. Retries exceeded maximum.")

# In my case, I get errors like "2 retries remain"
# So, it's not a critical situation, and the request eventually goes through, even if not on the first attempt.
# It seems the error message 10002 "please check your server timestamp or recv_window param" can be ignored,
# but I need to react to the error 400 "Bad Request. Retries exceeded maximum."
# This is in my case, where this error occurs very rarely.
it-viktor commented 4 months ago

Done

Warshoow commented 4 months ago

@it-viktor How did you solve this problem ? For me, this happens on every request and increasing the recv_window parameter doesn't solve it.

it-viktor commented 4 months ago

@it-viktor How did you solve this problem ? For me, this happens on every request and increasing the recv_window parameter doesn't solve it.

As I understand it, you don't need to touch the recv_window parameter. You need to understand the reason for the time difference when sending your request. First of all, try checking if there are any time discrepancies between the Bybit server and your server:

from datetime import datetime
from pybit.unified_trading import HTTP

session = HTTP(testnet=False,
               api_key="key",
               api_secret="scrtkey")

# Get bybit srv time
def get_srv_time():
    server_time = session.get_server_time()
    timeNano = server_time['result']['timeNano']
    return int(timeNano[:13])

def unix_to_datetime(unix_time):
    return datetime.fromtimestamp(unix_time/1000.0)

# Print local and bybit time
def prnt_time():
    ptrn = '%d.%m.%Y %H:%M:%S'
    # prnt bb time
    print(unix_to_datetime(get_srv_time()).strftime(ptrn))
    # prnt local time
    print(datetime.now().strftime(ptrn))

prnt_time()

Then think about why there is a delay of several seconds between sending your request to the server and the server receiving your message. What could be the reason?

dextertd commented 4 months ago

Read this link to understand the root cause of the problem and how to rectify it. https://github.com/tiagosiebler/awesome-crypto-examples/wiki/Timestamp-for-this-request-is-outside-of-the-recvWindow