]> Piment Noir Git Repositories - freqai-strategies.git/commitdiff
refactor: extract optuna_load/save_best_params to shared Utils
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 1 May 2026 15:25:13 +0000 (17:25 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 1 May 2026 15:25:13 +0000 (17:25 +0200)
quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py
quickadapter/user_data/strategies/QuickAdapterV3.py
quickadapter/user_data/strategies/Utils.py

index 70747ab49a8d71aaa536cd5a0586f774e3cddd4e..cd4d6108cf707688dfdac277892a0759c14f1363 100644 (file)
@@ -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(
index b5d9ca8b35662d52babbb05342193cf2ac7fbd33..1efd1463b5614b53150a75a543c6fca352712e62 100644 (file)
@@ -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)
index 3050883fe50446814d1925d17af765b534353a1a..d0b59635a20566fa7c16f45579524b5e66e525c4 100644 (file)
@@ -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,