Small refactor for new methods

This commit is contained in:
Matthias 2023-03-19 14:56:41 +01:00
parent 3adb3d9b1e
commit af6fc886f6
4 changed files with 20 additions and 23 deletions

View File

@ -29,8 +29,7 @@ from freqtrade.mixins import LoggingMixin
from freqtrade.optimize.backtest_caching import get_strategy_run_id from freqtrade.optimize.backtest_caching import get_strategy_run_id
from freqtrade.optimize.bt_progress import BTProgress from freqtrade.optimize.bt_progress import BTProgress
from freqtrade.optimize.optimize_reports import (generate_backtest_stats, show_backtest_results, from freqtrade.optimize.optimize_reports import (generate_backtest_stats, show_backtest_results,
store_backtest_rejected_signals, store_backtest_analysis_results,
store_backtest_signal_candles,
store_backtest_stats) store_backtest_stats)
from freqtrade.persistence import LocalTrade, Order, PairLocks, Trade from freqtrade.persistence import LocalTrade, Order, PairLocks, Trade
from freqtrade.plugins.pairlistmanager import PairListManager from freqtrade.plugins.pairlistmanager import PairListManager
@ -1389,11 +1388,9 @@ class Backtesting:
if (self.config.get('export', 'none') == 'signals' and if (self.config.get('export', 'none') == 'signals' and
self.dataprovider.runmode == RunMode.BACKTEST): self.dataprovider.runmode == RunMode.BACKTEST):
store_backtest_signal_candles( store_backtest_analysis_results(
self.config['exportfilename'], self.processed_dfs, dt_appendix) self.config['exportfilename'], self.processed_dfs, self.rejected_df,
dt_appendix)
store_backtest_rejected_signals(
self.config['exportfilename'], self.rejected_df, dt_appendix)
# Results may be mixed up now. Sort them so they follow --strategy-list order. # Results may be mixed up now. Sort them so they follow --strategy-list order.
if 'strategy_list' in self.config and len(self.results) > 0: if 'strategy_list' in self.config and len(self.results) > 0:

View File

@ -70,14 +70,11 @@ def _store_backtest_analysis_data(
return filename return filename
def store_backtest_signal_candles( def store_backtest_analysis_results(
recordfilename: Path, candles: Dict[str, Dict], dtappendix: str) -> Path: recordfilename: Path, candles: Dict[str, Dict], trades: Dict[str, Dict],
return _store_backtest_analysis_data(Path(recordfilename), candles, dtappendix, "signals") dtappendix: str) -> Path:
_store_backtest_analysis_data(Path(recordfilename), candles, dtappendix, "signals")
_store_backtest_analysis_data(Path(recordfilename), trades, dtappendix, "rejected")
def store_backtest_rejected_signals(
recordfilename: Path, trades: Dict[str, Dict], dtappendix: str) -> Path:
return _store_backtest_analysis_data(Path(recordfilename), trades, dtappendix, "rejected")
def _get_line_floatfmt(stake_currency: str) -> List[str]: def _get_line_floatfmt(stake_currency: str) -> List[str]:

View File

@ -355,7 +355,7 @@ def test_backtesting_start(default_conf, mocker, testdatadir, caplog) -> None:
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_stats')
sbc = mocker.patch('freqtrade.optimize.backtesting.store_backtest_signal_candles') sbc = mocker.patch('freqtrade.optimize.backtesting.store_backtest_analysis_results')
mocker.patch('freqtrade.plugins.pairlistmanager.PairListManager.whitelist', mocker.patch('freqtrade.plugins.pairlistmanager.PairListManager.whitelist',
PropertyMock(return_value=['UNITTEST/BTC'])) PropertyMock(return_value=['UNITTEST/BTC']))

View File

@ -21,7 +21,7 @@ from freqtrade.optimize.optimize_reports import (_get_resample_from_period, gene
generate_periodic_breakdown_stats, generate_periodic_breakdown_stats,
generate_strategy_comparison, generate_strategy_comparison,
generate_trading_stats, show_sorted_pairlist, generate_trading_stats, show_sorted_pairlist,
store_backtest_signal_candles, store_backtest_analysis_results,
store_backtest_stats, text_table_bt_results, store_backtest_stats, text_table_bt_results,
text_table_exit_reason, text_table_strategy) text_table_exit_reason, text_table_strategy)
from freqtrade.resolvers.strategy_resolver import StrategyResolver from freqtrade.resolvers.strategy_resolver import StrategyResolver
@ -232,17 +232,17 @@ def test_store_backtest_candles(testdatadir, mocker):
candle_dict = {'DefStrat': {'UNITTEST/BTC': pd.DataFrame()}} candle_dict = {'DefStrat': {'UNITTEST/BTC': pd.DataFrame()}}
# mock directory exporting # mock directory exporting
store_backtest_signal_candles(testdatadir, candle_dict, '2022_01_01_15_05_13') store_backtest_analysis_results(testdatadir, candle_dict, {}, '2022_01_01_15_05_13')
assert dump_mock.call_count == 1 assert dump_mock.call_count == 2
assert isinstance(dump_mock.call_args_list[0][0][0], Path) assert isinstance(dump_mock.call_args_list[0][0][0], Path)
assert str(dump_mock.call_args_list[0][0][0]).endswith(str('_signals.pkl')) assert str(dump_mock.call_args_list[0][0][0]).endswith(str('_signals.pkl'))
dump_mock.reset_mock() dump_mock.reset_mock()
# mock file exporting # mock file exporting
filename = Path(testdatadir / 'testresult') filename = Path(testdatadir / 'testresult')
store_backtest_signal_candles(filename, candle_dict, '2022_01_01_15_05_13') store_backtest_analysis_results(filename, candle_dict, {}, '2022_01_01_15_05_13')
assert dump_mock.call_count == 1 assert dump_mock.call_count == 2
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
assert str(dump_mock.call_args_list[0][0][0]).endswith(str('_signals.pkl')) assert str(dump_mock.call_args_list[0][0][0]).endswith(str('_signals.pkl'))
@ -254,7 +254,9 @@ def test_write_read_backtest_candles(tmpdir):
candle_dict = {'DefStrat': {'UNITTEST/BTC': pd.DataFrame()}} candle_dict = {'DefStrat': {'UNITTEST/BTC': pd.DataFrame()}}
# test directory exporting # test directory exporting
stored_file = store_backtest_signal_candles(Path(tmpdir), candle_dict, '2022_01_01_15_05_13') sample_date = '2022_01_01_15_05_13'
store_backtest_analysis_results(Path(tmpdir), candle_dict, {}, sample_date)
stored_file = Path(tmpdir / f'backtest-result-{sample_date}_signals.pkl')
scp = open(stored_file, "rb") scp = open(stored_file, "rb")
pickled_signal_candles = joblib.load(scp) pickled_signal_candles = joblib.load(scp)
scp.close() scp.close()
@ -268,7 +270,8 @@ def test_write_read_backtest_candles(tmpdir):
# test file exporting # test file exporting
filename = Path(tmpdir / 'testresult') filename = Path(tmpdir / 'testresult')
stored_file = store_backtest_signal_candles(filename, candle_dict, '2022_01_01_15_05_13') store_backtest_analysis_results(filename, candle_dict, {}, sample_date)
stored_file = Path(tmpdir / f'testresult-{sample_date}_signals.pkl')
scp = open(stored_file, "rb") scp = open(stored_file, "rb")
pickled_signal_candles = joblib.load(scp) pickled_signal_candles = joblib.load(scp)
scp.close() scp.close()