From fd216585236acab80e7f19fc84e9e403477df37d Mon Sep 17 00:00:00 2001 From: Bloodhunter4rc Date: Fri, 26 Jan 2024 16:46:54 +0100 Subject: [PATCH] extend error except, add saving to a file of processed pairlist + docs --- docs/includes/pairlists.md | 5 ++++- freqtrade/plugins/pairlist/RemotePairList.py | 23 +++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/docs/includes/pairlists.md b/docs/includes/pairlists.md index 8e4b43178..950f51f99 100644 --- a/docs/includes/pairlists.md +++ b/docs/includes/pairlists.md @@ -192,7 +192,8 @@ The RemotePairList is defined in the pairlists section of the configuration sett "refresh_period": 1800, "keep_pairlist_on_failure": true, "read_timeout": 60, - "bearer_token": "my-bearer-token" + "bearer_token": "my-bearer-token", + "save_to_file": "user_data/filename.json" } ] ``` @@ -207,6 +208,8 @@ In "append" mode, the retrieved pairlist is added to the original pairlist. All The `pairlist_url` option specifies the URL of the remote server where the pairlist is located, or the path to a local file (if file:/// is prepended). This allows the user to use either a remote server or a local file as the source for the pairlist. +The `save_to_file` option, when provided with a valid filename, saves the processed pairlist to that file in JSON format. This option is optional, and by default, the pairlist is not saved. + The user is responsible for providing a server or local file that returns a JSON object with the following structure: ```json diff --git a/freqtrade/plugins/pairlist/RemotePairList.py b/freqtrade/plugins/pairlist/RemotePairList.py index 2f03678e2..bf6bcd51b 100644 --- a/freqtrade/plugins/pairlist/RemotePairList.py +++ b/freqtrade/plugins/pairlist/RemotePairList.py @@ -52,6 +52,7 @@ class RemotePairList(IPairList): self._read_timeout = self._pairlistconfig.get('read_timeout', 60) self._bearer_token = self._pairlistconfig.get('bearer_token', '') self._init_done = False + self._save_to_file = self._pairlistconfig.get('save_to_file', None) self._last_pairlist: List[Any] = list() if self._mode not in ['whitelist', 'blacklist']: @@ -236,15 +237,15 @@ class RemotePairList(IPairList): if file_path.exists(): with file_path.open() as json_file: - # Load the JSON data into a dictionary - jsonparse = rapidjson.load(json_file, parse_mode=CONFIG_PARSE_MODE) - try: + # Load the JSON data into a dictionary + jsonparse = rapidjson.load(json_file, parse_mode=CONFIG_PARSE_MODE) pairlist = self.process_json(jsonparse) except Exception as e: if self._init_done: pairlist = self.return_last_pairlist() logger.warning(f'Error while processing JSON data: {type(e)}') + logger.debug(f'Error while processing JSON data: {e}') else: raise OperationalException('Error while processing' f'JSON data: {type(e)}') @@ -273,8 +274,24 @@ class RemotePairList(IPairList): self._last_pairlist = list(pairlist) + if self._save_to_file: + self.save_pairlist(pairlist, self._save_to_file) + return pairlist + def save_pairlist(self, pairlist: List[str], filename: str) -> None: + pairlist_data = { + "pairs": pairlist, + "refresh_period": self._refresh_period + } + try: + file_path = Path(filename) + with file_path.open('w') as json_file: + rapidjson.dump(pairlist_data, json_file) + logger.info(f"Processed pairlist saved to {filename}") + except Exception as e: + logger.error(f"Error saving processed pairlist to {filename}: {e}") + def filter_pairlist(self, pairlist: List[str], tickers: Dict) -> List[str]: """ Filters and sorts pairlist and returns the whitelist again.