fix conflicts
This commit is contained in:
117
tests/freqai/conftest.py
Normal file
117
tests/freqai/conftest.py
Normal file
@@ -0,0 +1,117 @@
|
||||
from copy import deepcopy
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from freqtrade.configuration import TimeRange
|
||||
from freqtrade.data.dataprovider import DataProvider
|
||||
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
|
||||
from freqtrade.resolvers import StrategyResolver
|
||||
from freqtrade.resolvers.freqaimodel_resolver import FreqaiModelResolver
|
||||
from tests.conftest import get_patched_exchange
|
||||
|
||||
|
||||
# @pytest.fixture(scope="function")
|
||||
def freqai_conf(default_conf):
|
||||
freqaiconf = deepcopy(default_conf)
|
||||
freqaiconf.update(
|
||||
{
|
||||
"datadir": Path(default_conf["datadir"]),
|
||||
"strategy": "freqai_test_strat",
|
||||
"strategy-path": "freqtrade/tests/strategy/strats",
|
||||
"freqaimodel": "LightGBMPredictionModel",
|
||||
"freqaimodel_path": "freqai/prediction_models",
|
||||
"timerange": "20180110-20180115",
|
||||
"freqai": {
|
||||
"startup_candles": 10000,
|
||||
"purge_old_models": True,
|
||||
"train_period_days": 5,
|
||||
"backtest_period_days": 2,
|
||||
"live_retrain_hours": 0,
|
||||
"expiration_hours": 1,
|
||||
"identifier": "uniqe-id100",
|
||||
"live_trained_timestamp": 0,
|
||||
"feature_parameters": {
|
||||
"include_timeframes": ["5m"],
|
||||
"include_corr_pairlist": ["ADA/BTC", "DASH/BTC"],
|
||||
"label_period_candles": 20,
|
||||
"include_shifted_candles": 1,
|
||||
"DI_threshold": 0.9,
|
||||
"weight_factor": 0.9,
|
||||
"principal_component_analysis": False,
|
||||
"use_SVM_to_remove_outliers": True,
|
||||
"stratify_training_data": 0,
|
||||
"indicator_max_period_candles": 10,
|
||||
"indicator_periods_candles": [10],
|
||||
},
|
||||
"data_split_parameters": {"test_size": 0.33, "random_state": 1},
|
||||
"model_training_parameters": {"n_estimators": 100, "verbosity": 0},
|
||||
},
|
||||
"config_files": [Path('config_examples', 'config_freqai_futures.example.json')]
|
||||
}
|
||||
)
|
||||
freqaiconf['exchange'].update({'pair_whitelist': ['ADA/BTC', 'DASH/BTC', 'ETH/BTC', 'LTC/BTC']})
|
||||
return freqaiconf
|
||||
|
||||
|
||||
def get_patched_data_kitchen(mocker, freqaiconf):
|
||||
dd = mocker.patch('freqtrade.freqai.data_drawer', MagicMock())
|
||||
dk = FreqaiDataKitchen(freqaiconf, dd)
|
||||
return dk
|
||||
|
||||
|
||||
def get_patched_freqai_strategy(mocker, freqaiconf):
|
||||
strategy = StrategyResolver.load_strategy(freqaiconf)
|
||||
strategy.bot_start()
|
||||
|
||||
return strategy
|
||||
|
||||
|
||||
def get_patched_freqaimodel(mocker, freqaiconf):
|
||||
freqaimodel = FreqaiModelResolver.load_freqaimodel(freqaiconf)
|
||||
|
||||
return freqaimodel
|
||||
|
||||
|
||||
def get_freqai_live_analyzed_dataframe(mocker, freqaiconf):
|
||||
strategy = get_patched_freqai_strategy(mocker, freqaiconf)
|
||||
exchange = get_patched_exchange(mocker, freqaiconf)
|
||||
strategy.dp = DataProvider(freqaiconf, exchange)
|
||||
freqai = strategy.model.bridge
|
||||
freqai.live = True
|
||||
freqai.dk = FreqaiDataKitchen(freqaiconf, freqai.dd)
|
||||
timerange = TimeRange.parse_timerange("20180110-20180114")
|
||||
freqai.dk.load_all_pair_histories(timerange)
|
||||
|
||||
strategy.analyze_pair('ADA/BTC', '5m')
|
||||
return strategy.dp.get_analyzed_dataframe('ADA/BTC', '5m')
|
||||
|
||||
|
||||
def get_freqai_analyzed_dataframe(mocker, freqaiconf):
|
||||
strategy = get_patched_freqai_strategy(mocker, freqaiconf)
|
||||
exchange = get_patched_exchange(mocker, freqaiconf)
|
||||
strategy.dp = DataProvider(freqaiconf, exchange)
|
||||
strategy.freqai_info = freqaiconf.get("freqai", {})
|
||||
freqai = strategy.model.bridge
|
||||
freqai.live = True
|
||||
freqai.dk = FreqaiDataKitchen(freqaiconf, freqai.dd)
|
||||
timerange = TimeRange.parse_timerange("20180110-20180114")
|
||||
freqai.dk.load_all_pair_histories(timerange)
|
||||
sub_timerange = TimeRange.parse_timerange("20180111-20180114")
|
||||
corr_df, base_df = freqai.dk.get_base_and_corr_dataframes(sub_timerange, "LTC/BTC")
|
||||
|
||||
return freqai.dk.use_strategy_to_populate_indicators(strategy, corr_df, base_df, 'LTC/BTC')
|
||||
|
||||
|
||||
def get_ready_to_train(mocker, freqaiconf):
|
||||
strategy = get_patched_freqai_strategy(mocker, freqaiconf)
|
||||
exchange = get_patched_exchange(mocker, freqaiconf)
|
||||
strategy.dp = DataProvider(freqaiconf, exchange)
|
||||
strategy.freqai_info = freqaiconf.get("freqai", {})
|
||||
freqai = strategy.model.bridge
|
||||
freqai.live = True
|
||||
freqai.dk = FreqaiDataKitchen(freqaiconf, freqai.dd)
|
||||
timerange = TimeRange.parse_timerange("20180110-20180114")
|
||||
freqai.dk.load_all_pair_histories(timerange)
|
||||
sub_timerange = TimeRange.parse_timerange("20180111-20180114")
|
||||
corr_df, base_df = freqai.dk.get_base_and_corr_dataframes(sub_timerange, "LTC/BTC")
|
||||
return corr_df, base_df, freqai, strategy
|
167
tests/freqai/test_freqai_datakitchen.py
Normal file
167
tests/freqai/test_freqai_datakitchen.py
Normal file
@@ -0,0 +1,167 @@
|
||||
# from unittest.mock import MagicMock
|
||||
# from freqtrade.commands.optimize_commands import setup_optimize_configuration, start_edge
|
||||
import copy
|
||||
import datetime
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from freqtrade.configuration import TimeRange
|
||||
from freqtrade.data.dataprovider import DataProvider
|
||||
# from freqtrade.freqai.data_drawer import FreqaiDataDrawer
|
||||
from freqtrade.exceptions import OperationalException
|
||||
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
|
||||
from tests.conftest import get_patched_exchange
|
||||
from tests.freqai.conftest import freqai_conf, get_patched_data_kitchen, get_patched_freqai_strategy
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"timerange, train_period_days, expected_result",
|
||||
[
|
||||
("20220101-20220201", 30, "20211202-20220201"),
|
||||
("20220301-20220401", 15, "20220214-20220401"),
|
||||
],
|
||||
)
|
||||
def test_create_fulltimerange(
|
||||
timerange, train_period_days, expected_result, default_conf, mocker, caplog
|
||||
):
|
||||
dk = get_patched_data_kitchen(mocker, freqai_conf(copy.deepcopy(default_conf)))
|
||||
assert dk.create_fulltimerange(timerange, train_period_days) == expected_result
|
||||
shutil.rmtree(Path(dk.full_path))
|
||||
|
||||
|
||||
def test_create_fulltimerange_incorrect_backtest_period(mocker, default_conf):
|
||||
dk = get_patched_data_kitchen(mocker, freqai_conf(copy.deepcopy(default_conf)))
|
||||
with pytest.raises(OperationalException, match=r"backtest_period_days must be an integer"):
|
||||
dk.create_fulltimerange("20220101-20220201", 0.5)
|
||||
with pytest.raises(OperationalException, match=r"backtest_period_days must be positive"):
|
||||
dk.create_fulltimerange("20220101-20220201", -1)
|
||||
shutil.rmtree(Path(dk.full_path))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"timerange, train_period_days, backtest_period_days, expected_result",
|
||||
[
|
||||
("20220101-20220201", 30, 7, 9),
|
||||
("20220101-20220201", 30, 0.5, 120),
|
||||
("20220101-20220201", 10, 1, 80),
|
||||
],
|
||||
)
|
||||
def test_split_timerange(
|
||||
mocker, default_conf, timerange, train_period_days, backtest_period_days, expected_result
|
||||
):
|
||||
freqaiconf = freqai_conf(copy.deepcopy(default_conf))
|
||||
freqaiconf.update({"timerange": "20220101-20220401"})
|
||||
dk = get_patched_data_kitchen(mocker, freqaiconf)
|
||||
tr_list, bt_list = dk.split_timerange(timerange, train_period_days, backtest_period_days)
|
||||
assert len(tr_list) == len(bt_list) == expected_result
|
||||
|
||||
with pytest.raises(
|
||||
OperationalException, match=r"train_period_days must be an integer greater than 0."
|
||||
):
|
||||
dk.split_timerange("20220101-20220201", -1, 0.5)
|
||||
shutil.rmtree(Path(dk.full_path))
|
||||
|
||||
|
||||
def test_update_historic_data(mocker, default_conf):
|
||||
freqaiconf = freqai_conf(copy.deepcopy(default_conf))
|
||||
strategy = get_patched_freqai_strategy(mocker, freqaiconf)
|
||||
exchange = get_patched_exchange(mocker, freqaiconf)
|
||||
strategy.dp = DataProvider(freqaiconf, exchange)
|
||||
freqai = strategy.model.bridge
|
||||
freqai.live = True
|
||||
freqai.dk = FreqaiDataKitchen(freqaiconf, freqai.dd)
|
||||
timerange = TimeRange.parse_timerange("20180110-20180114")
|
||||
|
||||
freqai.dk.load_all_pair_histories(timerange)
|
||||
historic_candles = len(freqai.dd.historic_data["ADA/BTC"]["5m"])
|
||||
dp_candles = len(strategy.dp.get_pair_dataframe("ADA/BTC", "5m"))
|
||||
candle_difference = dp_candles - historic_candles
|
||||
freqai.dk.update_historic_data(strategy)
|
||||
|
||||
updated_historic_candles = len(freqai.dd.historic_data["ADA/BTC"]["5m"])
|
||||
|
||||
assert updated_historic_candles - historic_candles == candle_difference
|
||||
shutil.rmtree(Path(freqai.dk.full_path))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"timestamp, expected",
|
||||
[
|
||||
(datetime.datetime.now(tz=datetime.timezone.utc).timestamp() - 7200, True),
|
||||
(datetime.datetime.now(tz=datetime.timezone.utc).timestamp(), False),
|
||||
],
|
||||
)
|
||||
def test_check_if_model_expired(mocker, default_conf, timestamp, expected):
|
||||
freqaiconf = freqai_conf(copy.deepcopy(default_conf))
|
||||
dk = get_patched_data_kitchen(mocker, freqaiconf)
|
||||
assert dk.check_if_model_expired(timestamp) == expected
|
||||
shutil.rmtree(Path(dk.full_path))
|
||||
|
||||
|
||||
def test_load_all_pairs_histories(mocker, default_conf):
|
||||
freqaiconf = freqai_conf(copy.deepcopy(default_conf))
|
||||
strategy = get_patched_freqai_strategy(mocker, freqaiconf)
|
||||
exchange = get_patched_exchange(mocker, freqaiconf)
|
||||
strategy.dp = DataProvider(freqaiconf, exchange)
|
||||
freqai = strategy.model.bridge
|
||||
freqai.live = True
|
||||
freqai.dk = FreqaiDataKitchen(freqaiconf, freqai.dd)
|
||||
timerange = TimeRange.parse_timerange("20180110-20180114")
|
||||
freqai.dk.load_all_pair_histories(timerange)
|
||||
|
||||
assert len(freqai.dd.historic_data.keys()) == len(
|
||||
freqaiconf.get("exchange", {}).get("pair_whitelist")
|
||||
)
|
||||
assert len(freqai.dd.historic_data["ADA/BTC"]) == len(
|
||||
freqaiconf.get("freqai", {}).get("feature_parameters", {}).get("include_timeframes")
|
||||
)
|
||||
shutil.rmtree(Path(freqai.dk.full_path))
|
||||
|
||||
|
||||
def test_get_base_and_corr_dataframes(mocker, default_conf):
|
||||
freqaiconf = freqai_conf(copy.deepcopy(default_conf))
|
||||
strategy = get_patched_freqai_strategy(mocker, freqaiconf)
|
||||
exchange = get_patched_exchange(mocker, freqaiconf)
|
||||
strategy.dp = DataProvider(freqaiconf, exchange)
|
||||
freqai = strategy.model.bridge
|
||||
freqai.live = True
|
||||
freqai.dk = FreqaiDataKitchen(freqaiconf, freqai.dd)
|
||||
timerange = TimeRange.parse_timerange("20180110-20180114")
|
||||
freqai.dk.load_all_pair_histories(timerange)
|
||||
sub_timerange = TimeRange.parse_timerange("20180111-20180114")
|
||||
corr_df, base_df = freqai.dk.get_base_and_corr_dataframes(sub_timerange, "LTC/BTC")
|
||||
|
||||
num_tfs = len(
|
||||
freqaiconf.get("freqai", {}).get("feature_parameters", {}).get("include_timeframes")
|
||||
)
|
||||
|
||||
assert len(base_df.keys()) == num_tfs
|
||||
|
||||
assert len(corr_df.keys()) == len(
|
||||
freqaiconf.get("freqai", {}).get("feature_parameters", {}).get("include_corr_pairlist")
|
||||
)
|
||||
|
||||
assert len(corr_df["ADA/BTC"].keys()) == num_tfs
|
||||
shutil.rmtree(Path(freqai.dk.full_path))
|
||||
|
||||
|
||||
def test_use_strategy_to_populate_indicators(mocker, default_conf):
|
||||
freqaiconf = freqai_conf(copy.deepcopy(default_conf))
|
||||
strategy = get_patched_freqai_strategy(mocker, freqaiconf)
|
||||
exchange = get_patched_exchange(mocker, freqaiconf)
|
||||
strategy.dp = DataProvider(freqaiconf, exchange)
|
||||
strategy.freqai_info = freqaiconf.get("freqai", {})
|
||||
freqai = strategy.model.bridge
|
||||
freqai.live = True
|
||||
freqai.dk = FreqaiDataKitchen(freqaiconf, freqai.dd)
|
||||
timerange = TimeRange.parse_timerange("20180110-20180114")
|
||||
freqai.dk.load_all_pair_histories(timerange)
|
||||
sub_timerange = TimeRange.parse_timerange("20180111-20180114")
|
||||
corr_df, base_df = freqai.dk.get_base_and_corr_dataframes(sub_timerange, "LTC/BTC")
|
||||
|
||||
df = freqai.dk.use_strategy_to_populate_indicators(strategy, corr_df, base_df, 'LTC/BTC')
|
||||
|
||||
assert len(df.columns) == 45
|
||||
shutil.rmtree(Path(freqai.dk.full_path))
|
181
tests/freqai/test_freqai_interface.py
Normal file
181
tests/freqai/test_freqai_interface.py
Normal file
@@ -0,0 +1,181 @@
|
||||
# from unittest.mock import MagicMock
|
||||
# from freqtrade.commands.optimize_commands import setup_optimize_configuration, start_edge
|
||||
import copy
|
||||
# import platform
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from freqtrade.configuration import TimeRange
|
||||
from freqtrade.data.dataprovider import DataProvider
|
||||
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
|
||||
from tests.conftest import get_patched_exchange, log_has_re
|
||||
from tests.freqai.conftest import freqai_conf, get_patched_freqai_strategy
|
||||
|
||||
|
||||
def test_train_model_in_series_LightGBM(mocker, default_conf):
|
||||
freqaiconf = freqai_conf(copy.deepcopy(default_conf))
|
||||
freqaiconf.update({"timerange": "20180110-20180130"})
|
||||
|
||||
strategy = get_patched_freqai_strategy(mocker, freqaiconf)
|
||||
exchange = get_patched_exchange(mocker, freqaiconf)
|
||||
strategy.dp = DataProvider(freqaiconf, exchange)
|
||||
strategy.freqai_info = freqaiconf.get("freqai", {})
|
||||
freqai = strategy.model.bridge
|
||||
freqai.live = True
|
||||
freqai.dk = FreqaiDataKitchen(freqaiconf, freqai.dd)
|
||||
timerange = TimeRange.parse_timerange("20180110-20180130")
|
||||
freqai.dk.load_all_pair_histories(timerange)
|
||||
|
||||
freqai.dd.pair_dict = MagicMock()
|
||||
|
||||
data_load_timerange = TimeRange.parse_timerange("20180110-20180130")
|
||||
new_timerange = TimeRange.parse_timerange("20180120-20180130")
|
||||
|
||||
freqai.train_model_in_series(new_timerange, "ADA/BTC", strategy, freqai.dk, data_load_timerange)
|
||||
|
||||
assert (
|
||||
Path(freqai.dk.data_path / str(freqai.dk.model_filename + "_model.joblib"))
|
||||
.resolve()
|
||||
.exists()
|
||||
)
|
||||
assert (
|
||||
Path(freqai.dk.data_path / str(freqai.dk.model_filename + "_metadata.json"))
|
||||
.resolve()
|
||||
.exists()
|
||||
)
|
||||
assert (
|
||||
Path(freqai.dk.data_path / str(freqai.dk.model_filename + "_trained_df.pkl"))
|
||||
.resolve()
|
||||
.exists()
|
||||
)
|
||||
assert (
|
||||
Path(freqai.dk.data_path / str(freqai.dk.model_filename + "_svm_model.joblib"))
|
||||
.resolve()
|
||||
.exists()
|
||||
)
|
||||
|
||||
shutil.rmtree(Path(freqai.dk.full_path))
|
||||
|
||||
|
||||
# FIXME: hits segfault
|
||||
# @pytest.mark.skipif("arm" in platform.uname()[-1], reason="no ARM..")
|
||||
# def test_train_model_in_series_Catboost(mocker, default_conf):
|
||||
# freqaiconf = freqai_conf(copy.deepcopy(default_conf))
|
||||
# freqaiconf.update({"timerange": "20180110-20180130"})
|
||||
# freqaiconf.update({"freqaimodel": "CatboostPredictionModel"})
|
||||
# strategy = get_patched_freqai_strategy(mocker, freqaiconf)
|
||||
# exchange = get_patched_exchange(mocker, freqaiconf)
|
||||
# strategy.dp = DataProvider(freqaiconf, exchange)
|
||||
# strategy.freqai_info = freqaiconf.get("freqai", {})
|
||||
# freqai = strategy.model.bridge
|
||||
# freqai.live = True
|
||||
# freqai.dk = FreqaiDataKitchen(freqaiconf, freqai.dd)
|
||||
# timerange = TimeRange.parse_timerange("20180110-20180130")
|
||||
# freqai.dk.load_all_pair_histories(timerange)
|
||||
|
||||
# freqai.dd.pair_dict = MagicMock()
|
||||
|
||||
# data_load_timerange = TimeRange.parse_timerange("20180110-20180130")
|
||||
# new_timerange = TimeRange.parse_timerange("20180120-20180130")
|
||||
|
||||
# freqai.train_model_in_series(new_timerange, "ADA/BTC",
|
||||
# strategy, freqai.dk, data_load_timerange)
|
||||
|
||||
# assert (
|
||||
# Path(freqai.dk.data_path / str(freqai.dk.model_filename + "_model.joblib"))
|
||||
# .resolve()
|
||||
# .exists()
|
||||
# )
|
||||
# assert (
|
||||
# Path(freqai.dk.data_path / str(freqai.dk.model_filename + "_metadata.json"))
|
||||
# .resolve()
|
||||
# .exists()
|
||||
# )
|
||||
# assert (
|
||||
# Path(freqai.dk.data_path / str(freqai.dk.model_filename + "_trained_df.pkl"))
|
||||
# .resolve()
|
||||
# .exists()
|
||||
# )
|
||||
# assert (
|
||||
# Path(freqai.dk.data_path / str(freqai.dk.model_filename + "_svm_model.joblib"))
|
||||
# .resolve()
|
||||
# .exists()
|
||||
# )
|
||||
|
||||
# shutil.rmtree(Path(freqai.dk.full_path))
|
||||
|
||||
|
||||
def test_start_backtesting(mocker, default_conf):
|
||||
freqaiconf = freqai_conf(copy.deepcopy(default_conf))
|
||||
freqaiconf.update({"timerange": "20180120-20180130"})
|
||||
strategy = get_patched_freqai_strategy(mocker, freqaiconf)
|
||||
exchange = get_patched_exchange(mocker, freqaiconf)
|
||||
strategy.dp = DataProvider(freqaiconf, exchange)
|
||||
strategy.freqai_info = freqaiconf.get("freqai", {})
|
||||
freqai = strategy.model.bridge
|
||||
freqai.live = False
|
||||
freqai.dk = FreqaiDataKitchen(freqaiconf, freqai.dd)
|
||||
timerange = TimeRange.parse_timerange("20180110-20180130")
|
||||
freqai.dk.load_all_pair_histories(timerange)
|
||||
sub_timerange = TimeRange.parse_timerange("20180110-20180130")
|
||||
corr_df, base_df = freqai.dk.get_base_and_corr_dataframes(sub_timerange, "LTC/BTC")
|
||||
|
||||
df = freqai.dk.use_strategy_to_populate_indicators(strategy, corr_df, base_df, "LTC/BTC")
|
||||
|
||||
metadata = {"pair": "ADA/BTC"}
|
||||
freqai.start_backtesting(df, metadata, freqai.dk)
|
||||
model_folders = [x for x in freqai.dd.full_path.iterdir() if x.is_dir()]
|
||||
|
||||
assert len(model_folders) == 5
|
||||
|
||||
shutil.rmtree(Path(freqai.dk.full_path))
|
||||
|
||||
|
||||
def test_start_backtesting_from_existing_folder(mocker, default_conf, caplog):
|
||||
freqaiconf = freqai_conf(copy.deepcopy(default_conf))
|
||||
freqaiconf.update({"timerange": "20180120-20180130"})
|
||||
strategy = get_patched_freqai_strategy(mocker, freqaiconf)
|
||||
exchange = get_patched_exchange(mocker, freqaiconf)
|
||||
strategy.dp = DataProvider(freqaiconf, exchange)
|
||||
strategy.freqai_info = freqaiconf.get("freqai", {})
|
||||
freqai = strategy.model.bridge
|
||||
freqai.live = False
|
||||
freqai.dk = FreqaiDataKitchen(freqaiconf, freqai.dd)
|
||||
timerange = TimeRange.parse_timerange("20180110-20180130")
|
||||
freqai.dk.load_all_pair_histories(timerange)
|
||||
sub_timerange = TimeRange.parse_timerange("20180110-20180130")
|
||||
corr_df, base_df = freqai.dk.get_base_and_corr_dataframes(sub_timerange, "LTC/BTC")
|
||||
|
||||
df = freqai.dk.use_strategy_to_populate_indicators(strategy, corr_df, base_df, "LTC/BTC")
|
||||
|
||||
metadata = {"pair": "ADA/BTC"}
|
||||
freqai.start_backtesting(df, metadata, freqai.dk)
|
||||
model_folders = [x for x in freqai.dd.full_path.iterdir() if x.is_dir()]
|
||||
|
||||
assert len(model_folders) == 5
|
||||
|
||||
# without deleting the exiting folder structure, re-run
|
||||
|
||||
freqaiconf.update({"timerange": "20180120-20180130"})
|
||||
strategy = get_patched_freqai_strategy(mocker, freqaiconf)
|
||||
exchange = get_patched_exchange(mocker, freqaiconf)
|
||||
strategy.dp = DataProvider(freqaiconf, exchange)
|
||||
strategy.freqai_info = freqaiconf.get("freqai", {})
|
||||
freqai = strategy.model.bridge
|
||||
freqai.live = False
|
||||
freqai.dk = FreqaiDataKitchen(freqaiconf, freqai.dd)
|
||||
timerange = TimeRange.parse_timerange("20180110-20180130")
|
||||
freqai.dk.load_all_pair_histories(timerange)
|
||||
sub_timerange = TimeRange.parse_timerange("20180110-20180130")
|
||||
corr_df, base_df = freqai.dk.get_base_and_corr_dataframes(sub_timerange, "LTC/BTC")
|
||||
|
||||
df = freqai.dk.use_strategy_to_populate_indicators(strategy, corr_df, base_df, "LTC/BTC")
|
||||
freqai.start_backtesting(df, metadata, freqai.dk)
|
||||
|
||||
assert log_has_re(
|
||||
"Found model at ",
|
||||
caplog,
|
||||
)
|
||||
|
||||
shutil.rmtree(Path(freqai.dk.full_path))
|
@@ -1403,7 +1403,8 @@ def test_api_strategies(botclient):
|
||||
'StrategyTestV2',
|
||||
'StrategyTestV3',
|
||||
'StrategyTestV3Analysis',
|
||||
'StrategyTestV3Futures'
|
||||
'StrategyTestV3Futures',
|
||||
'freqai_test_strat'
|
||||
]}
|
||||
|
||||
|
||||
|
182
tests/strategy/strats/freqai_test_strat.py
Normal file
182
tests/strategy/strats/freqai_test_strat.py
Normal file
@@ -0,0 +1,182 @@
|
||||
import logging
|
||||
from functools import reduce
|
||||
|
||||
import pandas as pd
|
||||
import talib.abstract as ta
|
||||
from pandas import DataFrame
|
||||
|
||||
from freqtrade.freqai.strategy_bridge import CustomModel
|
||||
from freqtrade.strategy import DecimalParameter, IntParameter, merge_informative_pair
|
||||
from freqtrade.strategy.interface import IStrategy
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class freqai_test_strat(IStrategy):
|
||||
"""
|
||||
Example strategy showing how the user connects their own
|
||||
IFreqaiModel to the strategy. Namely, the user uses:
|
||||
self.model = CustomModel(self.config)
|
||||
self.model.bridge.start(dataframe, metadata)
|
||||
|
||||
to make predictions on their data. populate_any_indicators() automatically
|
||||
generates the variety of features indicated by the user in the
|
||||
canonical freqtrade configuration file under config['freqai'].
|
||||
"""
|
||||
|
||||
minimal_roi = {"0": 0.1, "240": -1}
|
||||
|
||||
plot_config = {
|
||||
"main_plot": {},
|
||||
"subplots": {
|
||||
"prediction": {"prediction": {"color": "blue"}},
|
||||
"target_roi": {
|
||||
"target_roi": {"color": "brown"},
|
||||
},
|
||||
"do_predict": {
|
||||
"do_predict": {"color": "brown"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
process_only_new_candles = True
|
||||
stoploss = -0.05
|
||||
use_exit_signal = True
|
||||
startup_candle_count: int = 300
|
||||
can_short = False
|
||||
|
||||
linear_roi_offset = DecimalParameter(
|
||||
0.00, 0.02, default=0.005, space="sell", optimize=False, load=True
|
||||
)
|
||||
max_roi_time_long = IntParameter(0, 800, default=400, space="sell", optimize=False, load=True)
|
||||
|
||||
def informative_pairs(self):
|
||||
whitelist_pairs = self.dp.current_whitelist()
|
||||
corr_pairs = self.config["freqai"]["feature_parameters"]["include_corr_pairlist"]
|
||||
informative_pairs = []
|
||||
for tf in self.config["freqai"]["feature_parameters"]["include_timeframes"]:
|
||||
for pair in whitelist_pairs:
|
||||
informative_pairs.append((pair, tf))
|
||||
for pair in corr_pairs:
|
||||
if pair in whitelist_pairs:
|
||||
continue # avoid duplication
|
||||
informative_pairs.append((pair, tf))
|
||||
return informative_pairs
|
||||
|
||||
def bot_start(self):
|
||||
self.model = CustomModel(self.config)
|
||||
|
||||
def populate_any_indicators(
|
||||
self, metadata, pair, df, tf, informative=None, coin="", set_generalized_indicators=False
|
||||
):
|
||||
"""
|
||||
Function designed to automatically generate, name and merge features
|
||||
from user indicated timeframes in the configuration file. User controls the indicators
|
||||
passed to the training/prediction by prepending indicators with `'%-' + coin `
|
||||
(see convention below). I.e. user should not prepend any supporting metrics
|
||||
(e.g. bb_lowerband below) with % unless they explicitly want to pass that metric to the
|
||||
model.
|
||||
:params:
|
||||
:pair: pair to be used as informative
|
||||
:df: strategy dataframe which will receive merges from informatives
|
||||
:tf: timeframe of the dataframe which will modify the feature names
|
||||
:informative: the dataframe associated with the informative pair
|
||||
:coin: the name of the coin which will modify the feature names.
|
||||
"""
|
||||
|
||||
with self.model.bridge.lock:
|
||||
if informative is None:
|
||||
informative = self.dp.get_pair_dataframe(pair, tf)
|
||||
|
||||
# first loop is automatically duplicating indicators for time periods
|
||||
for t in self.freqai_info["feature_parameters"]["indicator_periods_candles"]:
|
||||
|
||||
t = int(t)
|
||||
informative[f"%-{coin}rsi-period_{t}"] = ta.RSI(informative, timeperiod=t)
|
||||
informative[f"%-{coin}mfi-period_{t}"] = ta.MFI(informative, timeperiod=t)
|
||||
informative[f"%-{coin}adx-period_{t}"] = ta.ADX(informative, window=t)
|
||||
|
||||
informative[f"%-{coin}pct-change"] = informative["close"].pct_change()
|
||||
informative[f"%-{coin}raw_volume"] = informative["volume"]
|
||||
informative[f"%-{coin}raw_price"] = informative["close"]
|
||||
|
||||
indicators = [col for col in informative if col.startswith("%")]
|
||||
# This loop duplicates and shifts all indicators to add a sense of recency to data
|
||||
for n in range(self.freqai_info["feature_parameters"]["include_shifted_candles"] + 1):
|
||||
if n == 0:
|
||||
continue
|
||||
informative_shift = informative[indicators].shift(n)
|
||||
informative_shift = informative_shift.add_suffix("_shift-" + str(n))
|
||||
informative = pd.concat((informative, informative_shift), axis=1)
|
||||
|
||||
df = merge_informative_pair(df, informative, self.config["timeframe"], tf, ffill=True)
|
||||
skip_columns = [
|
||||
(s + "_" + tf) for s in ["date", "open", "high", "low", "close", "volume"]
|
||||
]
|
||||
df = df.drop(columns=skip_columns)
|
||||
|
||||
# Add generalized indicators here (because in live, it will call this
|
||||
# function to populate indicators during training). Notice how we ensure not to
|
||||
# add them multiple times
|
||||
if set_generalized_indicators:
|
||||
df["%-day_of_week"] = (df["date"].dt.dayofweek + 1) / 7
|
||||
df["%-hour_of_day"] = (df["date"].dt.hour + 1) / 25
|
||||
|
||||
# user adds targets here by prepending them with &- (see convention below)
|
||||
# If user wishes to use multiple targets, a multioutput prediction model
|
||||
# needs to be used such as templates/CatboostPredictionMultiModel.py
|
||||
df["&-s_close"] = (
|
||||
df["close"]
|
||||
.shift(-self.freqai_info["feature_parameters"]["label_period_candles"])
|
||||
.rolling(self.freqai_info["feature_parameters"]["label_period_candles"])
|
||||
.mean()
|
||||
/ df["close"]
|
||||
- 1
|
||||
)
|
||||
|
||||
return df
|
||||
|
||||
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||||
|
||||
self.freqai_info = self.config["freqai"]
|
||||
|
||||
# All indicators must be populated by populate_any_indicators() for live functionality
|
||||
# to work correctly.
|
||||
# the model will return 4 values, its prediction, an indication of whether or not the
|
||||
# prediction should be accepted, the target mean/std values from the labels used during
|
||||
# each training period.
|
||||
dataframe = self.model.bridge.start(dataframe, metadata, self)
|
||||
|
||||
dataframe["target_roi"] = dataframe["&-s_close_mean"] + dataframe["&-s_close_std"] * 1.25
|
||||
dataframe["sell_roi"] = dataframe["&-s_close_mean"] - dataframe["&-s_close_std"] * 1.25
|
||||
return dataframe
|
||||
|
||||
def populate_entry_trend(self, df: DataFrame, metadata: dict) -> DataFrame:
|
||||
|
||||
enter_long_conditions = [df["do_predict"] == 1, df["&-s_close"] > df["target_roi"]]
|
||||
|
||||
if enter_long_conditions:
|
||||
df.loc[
|
||||
reduce(lambda x, y: x & y, enter_long_conditions), ["enter_long", "enter_tag"]
|
||||
] = (1, "long")
|
||||
|
||||
enter_short_conditions = [df["do_predict"] == 1, df["&-s_close"] < df["sell_roi"]]
|
||||
|
||||
if enter_short_conditions:
|
||||
df.loc[
|
||||
reduce(lambda x, y: x & y, enter_short_conditions), ["enter_short", "enter_tag"]
|
||||
] = (1, "short")
|
||||
|
||||
return df
|
||||
|
||||
def populate_exit_trend(self, df: DataFrame, metadata: dict) -> DataFrame:
|
||||
exit_long_conditions = [df["do_predict"] == 1, df["&-s_close"] < df["sell_roi"] * 0.25]
|
||||
if exit_long_conditions:
|
||||
df.loc[reduce(lambda x, y: x & y, exit_long_conditions), "exit_long"] = 1
|
||||
|
||||
exit_short_conditions = [df["do_predict"] == 1, df["&-s_close"] > df["target_roi"] * 0.25]
|
||||
if exit_short_conditions:
|
||||
df.loc[reduce(lambda x, y: x & y, exit_short_conditions), "exit_short"] = 1
|
||||
|
||||
return df
|
@@ -34,7 +34,7 @@ def test_search_all_strategies_no_failed():
|
||||
directory = Path(__file__).parent / "strats"
|
||||
strategies = StrategyResolver.search_all_objects(directory, enum_failed=False)
|
||||
assert isinstance(strategies, list)
|
||||
assert len(strategies) == 7
|
||||
assert len(strategies) == 8
|
||||
assert isinstance(strategies[0], dict)
|
||||
|
||||
|
||||
@@ -42,10 +42,10 @@ def test_search_all_strategies_with_failed():
|
||||
directory = Path(__file__).parent / "strats"
|
||||
strategies = StrategyResolver.search_all_objects(directory, enum_failed=True)
|
||||
assert isinstance(strategies, list)
|
||||
assert len(strategies) == 8
|
||||
assert len(strategies) == 9
|
||||
# with enum_failed=True search_all_objects() shall find 2 good strategies
|
||||
# and 1 which fails to load
|
||||
assert len([x for x in strategies if x['class'] is not None]) == 7
|
||||
assert len([x for x in strategies if x['class'] is not None]) == 8
|
||||
assert len([x for x in strategies if x['class'] is None]) == 1
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user