JKorf / Bybit.Net

A C# .netstandard client library for the Bybit REST and Websocket V5 API focusing on clear usage and models
https://jkorf.github.io/Bybit.Net/
92 stars 64 forks source link

Each consecutive call of BybitClient.UsdPerpetualApi.Account.GetBalancesAsync() results in an error #67

Open ThaiCat opened 1 year ago

ThaiCat commented 1 year ago

example code tested

                var balances = await _BybitClient.UsdPerpetualApi.Account.GetBalancesAsync();
                Log(balances.Error?.ToString());
                await Task.Delay(10000);
                balances = await _BybitClient.UsdPerpetualApi.Account.GetBalancesAsync();
                Log(balances.Error.ToString());
                await Task.Delay(10000);
                balances = await _BybitClient.UsdPerpetualApi.Account.GetBalancesAsync();
                Log(balances.Error.ToString());
                await Task.Delay(10000);
                balances = await _BybitClient.UsdPerpetualApi.Account.GetBalancesAsync();
                Log(balances.Error.ToString())

produced output

[06:24:32.4116] Bybit XRPUSDT [06:24:42.6491] Bybit XRPUSDT 10002: invalid request, please check your timestamp and recv_window param. req_timestamp: 1665534282418 server_timestamp: 1665534281380 recv_window: 5000 [06:24:52.8935] Bybit XRPUSDT 10002: invalid request, please check your timestamp and recv_window param. req_timestamp: 1665534292657 server_timestamp: 1665534291618 recv_window: 5000 [06:25:03.1445] Bybit XRPUSDT 10002: invalid request, please check your timestamp and recv_window param. req_timestamp: 1665534302906 server_timestamp: 1665534301866 recv_window: 5000

JKorf commented 1 year ago

Seems like something is going wrong with the timestamping. Try setting AutoTimestamp to false in the client options.

JKorf commented 1 year ago

I've also done a few fixed in the latest version, can you try again after updating?

mircofx commented 1 year ago

The error is still present when posting orders unfortunately.

kulikov-dev commented 1 year ago

@mircofx Can you provide your part of code? I can't reproduce the issue.. :(

TimYorke commented 1 year ago

I have the same issue with the following code. Sometimes it works every time for 100+ calls over the period of an hour or two, other times it fails on the first attempt each time it's run.

class Account
{
    public Account(string name, CryptoExchange.Net.Authentication.ApiCredentials creds)
    {
        Name = name;
        restClient = new BybitClient(new BybitClientOptions
        {
            ApiCredentials = creds,
            LogLevel = Microsoft.Extensions.Logging.LogLevel.Debug,
            InversePerpetualApiOptions = new CryptoExchange.Net.Objects.RestApiClientOptions { },

        });
    }

    public async IAsyncEnumerable<AssetAmount> GetEquities()
    {
        var result = await restClient.InversePerpetualApi.Account.GetBalancesAsync();
        CheckResult(result);
        foreach (var kvp in result.Data.Where(kvp => kvp.Value.WalletBalance > 0))
        {
            yield return new AssetAmount(kvp.Key, kvp.Value.Equity);
        }
    }
}

GetBalancesAsync fails with message "invalid request, please check your server timestamp or recv_window param. req_timestamp[1686853165596],server_timestamp[1686853164037],recv_window[5000]"

TimYorke commented 1 year ago

I managed to workaround this by tweaking my local PC time (forced a resync with the NTP server). So I guess in order to recreate this issue you need to play around with the local PC time (advance it by a few minutes?)

JKorf commented 1 year ago

Well yes, if the local pc time is changed the timestamping will fail. There isn't an easy way for the library to fix this. The best solution is to make sure the local pc time is synced to a time server more often than default. On windows I've heard that the SP timesync tool works well