From fb2d1908657625cb6e17158d6f44eaa10fa28027 Mon Sep 17 00:00:00 2001 From: robcaulk Date: Fri, 16 Sep 2022 00:46:55 +0200 Subject: [PATCH] add tests for spice_rack --- freqtrade/freqai/data_drawer.py | 1 - .../freqai/spice_rack/lightgbm_config.json | 3 ++- freqtrade/strategy/interface.py | 6 ++--- tests/freqai/test_freqai_datakitchen.py | 25 +++++++++++++++++++ tests/freqai/test_freqai_interface.py | 19 ++++++++++++++ 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/freqtrade/freqai/data_drawer.py b/freqtrade/freqai/data_drawer.py index 39abcb7a6..e6660b6dc 100644 --- a/freqtrade/freqai/data_drawer.py +++ b/freqtrade/freqai/data_drawer.py @@ -586,7 +586,6 @@ class FreqaiDataDrawer: pairs = self.freqai_info["feature_parameters"].get( "include_corr_pairlist", [] ) - for tf in self.freqai_info["feature_parameters"].get("include_timeframes"): base_dataframes[tf] = dk.slice_dataframe(timerange, historic_data[pair][tf]) if pairs: diff --git a/freqtrade/freqai/spice_rack/lightgbm_config.json b/freqtrade/freqai/spice_rack/lightgbm_config.json index d9e644c92..aff28f037 100644 --- a/freqtrade/freqai/spice_rack/lightgbm_config.json +++ b/freqtrade/freqai/spice_rack/lightgbm_config.json @@ -4,7 +4,8 @@ "enabled": true, "purge_old_models": true, "train_period_days": 4, - "identifier": "uniqe-id", + "backtest_period_days": 2, + "identifier": "spicy-id", "feature_parameters": { "include_timeframes": [ "3m", diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 4d0837d8f..d536968de 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -161,9 +161,6 @@ class IStrategy(ABC, HyperStrategyMixin): from freqtrade.freqai.utils import auto_populate_any_indicators self.populate_any_indicators = types.MethodType( # type: ignore auto_populate_any_indicators, self) - # funcType = type(IStrategy.populate_any_indicators) - # self.populate_any_indicators = funcType(self.freqai.auto_populate_any_indicators, - # self, self.populate_any_indicators) self.freqai_info = self.config["freqai"] @@ -192,8 +189,9 @@ class IStrategy(ABC, HyperStrategyMixin): def setup_freqai_spice_rack(self, config: dict) -> Dict[str, Any]: import json from pathlib import Path + auto_config = config.get('freqai_config', 'lightgbm_config.json') with open(Path('freqtrade') / 'freqai' / 'spice_rack' - / 'lightgbm_config.json') as json_file: + / auto_config) as json_file: freqai_config = json.load(json_file) config['freqai'] = freqai_config['freqai'] config['freqai']['identifier'] = config['freqai_identifier'] diff --git a/tests/freqai/test_freqai_datakitchen.py b/tests/freqai/test_freqai_datakitchen.py index a9e7eac51..bb8d44e21 100644 --- a/tests/freqai/test_freqai_datakitchen.py +++ b/tests/freqai/test_freqai_datakitchen.py @@ -161,3 +161,28 @@ def test_make_train_test_datasets(mocker, freqai_conf): assert data_dictionary assert len(data_dictionary) == 7 assert len(data_dictionary['train_features'].index) == 1916 + + +@pytest.mark.parametrize('indicator', [ + '%-ADArsi-period_10_5m', + 'doesnt_exist', + ]) +def test_spice_extractor(mocker, freqai_conf, indicator, caplog): + freqai, unfiltered_dataframe = make_unfiltered_dataframe(mocker, freqai_conf) + freqai.dk.find_features(unfiltered_dataframe) + + features_filtered, labels_filtered = freqai.dk.filter_features( + unfiltered_dataframe, + freqai.dk.training_features_list, + freqai.dk.label_list, + training_filter=True, + ) + + vec = freqai.dk.spice_extractor(indicator, features_filtered) + if 'doesnt_exist' in indicator: + assert log_has_re( + "User asked spice_rack for", + caplog, + ) + else: + assert len(vec) == 2860 diff --git a/tests/freqai/test_freqai_interface.py b/tests/freqai/test_freqai_interface.py index 2a7cfeb73..70d6f1d7d 100644 --- a/tests/freqai/test_freqai_interface.py +++ b/tests/freqai/test_freqai_interface.py @@ -1,3 +1,4 @@ +import copy import platform import shutil from pathlib import Path @@ -314,3 +315,21 @@ def test_principal_component_analysis(mocker, freqai_conf): assert Path(freqai.dk.data_path / f"{freqai.dk.model_filename}_pca_object.pkl") shutil.rmtree(Path(freqai.dk.full_path)) + + +def test_spice_rack(mocker, default_conf, tmpdir): + default_conf.update({"freqai_spice_rack": "true"}) + default_conf.update({"freqai_config": "test_config.json"}) + default_conf.update({"freqai_identifier": "spicy-id"}) + default_conf.update({"strategy": "freqai_test_spice_rack"}) + default_conf["config_files"] = [Path('config_examples', 'config_freqai.example.json')] + default_conf["timerange"] = "20180110-20180115" + default_conf["datadir"] = Path(default_conf["datadir"]) + default_conf['exchange'].update({'pair_whitelist': + ['ADA/BTC', 'DASH/BTC', 'ETH/BTC', 'LTC/BTC']}) + default_conf["user_data_dir"] = Path(tmpdir) + freqai_conf = copy.deepcopy(default_conf) + + _ = get_patched_freqai_strategy(mocker, freqai_conf) + + assert 'freqai' in freqai_conf