test: Update tests for new bt storage method
This commit is contained in:
@@ -371,8 +371,7 @@ def test_backtesting_start(default_conf, mocker, caplog) -> None:
|
|||||||
mocker.patch("freqtrade.optimize.backtesting.Backtesting.backtest")
|
mocker.patch("freqtrade.optimize.backtesting.Backtesting.backtest")
|
||||||
mocker.patch("freqtrade.optimize.backtesting.generate_backtest_stats")
|
mocker.patch("freqtrade.optimize.backtesting.generate_backtest_stats")
|
||||||
mocker.patch("freqtrade.optimize.backtesting.show_backtest_results")
|
mocker.patch("freqtrade.optimize.backtesting.show_backtest_results")
|
||||||
sbs = mocker.patch("freqtrade.optimize.backtesting.store_backtest_stats")
|
sbs = mocker.patch("freqtrade.optimize.backtesting.store_backtest_results")
|
||||||
sbc = mocker.patch("freqtrade.optimize.backtesting.store_backtest_analysis_results")
|
|
||||||
mocker.patch(
|
mocker.patch(
|
||||||
"freqtrade.plugins.pairlistmanager.PairListManager.whitelist",
|
"freqtrade.plugins.pairlistmanager.PairListManager.whitelist",
|
||||||
PropertyMock(return_value=["UNITTEST/BTC"]),
|
PropertyMock(return_value=["UNITTEST/BTC"]),
|
||||||
@@ -397,7 +396,6 @@ def test_backtesting_start(default_conf, mocker, caplog) -> None:
|
|||||||
assert backtesting.strategy.bot_start.call_count == 1
|
assert backtesting.strategy.bot_start.call_count == 1
|
||||||
assert backtesting.strategy.bot_loop_start.call_count == 0
|
assert backtesting.strategy.bot_loop_start.call_count == 0
|
||||||
assert sbs.call_count == 1
|
assert sbs.call_count == 1
|
||||||
assert sbc.call_count == 1
|
|
||||||
|
|
||||||
|
|
||||||
def test_backtesting_start_no_data(default_conf, mocker, caplog, testdatadir) -> None:
|
def test_backtesting_start_no_data(default_conf, mocker, caplog, testdatadir) -> None:
|
||||||
|
|||||||
@@ -26,8 +26,7 @@ from freqtrade.optimize.optimize_reports import (
|
|||||||
generate_strategy_comparison,
|
generate_strategy_comparison,
|
||||||
generate_trading_stats,
|
generate_trading_stats,
|
||||||
show_sorted_pairlist,
|
show_sorted_pairlist,
|
||||||
store_backtest_analysis_results,
|
store_backtest_results,
|
||||||
store_backtest_stats,
|
|
||||||
text_table_bt_results,
|
text_table_bt_results,
|
||||||
text_table_strategy,
|
text_table_strategy,
|
||||||
)
|
)
|
||||||
@@ -226,8 +225,9 @@ def test_generate_backtest_stats(default_conf, testdatadir, tmp_path):
|
|||||||
filename_last = tmp_path / LAST_BT_RESULT_FN
|
filename_last = tmp_path / LAST_BT_RESULT_FN
|
||||||
_backup_file(filename_last, copy_file=True)
|
_backup_file(filename_last, copy_file=True)
|
||||||
assert not filename.is_file()
|
assert not filename.is_file()
|
||||||
|
default_conf["exportfilename"] = filename
|
||||||
|
|
||||||
store_backtest_stats(filename, stats, "2022_01_01_15_05_13")
|
store_backtest_results(default_conf, stats, "2022_01_01_15_05_13")
|
||||||
|
|
||||||
# get real Filename (it's btresult-<date>.json)
|
# get real Filename (it's btresult-<date>.json)
|
||||||
last_fn = get_latest_backtest_filename(filename_last.parent)
|
last_fn = get_latest_backtest_filename(filename_last.parent)
|
||||||
@@ -246,11 +246,12 @@ def test_generate_backtest_stats(default_conf, testdatadir, tmp_path):
|
|||||||
filename1.unlink()
|
filename1.unlink()
|
||||||
|
|
||||||
|
|
||||||
def test_store_backtest_stats(testdatadir, mocker):
|
def test_store_backtest_results(testdatadir, mocker):
|
||||||
dump_mock = mocker.patch("freqtrade.optimize.optimize_reports.bt_storage.file_dump_json")
|
dump_mock = mocker.patch("freqtrade.optimize.optimize_reports.bt_storage.file_dump_json")
|
||||||
|
|
||||||
data = {"metadata": {}, "strategy": {}, "strategy_comparison": []}
|
data = {"metadata": {}, "strategy": {}, "strategy_comparison": []}
|
||||||
store_backtest_stats(testdatadir, data, "2022_01_01_15_05_13")
|
|
||||||
|
store_backtest_results({"exportfilename": testdatadir}, data, "2022_01_01_15_05_13")
|
||||||
|
|
||||||
assert dump_mock.call_count == 3
|
assert dump_mock.call_count == 3
|
||||||
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
|
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
|
||||||
@@ -258,16 +259,16 @@ def test_store_backtest_stats(testdatadir, mocker):
|
|||||||
|
|
||||||
dump_mock.reset_mock()
|
dump_mock.reset_mock()
|
||||||
filename = testdatadir / "testresult.json"
|
filename = testdatadir / "testresult.json"
|
||||||
store_backtest_stats(filename, data, "2022_01_01_15_05_13")
|
store_backtest_results({"exportfilename": filename}, data, "2022_01_01_15_05_13")
|
||||||
assert dump_mock.call_count == 3
|
assert dump_mock.call_count == 3
|
||||||
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
|
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
|
||||||
# result will be testdatadir / testresult-<timestamp>.json
|
# result will be testdatadir / testresult-<timestamp>.json
|
||||||
assert str(dump_mock.call_args_list[0][0][0]).startswith(str(testdatadir / "testresult"))
|
assert str(dump_mock.call_args_list[0][0][0]).startswith(str(testdatadir / "testresult"))
|
||||||
|
|
||||||
|
|
||||||
def test_store_backtest_stats_real(tmp_path):
|
def test_store_backtest_results_real(tmp_path):
|
||||||
data = {"metadata": {}, "strategy": {}, "strategy_comparison": []}
|
data = {"metadata": {}, "strategy": {}, "strategy_comparison": []}
|
||||||
store_backtest_stats(tmp_path, data, "2022_01_01_15_05_13")
|
store_backtest_results({"exportfilename": tmp_path}, data, "2022_01_01_15_05_13")
|
||||||
|
|
||||||
assert (tmp_path / "backtest-result-2022_01_01_15_05_13.json").is_file()
|
assert (tmp_path / "backtest-result-2022_01_01_15_05_13.json").is_file()
|
||||||
assert (tmp_path / "backtest-result-2022_01_01_15_05_13.meta.json").is_file()
|
assert (tmp_path / "backtest-result-2022_01_01_15_05_13.meta.json").is_file()
|
||||||
@@ -276,7 +277,9 @@ def test_store_backtest_stats_real(tmp_path):
|
|||||||
fn = get_latest_backtest_filename(tmp_path)
|
fn = get_latest_backtest_filename(tmp_path)
|
||||||
assert fn == "backtest-result-2022_01_01_15_05_13.json"
|
assert fn == "backtest-result-2022_01_01_15_05_13.json"
|
||||||
|
|
||||||
store_backtest_stats(tmp_path, data, "2024_01_01_15_05_25", market_change_data=pd.DataFrame())
|
store_backtest_results(
|
||||||
|
{"exportfilename": tmp_path}, data, "2024_01_01_15_05_25", market_change_data=pd.DataFrame()
|
||||||
|
)
|
||||||
assert (tmp_path / "backtest-result-2024_01_01_15_05_25.json").is_file()
|
assert (tmp_path / "backtest-result-2024_01_01_15_05_25.json").is_file()
|
||||||
assert (tmp_path / "backtest-result-2024_01_01_15_05_25.meta.json").is_file()
|
assert (tmp_path / "backtest-result-2024_01_01_15_05_25.meta.json").is_file()
|
||||||
assert (tmp_path / "backtest-result-2024_01_01_15_05_25_market_change.feather").is_file()
|
assert (tmp_path / "backtest-result-2024_01_01_15_05_25_market_change.feather").is_file()
|
||||||
@@ -291,6 +294,13 @@ def test_store_backtest_candles(testdatadir, mocker):
|
|||||||
dump_mock = mocker.patch("freqtrade.optimize.optimize_reports.bt_storage.file_dump_joblib")
|
dump_mock = mocker.patch("freqtrade.optimize.optimize_reports.bt_storage.file_dump_joblib")
|
||||||
|
|
||||||
candle_dict = {"DefStrat": {"UNITTEST/BTC": pd.DataFrame()}}
|
candle_dict = {"DefStrat": {"UNITTEST/BTC": pd.DataFrame()}}
|
||||||
|
bt_results = {"metadata": {}, "strategy": {}, "strategy_comparison": []}
|
||||||
|
|
||||||
|
mock_conf = {
|
||||||
|
"exportfilename": testdatadir,
|
||||||
|
"export": "signals",
|
||||||
|
"runmode": "backtest",
|
||||||
|
}
|
||||||
|
|
||||||
# mock directory exporting
|
# mock directory exporting
|
||||||
data = {
|
data = {
|
||||||
@@ -299,7 +309,7 @@ def test_store_backtest_candles(testdatadir, mocker):
|
|||||||
"exited": {},
|
"exited": {},
|
||||||
}
|
}
|
||||||
|
|
||||||
store_backtest_analysis_results(testdatadir, data, "2022_01_01_15_05_13")
|
store_backtest_results(mock_conf, bt_results, "2022_01_01_15_05_13", analysis_results=data)
|
||||||
|
|
||||||
assert dump_mock.call_count == 3
|
assert dump_mock.call_count == 3
|
||||||
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
|
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
|
||||||
@@ -310,7 +320,8 @@ def test_store_backtest_candles(testdatadir, mocker):
|
|||||||
dump_mock.reset_mock()
|
dump_mock.reset_mock()
|
||||||
# mock file exporting
|
# mock file exporting
|
||||||
filename = Path(testdatadir / "testresult")
|
filename = Path(testdatadir / "testresult")
|
||||||
store_backtest_analysis_results(filename, data, "2022_01_01_15_05_13")
|
mock_conf["exportfilename"] = filename
|
||||||
|
store_backtest_results(mock_conf, bt_results, "2022_01_01_15_05_13", analysis_results=data)
|
||||||
assert dump_mock.call_count == 3
|
assert dump_mock.call_count == 3
|
||||||
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
|
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
|
||||||
# result will be testdatadir / testresult-<timestamp>_signals.pkl
|
# result will be testdatadir / testresult-<timestamp>_signals.pkl
|
||||||
@@ -323,7 +334,13 @@ def test_store_backtest_candles(testdatadir, mocker):
|
|||||||
|
|
||||||
def test_write_read_backtest_candles(tmp_path):
|
def test_write_read_backtest_candles(tmp_path):
|
||||||
candle_dict = {"DefStrat": {"UNITTEST/BTC": pd.DataFrame()}}
|
candle_dict = {"DefStrat": {"UNITTEST/BTC": pd.DataFrame()}}
|
||||||
|
bt_results = {"metadata": {}, "strategy": {}, "strategy_comparison": []}
|
||||||
|
|
||||||
|
mock_conf = {
|
||||||
|
"exportfilename": tmp_path,
|
||||||
|
"export": "signals",
|
||||||
|
"runmode": "backtest",
|
||||||
|
}
|
||||||
# test directory exporting
|
# test directory exporting
|
||||||
sample_date = "2022_01_01_15_05_13"
|
sample_date = "2022_01_01_15_05_13"
|
||||||
data = {
|
data = {
|
||||||
@@ -331,7 +348,7 @@ def test_write_read_backtest_candles(tmp_path):
|
|||||||
"rejected": {},
|
"rejected": {},
|
||||||
"exited": {},
|
"exited": {},
|
||||||
}
|
}
|
||||||
store_backtest_analysis_results(tmp_path, data, sample_date)
|
store_backtest_results(mock_conf, bt_results, sample_date, analysis_results=data)
|
||||||
stored_file = tmp_path / f"backtest-result-{sample_date}_signals.pkl"
|
stored_file = tmp_path / f"backtest-result-{sample_date}_signals.pkl"
|
||||||
with stored_file.open("rb") as scp:
|
with stored_file.open("rb") as scp:
|
||||||
pickled_signal_candles = joblib.load(scp)
|
pickled_signal_candles = joblib.load(scp)
|
||||||
@@ -346,7 +363,8 @@ def test_write_read_backtest_candles(tmp_path):
|
|||||||
|
|
||||||
# test file exporting
|
# test file exporting
|
||||||
filename = tmp_path / "testresult"
|
filename = tmp_path / "testresult"
|
||||||
store_backtest_analysis_results(filename, data, sample_date)
|
mock_conf["exportfilename"] = filename
|
||||||
|
store_backtest_results(mock_conf, bt_results, sample_date, analysis_results=data)
|
||||||
stored_file = tmp_path / f"testresult-{sample_date}_signals.pkl"
|
stored_file = tmp_path / f"testresult-{sample_date}_signals.pkl"
|
||||||
with stored_file.open("rb") as scp:
|
with stored_file.open("rb") as scp:
|
||||||
pickled_signal_candles = joblib.load(scp)
|
pickled_signal_candles = joblib.load(scp)
|
||||||
|
|||||||
Reference in New Issue
Block a user