Refactor trade downloading to handle exceptions only once
This commit is contained in:
		| @@ -364,6 +364,7 @@ def download_trades_history(datadir: Optional[Path], | |||||||
|         logger.debug("New Start: %s", trades[0]['datetime']) |         logger.debug("New Start: %s", trades[0]['datetime']) | ||||||
|         logger.debug("New End: %s", trades[-1]['datetime']) |         logger.debug("New End: %s", trades[-1]['datetime']) | ||||||
|         logger.info(f"New Amount of trades: {len(trades)}") |         logger.info(f"New Amount of trades: {len(trades)}") | ||||||
|  |         return True | ||||||
|  |  | ||||||
|     except Exception as e: |     except Exception as e: | ||||||
|         logger.error( |         logger.error( | ||||||
|   | |||||||
| @@ -751,13 +751,11 @@ class Exchange: | |||||||
|                                   params: Optional[dict] = None) -> List[Dict]: |                                   params: Optional[dict] = None) -> List[Dict]: | ||||||
|         """ |         """ | ||||||
|         Asyncronously gets trade history using fetch_trades. |         Asyncronously gets trade history using fetch_trades. | ||||||
|  |         Handles exchange errors, does one call to the exchange. | ||||||
|         :param pair: Pair to fetch trade data for |         :param pair: Pair to fetch trade data for | ||||||
|         :param since: Since as integer timestamp in milliseconds |         :param since: Since as integer timestamp in milliseconds | ||||||
|         returns tuple: (pair, ticker_interval, ohlcv_list) |         returns tuple: (pair, ticker_interval, ohlcv_list) | ||||||
|         """ |         """ | ||||||
|         if not self.exchange_has("fetchTrades"): |  | ||||||
|             # TODO: Maybe don't stop the bot ... ? |  | ||||||
|             raise OperationalException("This exchange does not suport downloading Trades.") |  | ||||||
|         try: |         try: | ||||||
|             # fetch trades asynchronously |             # fetch trades asynchronously | ||||||
|             if params: |             if params: | ||||||
| @@ -787,14 +785,13 @@ class Exchange: | |||||||
|                                           from_id: Optional[str] = None) -> Tuple[str, List[Dict]]: |                                           from_id: Optional[str] = None) -> Tuple[str, List[Dict]]: | ||||||
|         """ |         """ | ||||||
|         Asyncronously gets trade history using fetch_trades |         Asyncronously gets trade history using fetch_trades | ||||||
|         use this when exchange doesn't use time-based pagination (e.g. Kraken) |         use this when exchange uses id-based iteration | ||||||
|         :param pair: Pair to fetch trade data for |         :param pair: Pair to fetch trade data for | ||||||
|         :param since: Since as integer timestamp in milliseconds |         :param since: Since as integer timestamp in milliseconds | ||||||
|         :param until: Until as integer timestamp in milliseconds |         :param until: Until as integer timestamp in milliseconds | ||||||
|         :param from_id: Download data starting with ID (if id is known). Ignores "since" if set. |         :param from_id: Download data starting with ID (if id is known). Ignores "since" if set. | ||||||
|         returns tuple: (pair, ticker_interval, ohlcv_list) |         returns tuple: (pair, ticker_interval, ohlcv_list) | ||||||
|         """ |         """ | ||||||
|         try: |  | ||||||
|         if self._trades_pagination == 'time': |         if self._trades_pagination == 'time': | ||||||
|             raise OperationalException(f"Wrong method called to get trades for {self.name}") |             raise OperationalException(f"Wrong method called to get trades for {self.name}") | ||||||
|  |  | ||||||
| @@ -826,19 +823,9 @@ class Exchange: | |||||||
|                 break |                 break | ||||||
|  |  | ||||||
|         return (pair, trades) |         return (pair, trades) | ||||||
|         except ccxt.NotSupported as e: |  | ||||||
|             raise OperationalException( |  | ||||||
|                 f'Exchange {self._api.name} does not support fetching historical trade data.' |  | ||||||
|                 f'Message: {e}') from e |  | ||||||
|         except (ccxt.NetworkError, ccxt.ExchangeError) as e: |  | ||||||
|             raise TemporaryError(f'Could not load trade history due to {e.__class__.__name__}. ' |  | ||||||
|                                  f'Message: {e}') from e |  | ||||||
|         except ccxt.BaseError as e: |  | ||||||
|             raise OperationalException(f'Could not fetch trade data. Msg: {e}') from e |  | ||||||
|  |  | ||||||
|     async def _async_get_trade_history_time(self, pair: str, |     async def _async_get_trade_history_time(self, pair: str, until: int, | ||||||
|                                             since: Optional[int] = None, |                                             since: Optional[int] = None) -> Tuple[str, List]: | ||||||
|                                             until: Optional[int] = None) -> Tuple[str, List]: |  | ||||||
|         """ |         """ | ||||||
|         Asyncronously gets trade history using fetch_trades, |         Asyncronously gets trade history using fetch_trades, | ||||||
|         when the exchange uses time-based iteration |         when the exchange uses time-based iteration | ||||||
| @@ -849,7 +836,6 @@ class Exchange: | |||||||
|         """ |         """ | ||||||
|         if self._trades_pagination != 'time': |         if self._trades_pagination != 'time': | ||||||
|             raise OperationalException(f"Wrong method called to get trades for {self.name}") |             raise OperationalException(f"Wrong method called to get trades for {self.name}") | ||||||
|         try: |  | ||||||
|  |  | ||||||
|         trades: List[Dict] = [] |         trades: List[Dict] = [] | ||||||
|         while True: |         while True: | ||||||
| @@ -864,6 +850,27 @@ class Exchange: | |||||||
|                 break |                 break | ||||||
|  |  | ||||||
|         return (pair, trades) |         return (pair, trades) | ||||||
|  |  | ||||||
|  |     async def _async_get_trade_history(self, pair: str, | ||||||
|  |                                        since: Optional[int] = None, | ||||||
|  |                                        until: Optional[int] = None, | ||||||
|  |                                        from_id: Optional[str] = None) -> Tuple[str, List[Dict]]: | ||||||
|  |         """ | ||||||
|  |         Async wrapper handling downloading trades using either time or id based methods. | ||||||
|  |         """ | ||||||
|  |         if not self.exchange_has("fetchTrades"): | ||||||
|  |             # TODO: Maybe don't stop the bot ... ? | ||||||
|  |             raise OperationalException("This exchange does not suport downloading Trades.") | ||||||
|  |         try: | ||||||
|  |             if not until: | ||||||
|  |                 # Current milliseconds | ||||||
|  |                 until = ccxt.Exchange.milliseconds() | ||||||
|  |             if self._trades_pagination == 'time': | ||||||
|  |                 return await self._async_get_trade_history_time(pair=pair, since=since, until=until) | ||||||
|  |             elif self._trades_pagination == 'id': | ||||||
|  |                 return await self._async_get_trade_history_id(pair=pair, since=since, | ||||||
|  |                                                               until=until, from_id=from_id) | ||||||
|  |  | ||||||
|         except ccxt.NotSupported as e: |         except ccxt.NotSupported as e: | ||||||
|             raise OperationalException( |             raise OperationalException( | ||||||
|                 f'Exchange {self._api.name} does not support fetching historical trade data.' |                 f'Exchange {self._api.name} does not support fetching historical trade data.' | ||||||
| @@ -877,7 +884,7 @@ class Exchange: | |||||||
|     def get_historic_trades(self, pair: str, |     def get_historic_trades(self, pair: str, | ||||||
|                             since: Optional[int] = None, |                             since: Optional[int] = None, | ||||||
|                             until: Optional[int] = None, |                             until: Optional[int] = None, | ||||||
|                             from_id: Optional[str] = None) -> List: |                             from_id: Optional[str] = None) -> Tuple[str, List]: | ||||||
|         """ |         """ | ||||||
|         Gets candle history using asyncio and returns the list of candles. |         Gets candle history using asyncio and returns the list of candles. | ||||||
|         Handles all async doing. |         Handles all async doing. | ||||||
| @@ -889,17 +896,9 @@ class Exchange: | |||||||
|         :param from_id: Download data starting with ID (if id is known) |         :param from_id: Download data starting with ID (if id is known) | ||||||
|         :returns List of tickers |         :returns List of tickers | ||||||
|         """ |         """ | ||||||
|         if not until: |  | ||||||
|             # Current milliseconds |  | ||||||
|             until = ccxt.Exchange.milliseconds() |  | ||||||
|         if self._trades_pagination == 'time': |  | ||||||
|             return asyncio.get_event_loop().run_until_complete( |  | ||||||
|                 self._async_get_trade_history_time(pair=pair, since=since, until=until)) |  | ||||||
|  |  | ||||||
|         elif self._trades_pagination == 'id': |  | ||||||
|             # Use id-based trade-downloader |  | ||||||
|         return asyncio.get_event_loop().run_until_complete( |         return asyncio.get_event_loop().run_until_complete( | ||||||
|                 self._async_get_trade_history_id(pair=pair, since=since, |             self._async_get_trade_history(pair=pair, since=since, | ||||||
|                                           until=until, from_id=from_id)) |                                           until=until, from_id=from_id)) | ||||||
|  |  | ||||||
|     @retrier |     @retrier | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user