Skip to content

Commit 4be582e

Browse files
Merge pull request #349 from Marinovsky/bug-7350-IncludeParametersInReportAndStatistics
Include parameters in report and statistics
2 parents 4346e68 + 32942c3 commit 4be582e

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,7 @@ Options:
12551255
--backtest-results FILE Path to the JSON file containing the backtest results
12561256
--live-results FILE Path to the JSON file containing the live trading results
12571257
--report-destination FILE Path where the generated report is stored as HTML (defaults to ./report.html)
1258+
--css FILE Path where the CSS override file is stored
12581259
-d, --detach Run the report creator in a detached Docker container and return immediately
12591260
--strategy-name TEXT Name of the strategy, will appear at the top-right corner of each page
12601261
--strategy-version TEXT Version number of the strategy, will appear next to the project name

lean/commands/report.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ def _find_project_directory(backtest_file: Path) -> Optional[Path]:
5252
type=PathParameter(exists=False, file_okay=True, dir_okay=False),
5353
default=lambda: Path.cwd() / "report.html",
5454
help="Path where the generated report is stored as HTML (defaults to ./report.html)")
55+
@option("--css",
56+
type=PathParameter(exists=False, file_okay=True, dir_okay=False),
57+
help="Path where the CSS override file is stored")
5558
@option("--detach", "-d",
5659
is_flag=True,
5760
default=False,
@@ -83,6 +86,7 @@ def _find_project_directory(backtest_file: Path) -> Optional[Path]:
8386
def report(backtest_results: Optional[Path],
8487
live_results: Optional[Path],
8588
report_destination: Path,
89+
css: Optional[Path],
8690
detach: bool,
8791
strategy_name: Optional[str],
8892
strategy_version: Optional[str],
@@ -157,6 +161,7 @@ def report(backtest_results: Optional[Path],
157161
"live-data-source-file": "live-data-source-file.json" if live_results is not None else "",
158162
"backtest-data-source-file": "backtest-data-source-file.json",
159163
"report-destination": "/tmp/report.html",
164+
"report-css-override-file": "report_override.css" if (css is not None) and (css.exists()) else "",
160165
"report-format": "pdf" if pdf else "",
161166
"environment": "report",
162167

@@ -222,6 +227,15 @@ def report(backtest_results: Optional[Path],
222227
}
223228
}
224229

230+
if css is not None:
231+
if css.exists():
232+
run_options["mounts"].append(Mount(target="/Lean/Report/bin/Debug/report_override.css",
233+
source=str(css),
234+
type="bind",
235+
read_only=True))
236+
else:
237+
logger.info(f"CSS override file '{css}' could not be found")
238+
225239
if pdf:
226240
run_options["commands"].append(f'cp /tmp/report.pdf "/Output/{report_destination.name.replace(".html", ".pdf")}"')
227241
if live_results is not None:

tests/commands/test_report.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ def setup_backtest_results() -> None:
4242
with results_path.open("w+", encoding="utf-8") as file:
4343
file.write("{}")
4444

45+
css_path = Path.cwd() / f"{results_id}.css"
46+
css_path.parent.mkdir(parents=True, exist_ok=True)
47+
with css_path.open("w+", encoding="utf-8") as file:
48+
file.write("")
49+
4550
results_config_path = results_dir / "config"
4651
with results_config_path.open("w+", encoding="utf-8") as file:
4752
file.write(json.dumps({'id': results_id}))
@@ -206,6 +211,39 @@ def test_report_mounts_given_backtest_data_source_file() -> None:
206211
mount = [m for m in kwargs["mounts"] if m["Target"] == "/Lean/Report/bin/Debug/backtest-data-source-file.json"][0]
207212
assert mount["Source"] == str(Path.cwd() / "Python Project" / "backtests" / "2020-01-01_00-00-00" / "1459804915.json")
208213

214+
def test_report_mounts_given_css_override_file() -> None:
215+
docker_manager = mock.Mock()
216+
docker_manager.run_image.side_effect = run_image
217+
initialize_container(docker_manager_to_use=docker_manager)
218+
219+
result = CliRunner().invoke(lean, ["report",
220+
"--css",
221+
"1459804915.css"])
222+
223+
assert result.exit_code == 0
224+
225+
docker_manager.run_image.assert_called_once()
226+
args, kwargs = docker_manager.run_image.call_args
227+
228+
mount = [m for m in kwargs["mounts"] if m["Target"] == "/Lean/Report/bin/Debug/report_override.css"][0]
229+
assert mount["Source"] == str(Path.cwd() / "1459804915.css")
230+
231+
def test_report_runs_even_when_css_override_file_does_not_exist() -> None:
232+
docker_manager = mock.Mock()
233+
docker_manager.run_image.side_effect = run_image
234+
initialize_container(docker_manager_to_use=docker_manager)
235+
236+
result = CliRunner().invoke(lean, ["report",
237+
"--css",
238+
"1459804916.css"])
239+
240+
assert result.exit_code == 0
241+
242+
docker_manager.run_image.assert_called_once()
243+
args, kwargs = docker_manager.run_image.call_args
244+
245+
mount = [m for m in kwargs["mounts"] if m["Target"] == "/Lean/Report/bin/Debug/report_override.css"]
246+
assert len(mount) == 0
209247

210248
def test_report_finds_latest_backtest_data_source_file_when_not_given() -> None:
211249
docker_manager = mock.Mock()

0 commit comments

Comments
 (0)