feat: add Strategy and parameter file to backtest zip file
This commit is contained in:
@@ -1792,6 +1792,7 @@ class Backtesting:
|
|||||||
dt_appendix,
|
dt_appendix,
|
||||||
market_change_data=combined_res,
|
market_change_data=combined_res,
|
||||||
analysis_results=self.analysis_results,
|
analysis_results=self.analysis_results,
|
||||||
|
strategy_files={s.get_strategy_name(): s.__file__ for s in self.strategylist},
|
||||||
)
|
)
|
||||||
|
|
||||||
# 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.
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ def store_backtest_results(
|
|||||||
*,
|
*,
|
||||||
market_change_data: DataFrame | None = None,
|
market_change_data: DataFrame | None = None,
|
||||||
analysis_results: dict[str, dict[str, DataFrame]] | None = None,
|
analysis_results: dict[str, dict[str, DataFrame]] | None = None,
|
||||||
|
strategy_files: dict[str, str] | None = None,
|
||||||
) -> Path:
|
) -> Path:
|
||||||
"""
|
"""
|
||||||
Stores backtest results and analysis data in a zip file, with metadata stored separately
|
Stores backtest results and analysis data in a zip file, with metadata stored separately
|
||||||
@@ -90,6 +91,25 @@ def store_backtest_results(
|
|||||||
dump_json_to_file(config_buf, sanitize_config(config["original_config"]))
|
dump_json_to_file(config_buf, sanitize_config(config["original_config"]))
|
||||||
zipf.writestr(f"{base_filename.stem}_config.json", config_buf.getvalue())
|
zipf.writestr(f"{base_filename.stem}_config.json", config_buf.getvalue())
|
||||||
|
|
||||||
|
for strategy_name, strategy_file in (strategy_files or {}).items():
|
||||||
|
# Store the strategy file and its parameters
|
||||||
|
strategy_buf = BytesIO()
|
||||||
|
strategy_path = Path(strategy_file)
|
||||||
|
with strategy_path.open("rb") as strategy_file_obj:
|
||||||
|
strategy_buf.write(strategy_file_obj.read())
|
||||||
|
strategy_buf.seek(0)
|
||||||
|
zipf.writestr(f"{base_filename.stem}_{strategy_name}.py", strategy_buf.getvalue())
|
||||||
|
strategy_params = strategy_path.with_suffix(".json")
|
||||||
|
if strategy_params.is_file():
|
||||||
|
strategy_params_buf = BytesIO()
|
||||||
|
with strategy_params.open("rb") as strategy_params_obj:
|
||||||
|
strategy_params_buf.write(strategy_params_obj.read())
|
||||||
|
strategy_params_buf.seek(0)
|
||||||
|
zipf.writestr(
|
||||||
|
f"{base_filename.stem}_{strategy_name}.json",
|
||||||
|
strategy_params_buf.getvalue(),
|
||||||
|
)
|
||||||
|
|
||||||
# Add market change data if present
|
# Add market change data if present
|
||||||
if market_change_data is not None:
|
if market_change_data is not None:
|
||||||
market_change_name = f"{base_filename.stem}_market_change.feather"
|
market_change_name = f"{base_filename.stem}_market_change.feather"
|
||||||
|
|||||||
Reference in New Issue
Block a user