bybit-exchange / pybit

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

Fully functional bot throws a TypeError: can only concatenate str (not "int") to str without me making any changes to the code #174

Closed kdevnoobs closed 10 months ago

kdevnoobs commented 10 months ago

So the bot that I made last week works perfectly. However, I sometimes get a TypeError when I connect to the session with some functions in the code that don't usually give errors using the EXACT same code that works normally. I thought it had something to do with the Spyder IDE so I installed Pycharm and still got the error. All the different bots I made seem to stop working at the same time as well, so if one bugs they all do at the same time. The code will usually run a couple of time before bugging, the only thing that seems to make it work again is restarting my PC. . The errors seems to come from the pybit module itself. Any ideas how to fix this issue?

session = HTTP(api_key=key, api_secret=secret, testnet=False,recv_window = "100000")
result = session.get_wallet_balance(accountType="UNIFIED",coin="USDT")

Traceback (most recent call last): File "C:\Users\Coding\Bybit.py", line 531, in data(sym,inter,session) File "C:\Users\Coding\Bybit.py", line 331, in data bal= session.get_wallet_balance(accountType="UNIFIED",coin="USDT") File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\pybit_v5_account.py", line 25, in get_wallet_balance auth=True, File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\pybit_http_manager.py", line 339, in _submit_request recv_window += 2500 #= str(int(recv_window)+ 2500) TypeError: can only concatenate str (not "int") to str

Here are some of the other functions that usually work and sometime throw an error:

pos=session.get_positions(category="linear",symbol=sym)
data= session.get_kline(category='linear', symbol=sym, interval=inter).get('result')

The strangest thing is that the code will sometimes start perfectly then start bugging in the middle regardless of the calls I make then run a couple of pybit calls after or not. It also randomly freezes while running or never restarts after a time.sleep(3600) function, hence preventing the rest of the code from being executed. It is a fairly simple bot, not some sort of HFT algo. Does print a bit of lines in the console though. What am I missing? Do you reckon a driver code that controls the frequency of the main code for the bot could improve the robustness of the code? Thanks.

kdevnoobs commented 10 months ago

Hi @sangechen @songhaitao @wuhewuhe @dextertd @tomcru @leftcoastgeek @cameronhh @kuegi @verata-veritatis

kdevnoobs commented 10 months ago

Using an interger as receiving windows with +=2500 doesn't work, the error was there before I changed anything, recv_window += 2500 is in the original code and if I use int(10000) I get an error saying I can't. Restarting the PC resyncs the clock hence why it was the only solution to my issue. Removing the recv_window parameter from session :

session = HTTP(api_key=key, api_secret=secret, testnet=False, )

As well as syncing the clock fixed it.

Explanation The auth/signature mechanism works using a time window.

You’re telling the exchange that you signed the request at this timestamp. The server will reject the request if it arrives later than “timestamp + recvWindow”, to prevent replay attacks, on the assumption the request took longer than expected to reach the exchange.

The recv window allows you to tell the API server how much of a delay you're willing to accept. Since this mechanism relies on your machine's clock to get a timestamp, it's important that your machine clock is in sync.

If your machine time is out of sync or your network is suffering from slow requests, you may need more time than the default recv window, but usually it’s better to treat the root cause (sync machine time via something like ntp or fix your internet) than to just keep bumping the recv window to a higher value.

Actions Check your system time. Ideally use a time service like NTP to set your system clock automatically. I cannot be wrong by more than a few seconds or any authenticated requests will fail.

If you’re more than 5 seconds on the wrong time, it’ll break authenticated requests due to the default recv window. You can change the recv window in most connectors, but the real solution is to fix your time first.

I also made this if you want to experiment with your system time vs latency vs server time on bybit spot apis: https://github.com/tiagosiebler/awesome-crypto-examples/blob/master/src/exchanges/bybit/time/log-latency-spot.ts

I'd suggest to try these, with each further step only a last resort if the previous step doesn't fix it:

Ensure your time is manually in sync (using whatever time update/sync service your machine has - e.g. on macOS you can set time and date automatically). Ensure your machine is updating/syncing time automatically. Check that your network connection is stable. If it happens sometimes but not often, or if the above solutions still don't help, you can try increasing the recv window param in the config from the default 5000 ms (5 seconds). However, this should be a last resort since 5 seconds is already quite a gap. If it STILL happens, you can try to "enable_time_sync: true" but that should be a last resort as I've seen this cause more problems than it solves, eventually... I used to have it enabled by default in all my connectors, but I've seen that one slow request can offset it too far and start causing timestamp errors. That's why I'd prefer to keep it disabled. Unix Use something like NTP to sync your system clock automatically: https://ubuntu.com/server/docs/network-ntp

Mac Open your system preferences. Navigate to "Date & Time" (usually under "General"). Enable "Set time and date automatically". Windows Open the "Run command" window (Windows + R) and go to "timedate.cpl" to access the "Date & Time" settings. Navigate to the "Internet Time" tab & click "Change settings". Enable "Synchronize with an Internet time server". Optional, use the atomic clock from PTB as the server: "ptbtime1.ptb.de"