From c9296dc9a0e08372a464c1da632071fa971cc26e Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 31 Dec 2021 13:01:13 +0100 Subject: [PATCH 1/3] Uvloop_helper should use "get_running_loop" --- freqtrade/rpc/api_server/uvicorn_threaded.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freqtrade/rpc/api_server/uvicorn_threaded.py b/freqtrade/rpc/api_server/uvicorn_threaded.py index 79af659c7..a79c1a5fc 100644 --- a/freqtrade/rpc/api_server/uvicorn_threaded.py +++ b/freqtrade/rpc/api_server/uvicorn_threaded.py @@ -47,7 +47,7 @@ class UvicornServer(uvicorn.Server): else: asyncio.set_event_loop(uvloop.new_event_loop()) try: - loop = asyncio.get_event_loop() + loop = asyncio.get_running_loop() except RuntimeError: # When running in a thread, we'll not have an eventloop yet. loop = asyncio.new_event_loop() From 0277d93a641b855ea52dddb3f6c63ab8c8a9d7ce Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 31 Dec 2021 16:34:15 +0100 Subject: [PATCH 2/3] don't use deprecated `asyncio.get_event_loop()` --- freqtrade/exchange/exchange.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index dc17f6217..8bd9db9f6 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -83,6 +83,8 @@ class Exchange: self._api: ccxt.Exchange = None self._api_async: ccxt_async.Exchange = None self._markets: Dict = {} + self.loop = asyncio.new_event_loop() + asyncio.set_event_loop(self.loop) self._config.update(config) @@ -173,7 +175,7 @@ class Exchange: if (self._api_async and inspect.iscoroutinefunction(self._api_async.close) and self._api_async.session): logger.info("Closing async ccxt session.") - asyncio.get_event_loop().run_until_complete(self._api_async.close()) + self.loop.run_until_complete(self._api_async.close()) def _init_ccxt(self, exchange_config: Dict[str, Any], ccxt_module: CcxtModuleType = ccxt, ccxt_kwargs: Dict = {}) -> ccxt.Exchange: @@ -328,7 +330,7 @@ class Exchange: def _load_async_markets(self, reload: bool = False) -> None: try: if self._api_async: - asyncio.get_event_loop().run_until_complete( + self.loop.run_until_complete( self._api_async.load_markets(reload=reload)) except (asyncio.TimeoutError, ccxt.BaseError) as e: @@ -1229,7 +1231,7 @@ class Exchange: :param since_ms: Timestamp in milliseconds to get history from :return: List with candle (OHLCV) data """ - pair, timeframe, data = asyncio.get_event_loop().run_until_complete( + pair, timeframe, data = self.loop.run_until_complete( self._async_get_historic_ohlcv(pair=pair, timeframe=timeframe, since_ms=since_ms, is_new_pair=is_new_pair)) logger.info(f"Downloaded data for {pair} with length {len(data)}.") @@ -1331,8 +1333,10 @@ class Exchange: results_df = {} # Chunk requests into batches of 100 to avoid overwelming ccxt Throttling for input_coro in chunks(input_coroutines, 100): - results = asyncio.get_event_loop().run_until_complete( - asyncio.gather(*input_coro, return_exceptions=True)) + async def gather_stuff(): + return await asyncio.gather(*input_coro, return_exceptions=True) + + results = self.loop.run_until_complete(gather_stuff()) # handle caching for res in results: @@ -1568,7 +1572,7 @@ class Exchange: if not self.exchange_has("fetchTrades"): raise OperationalException("This exchange does not support downloading Trades.") - return asyncio.get_event_loop().run_until_complete( + return self.loop.run_until_complete( self._async_get_trade_history(pair=pair, since=since, until=until, from_id=from_id)) From 670aed06bf27488e84528f16a018dcbf01ff4652 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 31 Dec 2021 17:35:08 +0100 Subject: [PATCH 3/3] Remove loop for hyperopt. --- freqtrade/optimize/hyperopt.py | 1 + 1 file changed, 1 insertion(+) diff --git a/freqtrade/optimize/hyperopt.py b/freqtrade/optimize/hyperopt.py index 2c7cc0ea7..58da7d0d5 100644 --- a/freqtrade/optimize/hyperopt.py +++ b/freqtrade/optimize/hyperopt.py @@ -422,6 +422,7 @@ class Hyperopt: self.backtesting.exchange.close() self.backtesting.exchange._api = None # type: ignore self.backtesting.exchange._api_async = None # type: ignore + self.backtesting.exchange.loop = None # type: ignore # self.backtesting.exchange = None # type: ignore self.backtesting.pairlists = None # type: ignore