From e66622691fdabd16e231ff48d006ecd527088075 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 1 May 2026 17:25:13 +0200 Subject: [PATCH] refactor: extract optuna_load/save_best_params to shared Utils --- .../freqaimodels/QuickAdapterRegressorV3.py | 27 ++++++----------- .../user_data/strategies/QuickAdapterV3.py | 10 ++----- quickadapter/user_data/strategies/Utils.py | 30 +++++++++++++++++++ 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py b/quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py index 70747ab..cd4d610 100644 --- a/quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py +++ b/quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py @@ -62,6 +62,8 @@ from Utils import ( get_min_max_label_period_candles, get_optuna_study_model_parameters, migrate_config, + optuna_load_best_params, + optuna_save_best_params, soft_extremum, zigzag, ) @@ -3426,29 +3428,18 @@ class QuickAdapterRegressorV3(BaseRegressionModel): ) def optuna_save_best_params(self, pair: str, namespace: OptunaNamespace) -> None: - best_params_path = Path( - self.full_path / f"optuna-{namespace}-best-params-{pair.split('/')[0]}.json" + optuna_save_best_params( + self.full_path, + pair, + namespace, + self.get_optuna_params(pair, namespace), + logger, ) - try: - with best_params_path.open("w", encoding="utf-8") as write_file: - json.dump(self.get_optuna_params(pair, namespace), write_file, indent=4) - except Exception as e: - logger.error( - f"[{pair}] Optuna {namespace} failed to save best params: {e!r}", - exc_info=True, - ) - raise def optuna_load_best_params( self, pair: str, namespace: OptunaNamespace ) -> Optional[dict[str, Any]]: - best_params_path = Path( - self.full_path / f"optuna-{namespace}-best-params-{pair.split('/')[0]}.json" - ) - if best_params_path.is_file(): - with best_params_path.open("r", encoding="utf-8") as read_file: - return json.load(read_file) - return None + return optuna_load_best_params(self.full_path, pair, namespace) @staticmethod def optuna_delete_study( diff --git a/quickadapter/user_data/strategies/QuickAdapterV3.py b/quickadapter/user_data/strategies/QuickAdapterV3.py index b5d9ca8..1efd146 100644 --- a/quickadapter/user_data/strategies/QuickAdapterV3.py +++ b/quickadapter/user_data/strategies/QuickAdapterV3.py @@ -53,6 +53,7 @@ from Utils import ( migrate_config, nan_average, non_zero_diff, + optuna_load_best_params, price_retracement_percent, smooth_label, top_log_return, @@ -2312,11 +2313,4 @@ class QuickAdapterV3(IStrategy): def optuna_load_best_params( self, pair: str, namespace: str ) -> Optional[dict[str, Any]]: - best_params_path = Path( - self.models_full_path - / f"optuna-{namespace}-best-params-{pair.split('/')[0]}.json" - ) - if best_params_path.is_file(): - with best_params_path.open("r", encoding="utf-8") as read_file: - return json.load(read_file) - return None + return optuna_load_best_params(self.models_full_path, pair, namespace) diff --git a/quickadapter/user_data/strategies/Utils.py b/quickadapter/user_data/strategies/Utils.py index 3050883..d0b5963 100644 --- a/quickadapter/user_data/strategies/Utils.py +++ b/quickadapter/user_data/strategies/Utils.py @@ -1,6 +1,7 @@ import copy import functools import hashlib +import json import math from dataclasses import dataclass from enum import IntEnum @@ -2567,6 +2568,35 @@ def _optuna_suggest_int_from_range( return trial.suggest_int(name, int_range[0], int_range[1], log=log) +def optuna_load_best_params( + base_path: Path, pair: str, namespace: str +) -> Optional[dict[str, Any]]: + best_params_path = base_path / f"optuna-{namespace}-best-params-{pair.split('/')[0]}.json" + if best_params_path.is_file(): + with best_params_path.open("r", encoding="utf-8") as read_file: + return json.load(read_file) + return None + + +def optuna_save_best_params( + base_path: Path, + pair: str, + namespace: str, + params: dict[str, Any], + logger: Logger, +) -> None: + best_params_path = base_path / f"optuna-{namespace}-best-params-{pair.split('/')[0]}.json" + try: + with best_params_path.open("w", encoding="utf-8") as write_file: + json.dump(params, write_file, indent=4) + except Exception as e: + logger.error( + f"[{pair}] Optuna {namespace} failed to save best params: {e!r}", + exc_info=True, + ) + raise + + def get_optuna_study_model_parameters( trial: optuna.trial.Trial, regressor: Regressor, -- 2.53.0