2023-06-17 18:35:23 +00:00
|
|
|
import logging
|
2022-07-24 05:32:13 +00:00
|
|
|
import platform
|
2022-07-20 10:56:46 +00:00
|
|
|
import shutil
|
|
|
|
from pathlib import Path
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
2022-07-24 05:32:13 +00:00
|
|
|
import pytest
|
|
|
|
|
2022-07-20 10:56:46 +00:00
|
|
|
from freqtrade.configuration import TimeRange
|
|
|
|
from freqtrade.data.dataprovider import DataProvider
|
2022-10-05 12:08:03 +00:00
|
|
|
from freqtrade.enums import RunMode
|
2022-07-20 10:56:46 +00:00
|
|
|
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
|
2022-10-05 12:08:03 +00:00
|
|
|
from freqtrade.freqai.utils import download_all_data_for_training, get_required_data_timerange
|
2022-10-05 12:11:19 +00:00
|
|
|
from freqtrade.optimize.backtesting import Backtesting
|
2022-10-05 12:08:03 +00:00
|
|
|
from freqtrade.persistence import Trade
|
2022-09-17 08:18:08 +00:00
|
|
|
from freqtrade.plugins.pairlistmanager import PairListManager
|
2023-02-25 19:26:04 +00:00
|
|
|
from tests.conftest import EXMS, create_mock_trades, get_patched_exchange, log_has_re
|
2024-01-14 14:18:10 +00:00
|
|
|
from tests.freqai.conftest import (get_patched_freqai_strategy, is_mac, is_py12, make_rl_config,
|
2023-03-21 14:42:13 +00:00
|
|
|
mock_pytorch_mlp_model_training_parameters)
|
2022-07-20 10:56:46 +00:00
|
|
|
|
|
|
|
|
2022-08-08 18:15:18 +00:00
|
|
|
def is_arm() -> bool:
|
|
|
|
machine = platform.machine()
|
2022-10-22 15:31:55 +00:00
|
|
|
return "arm" in machine or "aarch64" in machine
|
2022-08-08 18:15:18 +00:00
|
|
|
|
2022-08-08 18:34:11 +00:00
|
|
|
|
2023-02-25 14:25:47 +00:00
|
|
|
def can_run_model(model: str) -> None:
|
2024-01-14 14:29:09 +00:00
|
|
|
is_pytorch_model = 'Reinforcement' in model or 'PyTorch' in model
|
|
|
|
|
|
|
|
if is_py12() and ("Catboost" in model or is_pytorch_model):
|
|
|
|
pytest.skip("Model not supported on python 3.12 yet.")
|
|
|
|
|
2023-05-02 05:23:38 +00:00
|
|
|
if is_arm() and "Catboost" in model:
|
2023-03-21 12:34:27 +00:00
|
|
|
pytest.skip("CatBoost is not supported on ARM.")
|
2023-02-25 14:25:47 +00:00
|
|
|
|
2023-03-21 12:34:27 +00:00
|
|
|
if is_pytorch_model and is_mac() and not is_arm():
|
|
|
|
pytest.skip("Reinforcement learning / PyTorch module not available on intel based Mac OS.")
|
2023-02-25 14:25:47 +00:00
|
|
|
|
|
|
|
|
2023-06-07 15:58:27 +00:00
|
|
|
@pytest.mark.parametrize('model, pca, dbscan, float32, can_short, shuffle, buffer, noise', [
|
|
|
|
('LightGBMRegressor', True, False, True, True, False, 0, 0),
|
|
|
|
('XGBoostRegressor', False, True, False, True, False, 10, 0.05),
|
|
|
|
('XGBoostRFRegressor', False, False, False, True, False, 0, 0),
|
|
|
|
('CatboostRegressor', False, False, False, True, True, 0, 0),
|
|
|
|
('PyTorchMLPRegressor', False, False, False, False, False, 0, 0),
|
|
|
|
('PyTorchTransformerRegressor', False, False, False, False, False, 0, 0),
|
|
|
|
('ReinforcementLearner', False, True, False, True, False, 0, 0),
|
|
|
|
('ReinforcementLearner_multiproc', False, False, False, True, False, 0, 0),
|
|
|
|
('ReinforcementLearner_test_3ac', False, False, False, False, False, 0, 0),
|
|
|
|
('ReinforcementLearner_test_3ac', False, False, False, True, False, 0, 0),
|
|
|
|
('ReinforcementLearner_test_4ac', False, False, False, True, False, 0, 0),
|
2022-09-10 18:06:52 +00:00
|
|
|
])
|
2023-05-12 18:01:16 +00:00
|
|
|
def test_extract_data_and_train_model_Standard(mocker, freqai_conf, model, pca,
|
2023-06-07 15:58:27 +00:00
|
|
|
dbscan, float32, can_short, shuffle,
|
|
|
|
buffer, noise):
|
2023-02-16 17:46:01 +00:00
|
|
|
|
2023-02-25 14:25:47 +00:00
|
|
|
can_run_model(model)
|
2023-05-12 18:01:16 +00:00
|
|
|
|
2023-05-14 16:05:49 +00:00
|
|
|
test_tb = True
|
|
|
|
if is_mac():
|
|
|
|
test_tb = False
|
2023-05-14 14:08:00 +00:00
|
|
|
|
2022-10-05 12:08:03 +00:00
|
|
|
model_save_ext = 'joblib'
|
2022-09-10 18:06:52 +00:00
|
|
|
freqai_conf.update({"freqaimodel": model})
|
2022-07-24 05:32:13 +00:00
|
|
|
freqai_conf.update({"timerange": "20180110-20180130"})
|
2022-09-10 18:06:52 +00:00
|
|
|
freqai_conf.update({"strategy": "freqai_test_strat"})
|
2022-11-02 17:34:56 +00:00
|
|
|
freqai_conf['freqai']['feature_parameters'].update({"principal_component_analysis": pca})
|
|
|
|
freqai_conf['freqai']['feature_parameters'].update({"use_DBSCAN_to_remove_outliers": dbscan})
|
2022-11-13 14:38:35 +00:00
|
|
|
freqai_conf.update({"reduce_df_footprint": float32})
|
2022-12-16 10:20:37 +00:00
|
|
|
freqai_conf['freqai']['feature_parameters'].update({"shuffle_after_split": shuffle})
|
2023-02-21 20:08:34 +00:00
|
|
|
freqai_conf['freqai']['feature_parameters'].update({"buffer_train_data_candles": buffer})
|
2023-06-07 15:58:27 +00:00
|
|
|
freqai_conf['freqai']['feature_parameters'].update({"noise_standard_deviation": noise})
|
2022-07-20 10:56:46 +00:00
|
|
|
|
2022-09-14 22:46:35 +00:00
|
|
|
if 'ReinforcementLearner' in model:
|
|
|
|
model_save_ext = 'zip'
|
2022-10-08 10:10:38 +00:00
|
|
|
freqai_conf = make_rl_config(freqai_conf)
|
2022-11-13 14:31:37 +00:00
|
|
|
# test the RL guardrails
|
|
|
|
freqai_conf['freqai']['feature_parameters'].update({"use_SVM_to_remove_outliers": True})
|
2023-05-29 11:33:29 +00:00
|
|
|
freqai_conf['freqai']['feature_parameters'].update({"DI_threshold": 2})
|
2022-11-13 14:31:37 +00:00
|
|
|
freqai_conf['freqai']['data_split_parameters'].update({'shuffle': True})
|
2022-09-14 22:46:35 +00:00
|
|
|
|
2022-12-16 19:31:44 +00:00
|
|
|
if 'test_3ac' in model or 'test_4ac' in model:
|
2022-09-22 21:42:33 +00:00
|
|
|
freqai_conf["freqaimodel_path"] = str(Path(__file__).parents[1] / "freqai" / "test_models")
|
2023-03-07 10:33:54 +00:00
|
|
|
freqai_conf["freqai"]["rl_config"]["drop_ohlc_from_features"] = True
|
2022-09-22 21:42:33 +00:00
|
|
|
|
2023-05-01 13:18:03 +00:00
|
|
|
if 'PyTorch' in model:
|
2023-03-20 16:04:14 +00:00
|
|
|
model_save_ext = 'zip'
|
2023-03-21 14:20:35 +00:00
|
|
|
pytorch_mlp_mtp = mock_pytorch_mlp_model_training_parameters()
|
|
|
|
freqai_conf['freqai']['model_training_parameters'].update(pytorch_mlp_mtp)
|
2023-05-01 13:18:03 +00:00
|
|
|
if 'Transformer' in model:
|
|
|
|
# transformer model takes a window, unlike the MLP regressor
|
|
|
|
freqai_conf.update({"conv_width": 10})
|
2023-03-20 16:04:14 +00:00
|
|
|
|
2022-07-24 05:32:13 +00:00
|
|
|
strategy = get_patched_freqai_strategy(mocker, freqai_conf)
|
|
|
|
exchange = get_patched_exchange(mocker, freqai_conf)
|
|
|
|
strategy.dp = DataProvider(freqai_conf, exchange)
|
|
|
|
strategy.freqai_info = freqai_conf.get("freqai", {})
|
2022-07-23 14:05:25 +00:00
|
|
|
freqai = strategy.freqai
|
2022-07-20 10:56:46 +00:00
|
|
|
freqai.live = True
|
2023-05-14 16:05:49 +00:00
|
|
|
freqai.activate_tensorboard = test_tb
|
2022-12-19 10:49:31 +00:00
|
|
|
freqai.can_short = can_short
|
2022-07-26 08:24:14 +00:00
|
|
|
freqai.dk = FreqaiDataKitchen(freqai_conf)
|
2023-03-26 17:22:52 +00:00
|
|
|
freqai.dk.live = True
|
2022-10-08 10:10:38 +00:00
|
|
|
freqai.dk.set_paths('ADA/BTC', 10000)
|
2022-07-20 10:56:46 +00:00
|
|
|
timerange = TimeRange.parse_timerange("20180110-20180130")
|
2022-07-26 08:24:14 +00:00
|
|
|
freqai.dd.load_all_pair_histories(timerange, freqai.dk)
|
2022-07-20 10:56:46 +00:00
|
|
|
|
|
|
|
freqai.dd.pair_dict = MagicMock()
|
|
|
|
|
2022-10-05 12:08:03 +00:00
|
|
|
data_load_timerange = TimeRange.parse_timerange("20180125-20180130")
|
|
|
|
new_timerange = TimeRange.parse_timerange("20180127-20180130")
|
2022-10-15 06:48:41 +00:00
|
|
|
freqai.dk.set_paths('ADA/BTC', None)
|
2022-07-20 10:56:46 +00:00
|
|
|
|
2022-10-11 17:24:47 +00:00
|
|
|
freqai.train_timer("start", "ADA/BTC")
|
2022-09-03 13:52:29 +00:00
|
|
|
freqai.extract_data_and_train_model(
|
|
|
|
new_timerange, "ADA/BTC", strategy, freqai.dk, data_load_timerange)
|
2022-10-11 17:24:47 +00:00
|
|
|
freqai.train_timer("stop", "ADA/BTC")
|
|
|
|
freqai.dd.save_metric_tracker_to_disk()
|
|
|
|
freqai.dd.save_drawer_to_disk()
|
2022-07-20 10:56:46 +00:00
|
|
|
|
2022-10-11 17:24:47 +00:00
|
|
|
assert Path(freqai.dk.full_path / "metric_tracker.json").is_file()
|
|
|
|
assert Path(freqai.dk.full_path / "pair_dictionary.json").is_file()
|
2022-10-05 12:08:03 +00:00
|
|
|
assert Path(freqai.dk.data_path /
|
|
|
|
f"{freqai.dk.model_filename}_model.{model_save_ext}").is_file()
|
2022-07-25 08:48:04 +00:00
|
|
|
assert Path(freqai.dk.data_path / f"{freqai.dk.model_filename}_metadata.json").is_file()
|
|
|
|
assert Path(freqai.dk.data_path / f"{freqai.dk.model_filename}_trained_df.pkl").is_file()
|
2022-07-20 10:56:46 +00:00
|
|
|
|
|
|
|
shutil.rmtree(Path(freqai.dk.full_path))
|
|
|
|
|
|
|
|
|
2022-10-30 17:08:10 +00:00
|
|
|
@pytest.mark.parametrize('model, strat', [
|
|
|
|
('LightGBMRegressorMultiTarget', "freqai_test_multimodel_strat"),
|
|
|
|
('XGBoostRegressorMultiTarget', "freqai_test_multimodel_strat"),
|
|
|
|
('CatboostRegressorMultiTarget', "freqai_test_multimodel_strat"),
|
2022-11-11 16:45:53 +00:00
|
|
|
('LightGBMClassifierMultiTarget', "freqai_test_multimodel_classifier_strat"),
|
2022-10-30 17:08:10 +00:00
|
|
|
('CatboostClassifierMultiTarget', "freqai_test_multimodel_classifier_strat")
|
2022-09-10 17:57:21 +00:00
|
|
|
])
|
2022-10-30 17:08:10 +00:00
|
|
|
def test_extract_data_and_train_model_MultiTargets(mocker, freqai_conf, model, strat):
|
2023-03-21 13:18:05 +00:00
|
|
|
can_run_model(model)
|
2022-09-10 17:57:21 +00:00
|
|
|
|
2022-07-26 08:24:14 +00:00
|
|
|
freqai_conf.update({"timerange": "20180110-20180130"})
|
2022-10-30 17:08:10 +00:00
|
|
|
freqai_conf.update({"strategy": strat})
|
2022-09-10 17:57:21 +00:00
|
|
|
freqai_conf.update({"freqaimodel": model})
|
2022-07-26 08:24:14 +00:00
|
|
|
strategy = get_patched_freqai_strategy(mocker, freqai_conf)
|
|
|
|
exchange = get_patched_exchange(mocker, freqai_conf)
|
|
|
|
strategy.dp = DataProvider(freqai_conf, exchange)
|
|
|
|
strategy.freqai_info = freqai_conf.get("freqai", {})
|
|
|
|
freqai = strategy.freqai
|
|
|
|
freqai.live = True
|
|
|
|
freqai.dk = FreqaiDataKitchen(freqai_conf)
|
2023-03-26 17:22:52 +00:00
|
|
|
freqai.dk.live = True
|
2022-07-26 08:24:14 +00:00
|
|
|
timerange = TimeRange.parse_timerange("20180110-20180130")
|
|
|
|
freqai.dd.load_all_pair_histories(timerange, freqai.dk)
|
|
|
|
|
|
|
|
freqai.dd.pair_dict = MagicMock()
|
|
|
|
|
|
|
|
data_load_timerange = TimeRange.parse_timerange("20180110-20180130")
|
|
|
|
new_timerange = TimeRange.parse_timerange("20180120-20180130")
|
2022-10-15 06:48:41 +00:00
|
|
|
freqai.dk.set_paths('ADA/BTC', None)
|
2022-07-26 08:24:14 +00:00
|
|
|
|
2022-09-03 13:52:29 +00:00
|
|
|
freqai.extract_data_and_train_model(
|
|
|
|
new_timerange, "ADA/BTC", strategy, freqai.dk, data_load_timerange)
|
2022-07-26 08:24:14 +00:00
|
|
|
|
|
|
|
assert len(freqai.dk.label_list) == 2
|
|
|
|
assert Path(freqai.dk.data_path / f"{freqai.dk.model_filename}_model.joblib").is_file()
|
|
|
|
assert Path(freqai.dk.data_path / f"{freqai.dk.model_filename}_metadata.json").is_file()
|
|
|
|
assert Path(freqai.dk.data_path / f"{freqai.dk.model_filename}_trained_df.pkl").is_file()
|
2022-10-05 12:08:03 +00:00
|
|
|
assert len(freqai.dk.data['training_features_list']) == 14
|
2022-07-26 08:24:14 +00:00
|
|
|
|
|
|
|
shutil.rmtree(Path(freqai.dk.full_path))
|
|
|
|
|
|
|
|
|
2022-09-10 18:17:57 +00:00
|
|
|
@pytest.mark.parametrize('model', [
|
|
|
|
'LightGBMClassifier',
|
|
|
|
'CatboostClassifier',
|
2022-09-10 20:59:11 +00:00
|
|
|
'XGBoostClassifier',
|
2022-10-11 11:17:21 +00:00
|
|
|
'XGBoostRFClassifier',
|
2023-12-18 11:56:04 +00:00
|
|
|
'SKLearnRandomForestClassifier',
|
2023-03-20 16:04:02 +00:00
|
|
|
'PyTorchMLPClassifier',
|
2022-09-10 18:17:57 +00:00
|
|
|
])
|
|
|
|
def test_extract_data_and_train_model_Classifiers(mocker, freqai_conf, model):
|
2023-03-21 13:18:05 +00:00
|
|
|
can_run_model(model)
|
2022-07-24 05:32:13 +00:00
|
|
|
|
2022-09-10 18:17:57 +00:00
|
|
|
freqai_conf.update({"freqaimodel": model})
|
2022-08-06 15:51:21 +00:00
|
|
|
freqai_conf.update({"strategy": "freqai_test_classifier"})
|
|
|
|
freqai_conf.update({"timerange": "20180110-20180130"})
|
|
|
|
strategy = get_patched_freqai_strategy(mocker, freqai_conf)
|
|
|
|
exchange = get_patched_exchange(mocker, freqai_conf)
|
|
|
|
strategy.dp = DataProvider(freqai_conf, exchange)
|
|
|
|
|
|
|
|
strategy.freqai_info = freqai_conf.get("freqai", {})
|
|
|
|
freqai = strategy.freqai
|
|
|
|
freqai.live = True
|
|
|
|
freqai.dk = FreqaiDataKitchen(freqai_conf)
|
2023-03-26 17:22:52 +00:00
|
|
|
freqai.dk.live = True
|
2022-08-06 15:51:21 +00:00
|
|
|
timerange = TimeRange.parse_timerange("20180110-20180130")
|
|
|
|
freqai.dd.load_all_pair_histories(timerange, freqai.dk)
|
|
|
|
|
|
|
|
freqai.dd.pair_dict = MagicMock()
|
|
|
|
|
|
|
|
data_load_timerange = TimeRange.parse_timerange("20180110-20180130")
|
2022-07-24 05:32:13 +00:00
|
|
|
new_timerange = TimeRange.parse_timerange("20180120-20180130")
|
2022-10-15 06:48:41 +00:00
|
|
|
freqai.dk.set_paths('ADA/BTC', None)
|
2022-07-24 05:32:13 +00:00
|
|
|
|
2022-09-03 13:52:29 +00:00
|
|
|
freqai.extract_data_and_train_model(new_timerange, "ADA/BTC",
|
|
|
|
strategy, freqai.dk, data_load_timerange)
|
2022-07-24 05:32:13 +00:00
|
|
|
|
2023-03-20 16:10:17 +00:00
|
|
|
if 'PyTorchMLPClassifier':
|
2023-03-21 14:20:35 +00:00
|
|
|
pytorch_mlp_mtp = mock_pytorch_mlp_model_training_parameters()
|
|
|
|
freqai_conf['freqai']['model_training_parameters'].update(pytorch_mlp_mtp)
|
2023-03-20 16:08:38 +00:00
|
|
|
|
2023-03-18 18:51:30 +00:00
|
|
|
if freqai.dd.model_type == 'joblib':
|
|
|
|
model_file_extension = ".joblib"
|
|
|
|
elif freqai.dd.model_type == "pytorch":
|
|
|
|
model_file_extension = ".zip"
|
|
|
|
else:
|
|
|
|
raise Exception(f"Unsupported model type: {freqai.dd.model_type},"
|
|
|
|
f" can't assign model_file_extension")
|
2022-07-24 05:32:13 +00:00
|
|
|
|
2023-03-18 18:51:30 +00:00
|
|
|
assert Path(freqai.dk.data_path /
|
|
|
|
f"{freqai.dk.model_filename}_model{model_file_extension}").exists()
|
2022-07-24 05:32:13 +00:00
|
|
|
assert Path(freqai.dk.data_path / f"{freqai.dk.model_filename}_metadata.json").exists()
|
|
|
|
assert Path(freqai.dk.data_path / f"{freqai.dk.model_filename}_trained_df.pkl").exists()
|
|
|
|
|
|
|
|
shutil.rmtree(Path(freqai.dk.full_path))
|
|
|
|
|
|
|
|
|
2022-10-05 12:08:03 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"model, num_files, strat",
|
|
|
|
[
|
2022-11-24 17:16:33 +00:00
|
|
|
("LightGBMRegressor", 2, "freqai_test_strat"),
|
|
|
|
("XGBoostRegressor", 2, "freqai_test_strat"),
|
|
|
|
("CatboostRegressor", 2, "freqai_test_strat"),
|
2023-03-21 14:20:35 +00:00
|
|
|
("PyTorchMLPRegressor", 2, "freqai_test_strat"),
|
2023-05-01 13:18:03 +00:00
|
|
|
("PyTorchTransformerRegressor", 2, "freqai_test_strat"),
|
2023-05-14 16:18:33 +00:00
|
|
|
("ReinforcementLearner", 3, "freqai_rl_test_strat"),
|
2022-11-24 17:16:33 +00:00
|
|
|
("XGBoostClassifier", 2, "freqai_test_classifier"),
|
|
|
|
("LightGBMClassifier", 2, "freqai_test_classifier"),
|
2023-03-21 14:20:35 +00:00
|
|
|
("CatboostClassifier", 2, "freqai_test_classifier"),
|
|
|
|
("PyTorchMLPClassifier", 2, "freqai_test_classifier")
|
2022-10-05 12:08:03 +00:00
|
|
|
],
|
|
|
|
)
|
2022-10-08 14:15:48 +00:00
|
|
|
def test_start_backtesting(mocker, freqai_conf, model, num_files, strat, caplog):
|
2023-02-25 14:25:47 +00:00
|
|
|
can_run_model(model)
|
2023-05-14 16:05:49 +00:00
|
|
|
test_tb = True
|
|
|
|
if is_mac():
|
|
|
|
test_tb = False
|
2023-02-25 14:25:47 +00:00
|
|
|
|
2022-09-03 12:00:01 +00:00
|
|
|
freqai_conf.get("freqai", {}).update({"save_backtest_models": True})
|
2022-10-05 12:08:03 +00:00
|
|
|
freqai_conf['runmode'] = RunMode.BACKTEST
|
|
|
|
|
2022-10-07 05:05:56 +00:00
|
|
|
Trade.use_db = False
|
2022-09-23 08:30:52 +00:00
|
|
|
|
2022-10-05 12:08:03 +00:00
|
|
|
freqai_conf.update({"freqaimodel": model})
|
|
|
|
freqai_conf.update({"timerange": "20180120-20180130"})
|
|
|
|
freqai_conf.update({"strategy": strat})
|
|
|
|
|
2022-09-23 08:30:52 +00:00
|
|
|
if 'ReinforcementLearner' in model:
|
2022-10-08 10:10:38 +00:00
|
|
|
freqai_conf = make_rl_config(freqai_conf)
|
2022-09-23 08:30:52 +00:00
|
|
|
|
|
|
|
if 'test_4ac' in model:
|
|
|
|
freqai_conf["freqaimodel_path"] = str(Path(__file__).parents[1] / "freqai" / "test_models")
|
2022-09-23 16:04:43 +00:00
|
|
|
|
2023-05-01 13:18:03 +00:00
|
|
|
if 'PyTorch' in model:
|
2023-03-21 14:20:35 +00:00
|
|
|
pytorch_mlp_mtp = mock_pytorch_mlp_model_training_parameters()
|
|
|
|
freqai_conf['freqai']['model_training_parameters'].update(pytorch_mlp_mtp)
|
2023-05-01 13:18:03 +00:00
|
|
|
if 'Transformer' in model:
|
|
|
|
# transformer model takes a window, unlike the MLP regressor
|
|
|
|
freqai_conf.update({"conv_width": 10})
|
2023-03-21 14:20:35 +00:00
|
|
|
|
2023-01-05 20:54:56 +00:00
|
|
|
freqai_conf.get("freqai", {}).get("feature_parameters", {}).update(
|
|
|
|
{"indicator_periods_candles": [2]})
|
|
|
|
|
2022-07-24 05:32:13 +00:00
|
|
|
strategy = get_patched_freqai_strategy(mocker, freqai_conf)
|
|
|
|
exchange = get_patched_exchange(mocker, freqai_conf)
|
|
|
|
strategy.dp = DataProvider(freqai_conf, exchange)
|
|
|
|
strategy.freqai_info = freqai_conf.get("freqai", {})
|
2022-07-23 14:05:25 +00:00
|
|
|
freqai = strategy.freqai
|
2022-07-20 10:56:46 +00:00
|
|
|
freqai.live = False
|
2023-05-14 16:05:49 +00:00
|
|
|
freqai.activate_tensorboard = test_tb
|
2022-07-26 08:24:14 +00:00
|
|
|
freqai.dk = FreqaiDataKitchen(freqai_conf)
|
2022-07-20 10:56:46 +00:00
|
|
|
timerange = TimeRange.parse_timerange("20180110-20180130")
|
2022-07-26 08:24:14 +00:00
|
|
|
freqai.dd.load_all_pair_histories(timerange, freqai.dk)
|
2022-07-20 10:56:46 +00:00
|
|
|
sub_timerange = TimeRange.parse_timerange("20180110-20180130")
|
2022-12-29 19:35:11 +00:00
|
|
|
_, base_df = freqai.dd.get_base_and_corr_dataframes(sub_timerange, "LTC/BTC", freqai.dk)
|
|
|
|
df = base_df[freqai_conf["timeframe"]]
|
2022-07-20 10:56:46 +00:00
|
|
|
|
2022-07-25 13:07:09 +00:00
|
|
|
metadata = {"pair": "LTC/BTC"}
|
2023-05-12 07:56:44 +00:00
|
|
|
freqai.dk.set_paths('LTC/BTC', None)
|
2022-12-29 19:35:11 +00:00
|
|
|
freqai.start_backtesting(df, metadata, freqai.dk, strategy)
|
2022-07-20 10:56:46 +00:00
|
|
|
model_folders = [x for x in freqai.dd.full_path.iterdir() if x.is_dir()]
|
|
|
|
|
2022-10-05 12:08:03 +00:00
|
|
|
assert len(model_folders) == num_files
|
2022-10-05 15:06:18 +00:00
|
|
|
Trade.use_db = True
|
2022-10-05 12:08:03 +00:00
|
|
|
Backtesting.cleanup()
|
2022-07-20 10:56:46 +00:00
|
|
|
shutil.rmtree(Path(freqai.dk.full_path))
|
|
|
|
|
|
|
|
|
2022-07-25 13:07:09 +00:00
|
|
|
def test_start_backtesting_subdaily_backtest_period(mocker, freqai_conf):
|
|
|
|
freqai_conf.update({"timerange": "20180120-20180124"})
|
2024-01-06 17:14:14 +00:00
|
|
|
freqai_conf['runmode'] = 'backtest'
|
|
|
|
freqai_conf.get("freqai", {}).update({
|
|
|
|
"backtest_period_days": 0.5,
|
|
|
|
"save_backtest_models": True,
|
|
|
|
})
|
2023-01-05 20:54:56 +00:00
|
|
|
freqai_conf.get("freqai", {}).get("feature_parameters", {}).update(
|
|
|
|
{"indicator_periods_candles": [2]})
|
2022-07-25 13:07:09 +00:00
|
|
|
strategy = get_patched_freqai_strategy(mocker, freqai_conf)
|
|
|
|
exchange = get_patched_exchange(mocker, freqai_conf)
|
|
|
|
strategy.dp = DataProvider(freqai_conf, exchange)
|
|
|
|
strategy.freqai_info = freqai_conf.get("freqai", {})
|
|
|
|
freqai = strategy.freqai
|
|
|
|
freqai.live = False
|
2022-07-26 08:24:14 +00:00
|
|
|
freqai.dk = FreqaiDataKitchen(freqai_conf)
|
2022-07-25 13:07:09 +00:00
|
|
|
timerange = TimeRange.parse_timerange("20180110-20180130")
|
2022-07-26 08:24:14 +00:00
|
|
|
freqai.dd.load_all_pair_histories(timerange, freqai.dk)
|
2022-07-25 13:07:09 +00:00
|
|
|
sub_timerange = TimeRange.parse_timerange("20180110-20180130")
|
2022-12-29 19:35:11 +00:00
|
|
|
_, base_df = freqai.dd.get_base_and_corr_dataframes(sub_timerange, "LTC/BTC", freqai.dk)
|
|
|
|
df = base_df[freqai_conf["timeframe"]]
|
2022-07-25 13:07:09 +00:00
|
|
|
|
|
|
|
metadata = {"pair": "LTC/BTC"}
|
2022-12-29 19:35:11 +00:00
|
|
|
freqai.start_backtesting(df, metadata, freqai.dk, strategy)
|
2022-07-25 13:07:09 +00:00
|
|
|
model_folders = [x for x in freqai.dd.full_path.iterdir() if x.is_dir()]
|
2022-10-22 01:48:26 +00:00
|
|
|
|
2022-10-23 18:25:39 +00:00
|
|
|
assert len(model_folders) == 9
|
2022-07-25 13:07:09 +00:00
|
|
|
|
|
|
|
shutil.rmtree(Path(freqai.dk.full_path))
|
|
|
|
|
|
|
|
|
2022-07-24 05:32:13 +00:00
|
|
|
def test_start_backtesting_from_existing_folder(mocker, freqai_conf, caplog):
|
|
|
|
freqai_conf.update({"timerange": "20180120-20180130"})
|
2024-01-06 17:14:14 +00:00
|
|
|
freqai_conf['runmode'] = 'backtest'
|
2022-09-03 12:00:01 +00:00
|
|
|
freqai_conf.get("freqai", {}).update({"save_backtest_models": True})
|
2023-01-05 20:54:56 +00:00
|
|
|
freqai_conf.get("freqai", {}).get("feature_parameters", {}).update(
|
|
|
|
{"indicator_periods_candles": [2]})
|
2022-07-24 05:32:13 +00:00
|
|
|
strategy = get_patched_freqai_strategy(mocker, freqai_conf)
|
|
|
|
exchange = get_patched_exchange(mocker, freqai_conf)
|
|
|
|
strategy.dp = DataProvider(freqai_conf, exchange)
|
|
|
|
strategy.freqai_info = freqai_conf.get("freqai", {})
|
2022-07-23 14:05:25 +00:00
|
|
|
freqai = strategy.freqai
|
2022-07-20 10:56:46 +00:00
|
|
|
freqai.live = False
|
2022-07-26 08:24:14 +00:00
|
|
|
freqai.dk = FreqaiDataKitchen(freqai_conf)
|
2022-07-20 10:56:46 +00:00
|
|
|
timerange = TimeRange.parse_timerange("20180110-20180130")
|
2022-07-26 08:24:14 +00:00
|
|
|
freqai.dd.load_all_pair_histories(timerange, freqai.dk)
|
2023-01-05 20:54:56 +00:00
|
|
|
sub_timerange = TimeRange.parse_timerange("20180101-20180130")
|
2022-12-29 19:35:11 +00:00
|
|
|
_, base_df = freqai.dd.get_base_and_corr_dataframes(sub_timerange, "LTC/BTC", freqai.dk)
|
|
|
|
df = base_df[freqai_conf["timeframe"]]
|
2022-07-20 10:56:46 +00:00
|
|
|
|
2022-11-22 18:15:42 +00:00
|
|
|
pair = "ADA/BTC"
|
|
|
|
metadata = {"pair": pair}
|
|
|
|
freqai.dk.pair = pair
|
2022-12-29 19:35:11 +00:00
|
|
|
freqai.start_backtesting(df, metadata, freqai.dk, strategy)
|
2022-07-20 10:56:46 +00:00
|
|
|
model_folders = [x for x in freqai.dd.full_path.iterdir() if x.is_dir()]
|
|
|
|
|
2022-11-24 17:16:33 +00:00
|
|
|
assert len(model_folders) == 2
|
2022-07-20 10:56:46 +00:00
|
|
|
|
2022-10-05 12:08:03 +00:00
|
|
|
# without deleting the existing folder structure, re-run
|
2022-07-20 10:56:46 +00:00
|
|
|
|
2022-07-24 05:32:13 +00:00
|
|
|
freqai_conf.update({"timerange": "20180120-20180130"})
|
|
|
|
strategy = get_patched_freqai_strategy(mocker, freqai_conf)
|
|
|
|
exchange = get_patched_exchange(mocker, freqai_conf)
|
|
|
|
strategy.dp = DataProvider(freqai_conf, exchange)
|
|
|
|
strategy.freqai_info = freqai_conf.get("freqai", {})
|
2022-07-23 14:05:25 +00:00
|
|
|
freqai = strategy.freqai
|
2022-07-20 10:56:46 +00:00
|
|
|
freqai.live = False
|
2022-07-26 08:24:14 +00:00
|
|
|
freqai.dk = FreqaiDataKitchen(freqai_conf)
|
2022-07-20 10:56:46 +00:00
|
|
|
timerange = TimeRange.parse_timerange("20180110-20180130")
|
2022-07-26 08:24:14 +00:00
|
|
|
freqai.dd.load_all_pair_histories(timerange, freqai.dk)
|
2022-07-20 10:56:46 +00:00
|
|
|
sub_timerange = TimeRange.parse_timerange("20180110-20180130")
|
2022-12-29 19:35:11 +00:00
|
|
|
_, base_df = freqai.dd.get_base_and_corr_dataframes(sub_timerange, "LTC/BTC", freqai.dk)
|
|
|
|
df = base_df[freqai_conf["timeframe"]]
|
2022-10-08 14:15:48 +00:00
|
|
|
|
2022-11-22 18:15:42 +00:00
|
|
|
pair = "ADA/BTC"
|
|
|
|
metadata = {"pair": pair}
|
|
|
|
freqai.dk.pair = pair
|
2022-12-29 19:35:11 +00:00
|
|
|
freqai.start_backtesting(df, metadata, freqai.dk, strategy)
|
2022-07-20 14:14:19 +00:00
|
|
|
|
|
|
|
assert log_has_re(
|
2022-09-03 12:00:01 +00:00
|
|
|
"Found backtesting prediction file ",
|
2022-07-20 10:56:46 +00:00
|
|
|
caplog,
|
|
|
|
)
|
|
|
|
|
2022-11-22 18:15:42 +00:00
|
|
|
pair = "ETH/BTC"
|
|
|
|
metadata = {"pair": pair}
|
|
|
|
freqai.dk.pair = pair
|
2022-12-29 19:35:11 +00:00
|
|
|
freqai.start_backtesting(df, metadata, freqai.dk, strategy)
|
2022-11-22 18:15:42 +00:00
|
|
|
|
2022-09-03 12:00:01 +00:00
|
|
|
path = (freqai.dd.full_path / freqai.dk.backtest_predictions_folder)
|
|
|
|
prediction_files = [x for x in path.iterdir() if x.is_file()]
|
2022-11-22 18:15:42 +00:00
|
|
|
assert len(prediction_files) == 2
|
2022-09-03 12:00:01 +00:00
|
|
|
|
2022-07-20 10:56:46 +00:00
|
|
|
shutil.rmtree(Path(freqai.dk.full_path))
|
2022-07-25 08:48:04 +00:00
|
|
|
|
|
|
|
|
2022-11-17 18:20:07 +00:00
|
|
|
def test_backtesting_fit_live_predictions(mocker, freqai_conf, caplog):
|
2024-01-06 17:14:14 +00:00
|
|
|
freqai_conf['runmode'] = 'backtest'
|
2022-11-17 18:20:07 +00:00
|
|
|
freqai_conf.get("freqai", {}).update({"fit_live_predictions_candles": 10})
|
|
|
|
strategy = get_patched_freqai_strategy(mocker, freqai_conf)
|
|
|
|
exchange = get_patched_exchange(mocker, freqai_conf)
|
|
|
|
strategy.dp = DataProvider(freqai_conf, exchange)
|
|
|
|
strategy.freqai_info = freqai_conf.get("freqai", {})
|
|
|
|
freqai = strategy.freqai
|
|
|
|
freqai.live = False
|
|
|
|
freqai.dk = FreqaiDataKitchen(freqai_conf)
|
|
|
|
timerange = TimeRange.parse_timerange("20180128-20180130")
|
|
|
|
freqai.dd.load_all_pair_histories(timerange, freqai.dk)
|
|
|
|
sub_timerange = TimeRange.parse_timerange("20180129-20180130")
|
|
|
|
corr_df, base_df = freqai.dd.get_base_and_corr_dataframes(sub_timerange, "LTC/BTC", freqai.dk)
|
|
|
|
df = freqai.dk.use_strategy_to_populate_indicators(strategy, corr_df, base_df, "LTC/BTC")
|
2023-03-27 13:23:01 +00:00
|
|
|
df = strategy.set_freqai_targets(df.copy(), metadata={"pair": "LTC/BTC"})
|
|
|
|
df = freqai.dk.remove_special_chars_from_feature_names(df)
|
|
|
|
freqai.dk.get_unique_classes_from_labels(df)
|
2022-11-17 18:20:07 +00:00
|
|
|
freqai.dk.pair = "ADA/BTC"
|
2022-11-30 11:28:45 +00:00
|
|
|
freqai.dk.full_df = df.fillna(0)
|
|
|
|
freqai.dk.full_df
|
2022-11-17 18:20:07 +00:00
|
|
|
assert "&-s_close_mean" not in freqai.dk.full_df.columns
|
|
|
|
assert "&-s_close_std" not in freqai.dk.full_df.columns
|
|
|
|
freqai.backtesting_fit_live_predictions(freqai.dk)
|
|
|
|
assert "&-s_close_mean" in freqai.dk.full_df.columns
|
|
|
|
assert "&-s_close_std" in freqai.dk.full_df.columns
|
|
|
|
shutil.rmtree(Path(freqai.dk.full_path))
|
2022-07-25 09:46:59 +00:00
|
|
|
|
|
|
|
|
2022-09-17 17:17:44 +00:00
|
|
|
def test_plot_feature_importance(mocker, freqai_conf):
|
|
|
|
|
|
|
|
from freqtrade.freqai.utils import plot_feature_importance
|
|
|
|
|
|
|
|
freqai_conf.update({"timerange": "20180110-20180130"})
|
|
|
|
freqai_conf.get("freqai", {}).get("feature_parameters", {}).update(
|
|
|
|
{"princpial_component_analysis": "true"})
|
|
|
|
|
|
|
|
strategy = get_patched_freqai_strategy(mocker, freqai_conf)
|
|
|
|
exchange = get_patched_exchange(mocker, freqai_conf)
|
|
|
|
strategy.dp = DataProvider(freqai_conf, exchange)
|
|
|
|
strategy.freqai_info = freqai_conf.get("freqai", {})
|
|
|
|
freqai = strategy.freqai
|
|
|
|
freqai.live = True
|
|
|
|
freqai.dk = FreqaiDataKitchen(freqai_conf)
|
2023-04-08 11:20:29 +00:00
|
|
|
freqai.dk.live = True
|
2022-09-17 17:17:44 +00:00
|
|
|
timerange = TimeRange.parse_timerange("20180110-20180130")
|
|
|
|
freqai.dd.load_all_pair_histories(timerange, freqai.dk)
|
|
|
|
|
2023-04-08 11:20:29 +00:00
|
|
|
freqai.dd.pair_dict = {"ADA/BTC": {"model_filename": "fake_name",
|
|
|
|
"trained_timestamp": 1, "data_path": "", "extras": {}}}
|
2022-09-17 17:17:44 +00:00
|
|
|
|
|
|
|
data_load_timerange = TimeRange.parse_timerange("20180110-20180130")
|
|
|
|
new_timerange = TimeRange.parse_timerange("20180120-20180130")
|
2023-05-12 07:56:44 +00:00
|
|
|
freqai.dk.set_paths('ADA/BTC', None)
|
2022-09-17 17:17:44 +00:00
|
|
|
|
|
|
|
freqai.extract_data_and_train_model(
|
|
|
|
new_timerange, "ADA/BTC", strategy, freqai.dk, data_load_timerange)
|
|
|
|
|
|
|
|
model = freqai.dd.load_data("ADA/BTC", freqai.dk)
|
|
|
|
|
|
|
|
plot_feature_importance(model, "ADA/BTC", freqai.dk)
|
|
|
|
|
|
|
|
assert Path(freqai.dk.data_path / f"{freqai.dk.model_filename}.html")
|
|
|
|
|
|
|
|
shutil.rmtree(Path(freqai.dk.full_path))
|
2022-09-17 22:02:46 +00:00
|
|
|
|
|
|
|
|
2022-09-17 08:18:08 +00:00
|
|
|
@pytest.mark.parametrize('timeframes,corr_pairs', [
|
|
|
|
(['5m'], ['ADA/BTC', 'DASH/BTC']),
|
2022-09-17 12:19:20 +00:00
|
|
|
(['5m'], ['ADA/BTC', 'DASH/BTC', 'ETH/USDT']),
|
2022-09-17 08:18:08 +00:00
|
|
|
(['5m', '15m'], ['ADA/BTC', 'DASH/BTC', 'ETH/USDT']),
|
|
|
|
])
|
|
|
|
def test_freqai_informative_pairs(mocker, freqai_conf, timeframes, corr_pairs):
|
|
|
|
freqai_conf['freqai']['feature_parameters'].update({
|
|
|
|
'include_timeframes': timeframes,
|
|
|
|
'include_corr_pairlist': corr_pairs,
|
|
|
|
|
|
|
|
})
|
|
|
|
strategy = get_patched_freqai_strategy(mocker, freqai_conf)
|
|
|
|
exchange = get_patched_exchange(mocker, freqai_conf)
|
|
|
|
pairlists = PairListManager(exchange, freqai_conf)
|
|
|
|
strategy.dp = DataProvider(freqai_conf, exchange, pairlists)
|
|
|
|
pairlist = strategy.dp.current_whitelist()
|
|
|
|
|
|
|
|
pairs_a = strategy.informative_pairs()
|
|
|
|
assert len(pairs_a) == 0
|
|
|
|
pairs_b = strategy.gather_informative_pairs()
|
|
|
|
# we expect unique pairs * timeframes
|
|
|
|
assert len(pairs_b) == len(set(pairlist + corr_pairs)) * len(timeframes)
|
2022-10-05 12:08:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_start_set_train_queue(mocker, freqai_conf, caplog):
|
|
|
|
strategy = get_patched_freqai_strategy(mocker, freqai_conf)
|
|
|
|
exchange = get_patched_exchange(mocker, freqai_conf)
|
|
|
|
pairlist = PairListManager(exchange, freqai_conf)
|
|
|
|
strategy.dp = DataProvider(freqai_conf, exchange, pairlist)
|
|
|
|
strategy.freqai_info = freqai_conf.get("freqai", {})
|
|
|
|
freqai = strategy.freqai
|
|
|
|
freqai.live = False
|
|
|
|
|
|
|
|
freqai.train_queue = freqai._set_train_queue()
|
|
|
|
|
|
|
|
assert log_has_re(
|
|
|
|
"Set fresh train queue from whitelist.",
|
|
|
|
caplog,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_required_data_timerange(mocker, freqai_conf):
|
|
|
|
time_range = get_required_data_timerange(freqai_conf)
|
|
|
|
assert (time_range.stopts - time_range.startts) == 177300
|
|
|
|
|
|
|
|
|
2023-11-05 15:23:22 +00:00
|
|
|
def test_download_all_data_for_training(mocker, freqai_conf, caplog, tmp_path):
|
2023-06-17 18:35:23 +00:00
|
|
|
caplog.set_level(logging.DEBUG)
|
2022-10-05 12:08:03 +00:00
|
|
|
strategy = get_patched_freqai_strategy(mocker, freqai_conf)
|
|
|
|
exchange = get_patched_exchange(mocker, freqai_conf)
|
|
|
|
pairlist = PairListManager(exchange, freqai_conf)
|
|
|
|
strategy.dp = DataProvider(freqai_conf, exchange, pairlist)
|
|
|
|
freqai_conf['pairs'] = freqai_conf['exchange']['pair_whitelist']
|
2023-11-05 15:23:22 +00:00
|
|
|
freqai_conf['datadir'] = tmp_path
|
2022-10-05 12:08:03 +00:00
|
|
|
download_all_data_for_training(strategy.dp, freqai_conf)
|
|
|
|
|
|
|
|
assert log_has_re(
|
|
|
|
"Downloading",
|
|
|
|
caplog,
|
|
|
|
)
|
2022-11-13 14:31:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
@pytest.mark.parametrize('dp_exists', [(False), (True)])
|
|
|
|
def test_get_state_info(mocker, freqai_conf, dp_exists, caplog, tickers):
|
|
|
|
|
|
|
|
if is_mac():
|
|
|
|
pytest.skip("Reinforcement learning module not available on intel based Mac OS")
|
2023-12-25 14:58:33 +00:00
|
|
|
if is_py12():
|
|
|
|
pytest.skip("Reinforcement learning currently not available on python 3.12.")
|
2022-11-13 14:31:37 +00:00
|
|
|
|
|
|
|
freqai_conf.update({"freqaimodel": "ReinforcementLearner"})
|
|
|
|
freqai_conf.update({"timerange": "20180110-20180130"})
|
|
|
|
freqai_conf.update({"strategy": "freqai_rl_test_strat"})
|
|
|
|
freqai_conf = make_rl_config(freqai_conf)
|
|
|
|
freqai_conf['entry_pricing']['price_side'] = 'same'
|
|
|
|
freqai_conf['exit_pricing']['price_side'] = 'same'
|
|
|
|
|
|
|
|
strategy = get_patched_freqai_strategy(mocker, freqai_conf)
|
|
|
|
exchange = get_patched_exchange(mocker, freqai_conf)
|
|
|
|
ticker_mock = MagicMock(return_value=tickers()['ETH/BTC'])
|
2023-02-25 19:26:04 +00:00
|
|
|
mocker.patch(f"{EXMS}.fetch_ticker", ticker_mock)
|
2022-11-13 14:31:37 +00:00
|
|
|
strategy.dp = DataProvider(freqai_conf, exchange)
|
|
|
|
|
|
|
|
if not dp_exists:
|
|
|
|
strategy.dp._exchange = None
|
|
|
|
|
|
|
|
strategy.freqai_info = freqai_conf.get("freqai", {})
|
|
|
|
freqai = strategy.freqai
|
|
|
|
freqai.data_provider = strategy.dp
|
|
|
|
freqai.live = True
|
|
|
|
|
|
|
|
Trade.use_db = True
|
|
|
|
create_mock_trades(MagicMock(return_value=0.0025), False, True)
|
|
|
|
freqai.get_state_info("ADA/BTC")
|
|
|
|
freqai.get_state_info("ETH/BTC")
|
|
|
|
|
|
|
|
if not dp_exists:
|
|
|
|
assert log_has_re(
|
|
|
|
"No exchange available",
|
|
|
|
caplog,
|
|
|
|
)
|