Introduce --exit-signals flag to backtesting-analysis command

This commit is contained in:
jainanuj94
2024-08-02 20:09:56 +05:30
parent 8f8859a5f5
commit b0e863dbbb
5 changed files with 14 additions and 25 deletions

View File

@@ -222,6 +222,7 @@ ARGS_ANALYZE_ENTRIES_EXITS = [
"indicator_list",
"timerange",
"analysis_rejected",
"analysis_exited",
"analysis_to_csv",
"analysis_csv_path",
]

View File

@@ -720,6 +720,11 @@ AVAILABLE_CLI_OPTIONS = {
help="Analyse rejected signals",
action="store_true",
),
"analysis_exited": Arg(
"--exit-signals",
help="Analyse indicators at exit signals",
action="store_true",
),
"analysis_to_csv": Arg(
"--analysis-to-csv",
help="Save selected analysis tables to individual CSVs",

View File

@@ -401,6 +401,7 @@ class Configuration:
("indicator_list", "Analysis indicator list: {}"),
("timerange", "Filter trades by timerange: {}"),
("analysis_rejected", "Analyse rejected signals: {}"),
("analysis_exited", "Analyse exited signals: {}"),
("analysis_to_csv", "Store analysis tables to CSV: {}"),
("analysis_csv_path", "Path to store analysis CSVs: {}"),
# Lookahead analysis results

View File

@@ -319,6 +319,7 @@ def process_entry_exit_reasons(config: Config):
enter_reason_list = config.get("enter_reason_list", ["all"])
exit_reason_list = config.get("exit_reason_list", ["all"])
indicator_list = config.get("indicator_list", [])
do_exited = config.get("analysis_exited", False)
do_rejected = config.get("analysis_rejected", False)
to_csv = config.get("analysis_to_csv", False)
csv_path = Path(config.get("analysis_csv_path", config["exportfilename"]))
@@ -335,8 +336,10 @@ def process_entry_exit_reasons(config: Config):
trades = load_backtest_data(config["exportfilename"], strategy_name)
if trades is not None and not trades.empty:
if do_exited is True:
signal_candles = _load_exit_signal_candles(config["exportfilename"])
else:
signal_candles = _load_signal_candles(config["exportfilename"])
exit_signal_candles = _load_exit_signal_candles(config["exportfilename"])
rej_df = None
if do_rejected:
@@ -353,10 +356,6 @@ def process_entry_exit_reasons(config: Config):
config["exchange"]["pair_whitelist"], strategy_name, trades, signal_candles
)
exited_trades_dict = _process_candles_and_indicators(
config["exchange"]["pair_whitelist"], strategy_name, trades, exit_signal_candles
)
res_df = prepare_results(
analysed_trades_dict,
strategy_name,
@@ -365,23 +364,6 @@ def process_entry_exit_reasons(config: Config):
timerange=timerange,
)
exited_df = prepare_results(
exited_trades_dict,
strategy_name,
enter_reason_list,
exit_reason_list,
timerange=timerange,
)
print_results(
exited_df,
analysis_groups,
indicator_list,
to_csv=False,
rejected_signals=None,
csv_path=csv_path,
)
print_results(
res_df,
analysis_groups,

View File

@@ -295,7 +295,7 @@ def test_store_backtest_candles(testdatadir, mocker):
# mock directory exporting
store_backtest_analysis_results(testdatadir, candle_dict, {}, {}, "2022_01_01_15_05_13")
assert dump_mock.call_count == 2
assert dump_mock.call_count == 3
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
assert str(dump_mock.call_args_list[0][0][0]).endswith("_signals.pkl")
@@ -303,7 +303,7 @@ def test_store_backtest_candles(testdatadir, mocker):
# mock file exporting
filename = Path(testdatadir / "testresult")
store_backtest_analysis_results(filename, candle_dict, {}, {}, "2022_01_01_15_05_13")
assert dump_mock.call_count == 2
assert dump_mock.call_count == 3
assert isinstance(dump_mock.call_args_list[0][0][0], Path)
# result will be testdatadir / testresult-<timestamp>_signals.pkl
assert str(dump_mock.call_args_list[0][0][0]).endswith("_signals.pkl")