-
Notifications
You must be signed in to change notification settings - Fork 100
/
report.py
278 lines (236 loc) · 12.6 KB
/
report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean CLI v1.0. Copyright 2021 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from pathlib import Path
from typing import Any, Dict, Optional
from click import command, option
from lean.click import LeanCommand, PathParameter
from lean.constants import DEFAULT_ENGINE_IMAGE, PROJECT_CONFIG_FILE_NAME
from lean.container import container
from lean.models.errors import MoreInfoError
from lean.components.util.live_utils import get_latest_result_json_file
def _find_project_directory(backtest_file: Path) -> Optional[Path]:
"""Returns the project directory, or None if backtest_file is not stored in a project directory.
:param backtest_file: the path to the JSON file containing the backtest results
:return: the path to the project directory, or None if backtest_file is stored outside a project directory
"""
current_directory = backtest_file.parent
# Loop until we find the root directory
while current_directory != current_directory.parent:
if (current_directory / PROJECT_CONFIG_FILE_NAME).is_file():
return current_directory
current_directory = current_directory.parent
return None
@command(cls=LeanCommand, requires_lean_config=True, requires_docker=True)
@option("--backtest-results",
type=PathParameter(exists=True, file_okay=True, dir_okay=False),
help="Path to the JSON file containing the backtest results")
@option("--live-results",
type=PathParameter(exists=True, file_okay=True, dir_okay=False),
help="Path to the JSON file containing the live trading results")
@option("--report-destination",
type=PathParameter(exists=False, file_okay=True, dir_okay=False),
default=lambda: Path.cwd() / "report.html",
help="Path where the generated report is stored as HTML (defaults to ./report.html)")
@option("--css",
type=PathParameter(exists=False, file_okay=True, dir_okay=False),
help="Path where the CSS override file is stored")
@option("--html",
type=PathParameter(exists=False, file_okay=True, dir_okay=False),
help="Path where the custom HTML template file is stored")
@option("--detach", "-d",
is_flag=True,
default=False,
help="Run the report creator in a detached Docker container and return immediately")
@option("--strategy-name",
type=str,
help="Name of the strategy, will appear at the top-right corner of each page")
@option("--strategy-version",
type=str,
help="Version number of the strategy, will appear next to the project name")
@option("--strategy-description",
type=str,
help="Description of the strategy, will appear under the 'Strategy Description' section")
@option("--overwrite",
is_flag=True,
default=False,
help="Overwrite --report-destination if it already contains a file")
@option("--image",
type=str,
help=f"The LEAN engine image to use (defaults to {DEFAULT_ENGINE_IMAGE})")
@option("--update",
is_flag=True,
default=False,
help="Pull the LEAN engine image before running the report creator")
@option("--pdf",
is_flag=True,
default=False,
help="Create a PDF version along with the HTML version of the report")
def report(backtest_results: Optional[Path],
live_results: Optional[Path],
report_destination: Path,
css: Optional[Path],
html: Optional[Path],
detach: bool,
strategy_name: Optional[str],
strategy_version: Optional[str],
strategy_description: Optional[str],
overwrite: bool,
image: Optional[str],
update: bool,
pdf: bool) -> None:
"""Generate a report of a backtest.
This runs the LEAN Report Creator in Docker to generate a polished, professional-grade report of a backtest.
If --backtest-results is not given, a report is generated for the most recent local backtest.
The name, description, and version are optional and will be blank if not given.
If the given backtest data source file is stored in a project directory (or one of its subdirectories, like the
default <project>/backtests/<timestamp>), the default name is the name of the project directory and the default
description is the description stored in the project's config.json file.
By default the official LEAN engine image is used.
You can override this using the --image option.
Alternatively you can set the default engine image for all commands using `lean config set engine-image <image>`.
"""
from json import dump
from docker.types import Mount
if report_destination.exists() and not overwrite:
raise RuntimeError(f"{report_destination} already exists, use --overwrite to overwrite it")
environment = "backtests"
output_config_manager = container.output_config_manager
output_directory = output_config_manager.get_latest_output_directory(environment)
if output_directory is None:
raise ValueError(f"No output {environment} directories were found. "
f"Make sure you run a backtest or live deployment first.")
if backtest_results is None:
backtest_results = get_latest_result_json_file(output_directory)
if not backtest_results:
raise MoreInfoError(
"Could not find a recent backtest result file, please use the --backtest-results option",
"https://www.lean.io/docs/v2/lean-cli/reports#02-Generate-Reports"
)
logger = container.logger
if live_results is None:
logger.info(f"Generating a report from '{backtest_results}'")
else:
logger.info(f"Generating a report from '{backtest_results}' and '{live_results}'")
project_directory = _find_project_directory(backtest_results)
if project_directory is not None:
if strategy_name is None:
strategy_name = project_directory.name
if strategy_description is None:
project_config_manager = container.project_config_manager
project_config = project_config_manager.get_project_config(project_directory)
strategy_description = project_config.get("description", "")
# The configuration given to the report creator
# See https://github.com/QuantConnect/Lean/blob/master/Report/config.example.json
report_config = {
"data-folder": "/Lean/Data",
"strategy-name": strategy_name or "",
"strategy-version": strategy_version or "",
"strategy-description": strategy_description or "",
"live-data-source-file": "live-data-source-file.json" if live_results is not None else "",
"backtest-data-source-file": "backtest-data-source-file.json",
"report-destination": "/tmp/report.html",
"report-css-override-file": "report_override.css" if (css is not None) and (css.exists()) else "",
"report-html-custom-file": "template_custom.html" if (html is not None) and (html.exists()) else "",
"report-format": "pdf" if pdf else "",
"environment": "report",
"log-handler": "QuantConnect.Logging.CompositeLogHandler",
"messaging-handler": "QuantConnect.Messaging.Messaging",
"job-queue-handler": "QuantConnect.Queues.JobQueue",
"api-handler": "QuantConnect.Api.Api",
"map-file-provider": "QuantConnect.Data.Auxiliary.LocalDiskMapFileProvider",
"factor-file-provider": "QuantConnect.Data.Auxiliary.LocalDiskFactorFileProvider",
"data-provider": "QuantConnect.Lean.Engine.DataFeeds.DefaultDataProvider",
"alpha-handler": "QuantConnect.Lean.Engine.Alphas.DefaultAlphaHandler",
"data-channel-provider": "DataChannelProvider",
"environments": {
"report": {
"live-mode": False,
"setup-handler": "QuantConnect.Lean.Engine.Setup.ConsoleSetupHandler",
"result-handler": "QuantConnect.Lean.Engine.Results.BacktestingResultHandler",
"data-feed-handler": "QuantConnect.Lean.Engine.DataFeeds.FileSystemDataFeed",
"real-time-handler": "QuantConnect.Lean.Engine.RealTime.BacktestingRealTimeHandler",
"history-provider": "QuantConnect.Lean.Engine.HistoricalData.SubscriptionDataReaderHistoryProvider",
"transaction-handler": "QuantConnect.Lean.Engine.TransactionHandlers.BacktestingTransactionHandler"
}
}
}
config_path = container.temp_manager.create_temporary_directory() / "config.json"
with config_path.open("w+", encoding="utf-8") as file:
dump(report_config, file)
backtest_id = container.output_config_manager.get_backtest_id(output_directory)
lean_config_manager = container.lean_config_manager
data_dir = lean_config_manager.get_data_directory()
report_destination.parent.mkdir(parents=True, exist_ok=True)
run_options: Dict[str, Any] = {
"detach": detach,
"name": f"lean_cli_report_{backtest_id}",
"working_dir": "/Lean/Report/bin/Debug",
"commands": ["dotnet QuantConnect.Report.dll", f'cp /tmp/report.html "/Output/{report_destination.name}"'],
"mounts": [
Mount(target="/Lean/Report/bin/Debug/config.json",
source=str(config_path),
type="bind",
read_only=True),
Mount(target="/Lean/Report/bin/Debug/backtest-data-source-file.json",
source=str(backtest_results),
type="bind",
read_only=True)
],
"volumes": {
str(data_dir): {
"bind": "/Lean/Data",
"mode": "rw"
},
str(report_destination.parent): {
"bind": "/Output",
"mode": "rw"
}
}
}
if css is not None:
if css.exists():
run_options["mounts"].append(Mount(target="/Lean/Report/bin/Debug/report_override.css",
source=str(css),
type="bind",
read_only=True))
else:
logger.info(f"CSS override file '{css}' could not be found")
if html is not None:
if html.exists():
run_options["mounts"].append(Mount(target="/Lean/Report/bin/Debug/template_custom.html",
source=str(html),
type="bind",
read_only=True))
else:
logger.info(f"Custom HTML template file '{html}' could not be found")
if pdf:
run_options["commands"].append(f'cp /tmp/report.pdf "/Output/{report_destination.name.replace(".html", ".pdf")}"')
if live_results is not None:
run_options["mounts"].append(Mount(target="/Lean/Report/bin/Debug/live-data-source-file.json",
source=str(live_results),
type="bind",
read_only=True))
engine_image, container_module_version, project_config = container.manage_docker_image(image, update, False,
project_directory)
success = container.docker_manager.run_image(engine_image, **run_options)
if not success:
raise RuntimeError(
"Something went wrong while running the LEAN Report Creator, see the logs above for more information")
if detach:
temp_manager = container.temp_manager
temp_manager.delete_temporary_directories_when_done = False
logger.info(f"Successfully started the report creator in the '{run_options['name']}' container")
logger.info(f"The report will be generated to '{report_destination}'")
logger.info("You can use Docker's own commands to manage the detached container")
return
logger.info(f"Successfully generated report to '{report_destination}'")