uniVocity / univocity-trader

open-source trading framework for java, supports backtesting and live trading with exchanges
577 stars 140 forks source link

Connection times out when unused for a while #46

Closed wwadge closed 4 years ago

wwadge commented 4 years ago

When a signal is triggered, I often get the following exception. I put in an emergency exit (System.exit) upon repeated failures and restart the app at which point the connection works straight away. I haven't yet been able to track this one down yes -- I've already added a keep alive timer that also pings the connection. Leaving this bug report here until I can nail this one down (or perhaps open a new restClient() connection each time?)


   java.io.IOException: java.util.concurrent.TimeoutException
    at org.asynchttpclient.extras.retrofit.AsyncHttpClientCall.toIOException(AsyncHttpClientCall.java:295)
    at org.asynchttpclient.extras.retrofit.AsyncHttpClientCall.execute(AsyncHttpClientCall.java:133)
    at retrofit2.OkHttpCall.execute(OkHttpCall.java:190)
    at com.univocity.trader.exchange.binance.api.client.impl.BinanceApiServiceGenerator.executeSync(BinanceApiServiceGenerator.java:79)
    at com.univocity.trader.exchange.binance.api.client.impl.BinanceApiRestClientImpl.getAllPrices(BinanceApiRestClientImpl.java:102)
    at com.univocity.trader.exchange.binance.BinanceExchange.getLatestPrices(BinanceExchange.java:153)
    at com.univocity.trader.account.TradingManager.getAllPrices(TradingManager.java:107)
    at com.univocity.trader.account.AccountManager.getTotalFundsIn(AccountManager.java:365)
    at com.univocity.trader.account.AccountManager.executeAllocateFunds(AccountManager.java:277)
    at com.univocity.trader.account.AccountManager.allocateFunds(AccountManager.java:325)
    at com.univocity.trader.account.AccountManager.allocateFunds(AccountManager.java:347)
    at com.univocity.trader.account.TradingManager.allocateFunds(TradingManager.java:192)
    at com.univocity.trader.account.Trader.prepareTrade(Trader.java:370)
    at com.univocity.trader.account.Trader.buy(Trader.java:418)
    at com.univocity.trader.account.Trader.processBuy(Trader.java:201)
    at com.univocity.trader.account.Trader.trade(Trader.java:170)
    at com.univocity.trader.strategy.TradingEngine.process(TradingEngine.java:93)
    at com.univocity.trader.candles.CandleProcessor.processCandle(CandleProcessor.```
wwadge commented 4 years ago

Perhaps earlier pings are throwing some exception that kills off the keep-alive thread. Will test and report back

wwadge commented 4 years ago

Unfortunately trapping the exception, whilst probably is a good idea anyway, still doesn't resolve this issue. Might have to go down the route of opening a new connection if need be but I don't think this is the best approach

wwadge commented 4 years ago

Opening a new connection worked for me. Still can't figure out why we get Timeouts on the same connection that has ongoing traffic. Perhaps this is something on Binance side not using "ping" as a signal to reset their idle timeout.

jbax commented 4 years ago

99% of these timeout errors were synchronization issues that I've fixed in a separate branch that I just merged into master. Should be OK now. Thanks a lot for your help!