From 4d2b7a74f100b0aba2c5df9b3a638fd32d96780d Mon Sep 17 00:00:00 2001 From: robcaulk Date: Sun, 23 Oct 2022 20:51:32 +0200 Subject: [PATCH] move record params to utils, use rapidjson --- freqtrade/freqai/freqai_interface.py | 23 ++-------------------- freqtrade/freqai/utils.py | 29 +++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/freqtrade/freqai/freqai_interface.py b/freqtrade/freqai/freqai_interface.py index bba4cecdb..6cb7f79f0 100644 --- a/freqtrade/freqai/freqai_interface.py +++ b/freqtrade/freqai/freqai_interface.py @@ -1,4 +1,3 @@ -import json import logging import threading import time @@ -21,7 +20,7 @@ from freqtrade.exceptions import OperationalException from freqtrade.exchange import timeframe_to_seconds from freqtrade.freqai.data_drawer import FreqaiDataDrawer from freqtrade.freqai.data_kitchen import FreqaiDataKitchen -from freqtrade.freqai.utils import plot_feature_importance +from freqtrade.freqai.utils import plot_feature_importance, record_params from freqtrade.strategy.interface import IStrategy @@ -97,7 +96,7 @@ class IFreqaiModel(ABC): self._threads: List[threading.Thread] = [] self._stop_event = threading.Event() - self.record_params() + record_params(config, self.full_path) def __getstate__(self): """ @@ -536,24 +535,6 @@ class IFreqaiModel(ABC): ) self.full_path.mkdir(parents=True, exist_ok=True) - def record_params(self) -> None: - """ - Records run params in the full path for reproducibility - """ - self.params_record_path = self.full_path / "run_params.json" - - run_params = { - "freqai": self.config.get('freqai', {}), - "timeframe": self.config.get('timeframe'), - "stake_amount": self.config.get('stake_amount'), - "stake_currency": self.config.get('stake_currency'), - "max_open_trades": self.config.get('max_open_trades'), - "pairs": self.config.get('exchange', {}).get('pair_whitelist') - } - - with open(self.params_record_path, "w") as handle: - json.dump(run_params, handle, indent=4) - def extract_data_and_train_model( self, new_trained_timerange: TimeRange, diff --git a/freqtrade/freqai/utils.py b/freqtrade/freqai/utils.py index 22bc1e06e..b3f25d4d1 100644 --- a/freqtrade/freqai/utils.py +++ b/freqtrade/freqai/utils.py @@ -1,9 +1,11 @@ import logging from datetime import datetime, timezone -from typing import Any +from pathlib import Path +from typing import Any, Dict import numpy as np import pandas as pd +import rapidjson from freqtrade.configuration import TimeRange from freqtrade.constants import Config @@ -191,3 +193,28 @@ def plot_feature_importance(model: Any, pair: str, dk: FreqaiDataKitchen, fig.update_layout(title_text=f"Best and worst features by importance {pair}") label = label.replace('&', '').replace('%', '') # escape two FreqAI specific characters store_plot_file(fig, f"{dk.model_filename}-{label}.html", dk.data_path) + + +def record_params(config: Dict[str, Any], full_path: Path) -> None: + """ + Records run params in the full path for reproducibility + """ + params_record_path = full_path / "run_params.json" + + run_params = { + "freqai": config.get('freqai', {}), + "timeframe": config.get('timeframe'), + "stake_amount": config.get('stake_amount'), + "stake_currency": config.get('stake_currency'), + "max_open_trades": config.get('max_open_trades'), + "pairs": config.get('exchange', {}).get('pair_whitelist') + } + + with open(params_record_path, "w") as handle: + rapidjson.dump(run_params, handle, indent=4, default=np_encoder, + number_mode=rapidjson.NM_NATIVE) + + +def np_encoder(self, object): + if isinstance(object, np.generic): + return object.item()