move record params to utils, use rapidjson

This commit is contained in:
robcaulk 2022-10-23 20:51:32 +02:00
parent bb06745227
commit 4d2b7a74f1
2 changed files with 30 additions and 22 deletions

View File

@ -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,

View File

@ -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()