DigitalRuby / ExchangeSharp

ExchangeSharp is a powerful, fast and easy to use .NET/C# API for interfacing with many crypto currency exchanges. REST and web sockets are supported.
https://www.digitalruby.com
MIT License
737 stars 374 forks source link

Crashes when getting lots of data from Binance #805

Closed jacobdeboer33 closed 1 year ago

jacobdeboer33 commented 1 year ago

Hi there

About a week ago, I've started getting exceptions when retreiving candles or tickers from Binance. It seems to be a rate limit problem of sorts, but I could not find the cause. Here's a Unit Test that's failing for me right now

` [TestMethod] public void CrashTest() { var API = new ExchangeSharp.ExchangeBinanceAPI(); API.LoadAPIKeys("C:\Source\CryptoTrader\CryptoTrader.Win\Assets\BinanceKeys.bin");

        var markets = API.GetMarketSymbolsMetadataAsync().Result;
        var btcMarkets = markets.Where(x => x.QuoteCurrency == "BTC").ToList();
        foreach (var market in markets)
        {
            var candles = API.GetCandlesAsync(market.MarketSymbol, 86400, DateTime.UtcNow.AddYears(-1), DateTime.UtcNow).Result;
            Console.WriteLine("Candles: " + candles.Count());
        }

        Assert.IsTrue(true);
    }`

I've tested with the latest version on Nuget (1.0.4) and the previous version on github (1.0.3)

Hope you guys can help me.

Thanks, Jacob

vslee commented 1 year ago

I am unable to connect to Binance as I am in the US (it gives me the error "Service unavailable from a restricted location according to 'b. Eligibility' in https://www.binance.com/en/terms. Please contact customer service if you believe you received this message in error."). I did modify your code to try with Binance.US and the test passed...

        [TestMethod]
        public void CrashTest()
        {
            var API = new ExchangeBinanceUSAPI();

            var markets = API.GetMarketSymbolsMetadataAsync().Result;
            var btcMarkets = markets.Where(x => x.QuoteCurrency == "BTC").ToList();
            foreach (var market in markets)
            {
                var candles = API.GetCandlesAsync(market.MarketSymbol, 86400, DateTime.UtcNow.AddYears(-1), DateTime.UtcNow).Result;
                Console.WriteLine("Candles: " + candles.Count());
            }

            Assert.IsTrue(true);
        }
jacobdeboer33 commented 1 year ago

Hi there

Thank you for trying.

Can someone confirm that this test succeeds or failes using Binance Global?

I've read the following in the API changelog found here: API changelog

"Users connected to the websocket API will now be disconnected if their IP is banned due to violation of the IP rate limits(status 418)."

I'm receiving the following exceptions (in this order. App crashes after the last) Some text is GPT translated because parts of my exception are in dutch.

1 System.IO.IOException HResult=0x80131620 Message=Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request. Source=System.Net.Sockets StackTrace: at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)

Inner Exception 1: SocketException: The I/O operation has been aborted because of either a thread exit or an application request.

2 System.Threading.Tasks.TaskCanceledException HResult=0x8013153B Message=The operation was canceled. Source=System.Net.Http StackTrace: at System.Net.Http.HttpConnection.d__64.MoveNext()

Inner Exception 1: IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request.

Inner Exception 2: SocketException: The I/O operation has been aborted because of either a thread exit or an application request.

3 System.Threading.Tasks.TaskCanceledException HResult=0x8013153B Message=The operation was canceled. Source=System.Net.Http StackTrace: at System.Net.Http.HttpConnection.d__64.MoveNext()

Inner Exception 1: IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request.

Inner Exception 2: SocketException: The I/O operation has been aborted because of either a thread exit or an application request.

4 System.TimeoutException HResult=0x80131505 Message=APIRequest timeout Source=ExchangeSharp StackTrace: at ExchangeSharp.APIRequestMaker.d__10.MoveNext()

Inner Exception 1: TaskCanceledException: The operation was canceled.

Inner Exception 2: IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request.

Inner Exception 3: SocketException: The I/O operation has been aborted because of either a thread exit or an application request.

5 System.AggregateException HResult=0x80131500 Message=One or more errors occurred. (APIRequest timeout) Source=System.Private.CoreLib StackTrace: at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)

Inner Exception 1: TimeoutException: APIRequest timeout

Inner Exception 2: TaskCanceledException: The operation was canceled.

Inner Exception 3: IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request.

Inner Exception 4: SocketException: The I/O operation has been aborted because of either a thread exit or an application request.

6 System.Reflection.TargetInvocationException HResult=0x80131604 Message=Exception has been thrown by the target of an invocation. Source=System.Private.CoreLib StackTrace: at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

Inner Exception 1: AggregateException: One or more errors occurred. (APIRequest timeout)

Inner Exception 2: TimeoutException: APIRequest timeout

Inner Exception 3: TaskCanceledException: The operation was canceled.

Inner Exception 4: IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request.

Inner Exception 5: SocketException: The I/O operation has been aborted because of either a thread exit or an application request.

Thanks, Jacob

jacobdeboer33 commented 1 year ago

Problem seems to be resolved by itself. Maybe it was a problem with the API or my internet connection. Bot seems to be running without problems now...

I wil close this issue.

Thanks, Jacob