forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeanReportCreator.py
408 lines (357 loc) · 16.4 KB
/
LeanReportCreator.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 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.
import os
import json
from quantconnect.LeanOutputReader import LeanOutputReader
class LeanReportCreator(object):
def __init__(self, argv, save_images = True):
input, self.output, user_data = self.read_input(argv)
self.user = self.read_user_data(user_data)
self.hash = self.user.pop('backtestHash', '')
self.count = 0
# Read input file and pass it to the LeanOutputReader
data = dict()
with open(input, 'r') as fp:
data = json.load(fp)
outdir = os.path.dirname(self.output) if save_images else None
self.reader = LeanOutputReader(data, 200, outdir)
def read_input(self, args):
if type(args) is str:
args = args.split(' ')
tmp = next((x for x in args if x.strip().startswith('--backtest')), None)
if tmp is None:
raise KeyError('Please provide --backtest=file.json argument')
input = os.path.abspath(tmp[11:])
if not os.path.isfile(input):
raise FileNotFoundError(f'Backtest file not found: {input}')
tmp = next((x for x in args if x.strip().startswith('--user')), None)
if tmp is None:
tmp = '--user=user_data.json'
user = tmp[7:]
tmp = next((x for x in args if x.strip().startswith('--output')), None)
if tmp is None:
tmp = f'--output={input[:-5]}.html'
output = os.path.abspath(tmp[9:])
# create output directory
os.makedirs(os.path.dirname(output), exist_ok = True)
return input, output, user
def read_user_data(self, file):
if os.path.isfile(file):
with open(file, 'r', encoding = "utf-8") as fp:
return json.load(fp)
return {
"authorName": "QuantConnect User",
"authorPicture": "AuthorProfile.png",
"authorBiography": "Put your biography here.",
"projectName": "Basic Template Algorithm",
"projectDescription": "Basic Template Algorithm",
}
def get_footer(self):
self.count += 1
return f'''
<div class="footer">
<div class="footer-id">Hash: {self.hash} </div>
Democratizing Finance, Empowering Individuals
<div class="footer-page">{self.count}</div>
</div>'''
def create(self):
assets = self.reader.asset_allocation()
for title, image in assets.items():
assets[title] = self.get_image_box(title, image)
crisis = self.reader.crisis_events()
for title, image in crisis.items():
crisis[title] = self.get_image_box(title, image)
chartAssetAllocation = assets.pop("Asset Allocation", str())
chartAnnualReturns = self.reader.annual_returns()
chartCumulativeReturns = self.reader.cumulative_return()
chartMonthlyReturns = self.get_image_box('Monthly Returns', self.reader.monthly_returns())
chartReturnsHistogram = self.get_image_box('Return Histogram', self.reader.monthly_return_distribution())
chartDrawdown = self.get_image_box('Drawdown', self.reader.drawdown(), 12)
chartDailyReturns = self.get_image_box('Daily Returns', self.reader.daily_returns(), 12)
chartRollingBeta = self.get_image_box('Rolling Portfolio Beta to Equity', self.reader.rolling_beta(), 12)
chartRollingSP = self.get_image_box('Rolling Sharpe Ratio (6 Months)', self.reader.rolling_sharpe(), 12)
chartNetHoldings = self.get_image_box('Net Holdings', self.reader.net_holdings(), 12)
chartLeverage = self.get_image_box('Leverage', self.reader.leverage(), 12)
tmp = self.reader.statistics()
keyStatistics = self.get_table('Key Statistics', tmp.get("Key Statistics"))
keyCharacteristics = self.get_table('Key Characteristics', tmp.get("Key Characteristics"))
locationPrefix = "https://www.quantconnect.com/terminal"
downloadButton = '' if len(self.hash) == 0 else f'''
<div style="position: fixed;bottom: 50px;right: 50px;">
<a href="https://www.quantconnect.com/terminal/processReports/get/pdf/{self.hash}" target="_blank" class="btn btn-lg btn-default hide-print">
<i class="fa fa-download"></i>
</a>
</div>'''
html = '''<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta property="og:title" content="QuantConnect Backtest Report: ''' + self.user['projectName'] + '''"/>
<meta property="og:type" content="website"/>
<meta property="og:site_name" content="QuantConnect.com"/>
<meta property="og:description" content="''' + self.user['projectDescription'] + '''"/>
<meta property="og:url" content="https://www.quantconnect.com/terminal/reports/''' + self.hash + '''"/>
<meta property="og:image" content="''' + chartCumulativeReturns + '''"/>
<meta property="og:image" content="''' + chartAnnualReturns + '''"/>
<meta property="og:image" content=''' + self.user['authorPicture'] + '''"/>
<link rel="stylesheet" href="''' + locationPrefix + '''/css/reports/bootstrap.css">
<link rel="stylesheet" href="''' + locationPrefix + '''/css/reports/font-awesome.css">
<link rel="stylesheet" href="''' + locationPrefix + '''/css/reports/pdf.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="''' + locationPrefix + '''/js/libraries/jquery.js"></script>
<script src="''' + locationPrefix + '''/js/libraries/bootstrap.min.js"></script>
<script src="''' + locationPrefix + '''/js/libraries/jquery.growl.js"></script>
<link rel="stylesheet" href="''' + locationPrefix + '''/css/libraries/jquery.growl.css">
<style>
#table-summary .fa-times {
color: #d9534f;
}
#key-statistics tr > td:first-child,
#author-metadata tr > td:first-child,
#project-metadata tr > td:first-child,
#key-statistics tr > td:first-child,
#key-characteristics tr > td:first-child {
font-family: Norpeth-Bold, Helvetica, Arial, sans-serif;
font-weight: bold;
}
#author-metadata tr > td:first-child,
#project-metadata tr > td:first-child,
#key-characteristics tr > td:first-child {
width: 66%;
}
#key-characteristics tr > td:first-child {
width: 50%;
}
#key-characteristics tr > td:last-child {
text-align: center;
padding: 0;
}
#author-metadata tr > td:last-child,
#project-metadata tr > td:last-child {
text-align: center;
}
#key-statistics tr > td:last-child {
text-align: right;
}
#key-statistics tr > td,
#key-characteristics tr > td,
table.table tr > td {
vertical-align: middle;
}
table.table.align-top tr > td {
vertical-align: top;
}
.table.qc-table.compact {
height: 234px;
}
.table.qc-table.compact img {
width: 100%;
height: 100%;
max-height: 196px;
}
#author-box img {
max-width: 33%;
height: auto;
max-height: 33%;
float: left;
margin-right: 5px;
margin-bottom: 5px;
}
.header .header-title {
margin-top: 40px;
line-height: 30px;
position: absolute;
left: 0;
right: 0;
text-align: center;
}
#strategy-description,
#author-bio {
max-height: 194px;
word-wrap: break-word;
height: 100%;
}
</style>
</head>
<body>''' + downloadButton + '''
<div class="page">
<div class="header">
<div class="header-left">
<img src="https://cdn.quantconnect.com/web/i/logo.png">
</div>
<div class="header-title">Strategy Report Summary</div>
<div class="header-right">''' + self.user['projectName'] + '''</div>
</div>
<div class="content">
<h1 class="hidden">Strategy Report</h1>
<div class="container-row">
<div class="col-xs-8">
<table id="description-box" class="table qc-table compact no-margin align-top">
<thead>
<tr>
<th>
Strategy Description
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<p id="strategy-description" class="text-justify editable" style="max-width: 590px;overflow: hidden;height:193px">''' + self.user['projectDescription'] + '''</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="col-xs-4">
<table id="author-box" class="table qc-table compact no-margin align-top">
<thead>
<tr>
<th>
About the Author <span class="pull-right">''' + self.user['authorName'] + '''</span>
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="overflow: hidden;">
<img src="''' + self.user['authorPicture'] + '''">
<p id="author-bio" class="text-justify editable" style="max-width: 286px;height:193px">''' + self.user['authorBiography']+ '''</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="container-row">
''' + keyCharacteristics + '''
''' + keyStatistics + '''
''' + chartMonthlyReturns + '''
</div>
<div class="container-row">
''' + self.get_image_box('Cumulative Returns', chartCumulativeReturns, 12) + '''
</div>
<div class="container-row">
''' + self.get_image_box('Annual Returns', chartAnnualReturns) + '''
''' + chartReturnsHistogram + '''
''' + chartAssetAllocation + '''
</div>
<div class="container-row">
''' + chartDrawdown + '''
</div>
</div>
''' + self.get_footer() + '''
</div>
<div class="page">
<div class="header">
<div class="header-left">
<img src="https://cdn.quantconnect.com/web/i/logo.png">
</div>
<div class="header-title">Backtest Strategy Analysis</div>
<div class="header-right">''' + self.user['projectName'] + '''</div>
</div>
<div class="content">
<div class="container-row">
''' + chartDailyReturns + '''
</div>
<div class="container-row">
''' + chartRollingBeta + '''
</div>
<div class="container-row">
''' + chartRollingSP + '''
</div>
<div class="container-row">
''' + chartNetHoldings + '''
</div>
<div class="container-row">
''' + chartLeverage + '''
</div>
</div>
''' + self.get_footer() + '''
</div>
''' + self.get_page_from_dict("Backtest Crisis Analysis", crisis) + '''
''' + self.get_page_from_dict("Asset Allocation", assets) + '''
</body>
</html>'''
with open(self.output, 'w', encoding = "utf-8") as fp:
fp.write(html)
return html
def clean(self):
outdir = os.path.dirname(self.output)
items = os.listdir(outdir)
for item in items:
if item.endswith(".png"):
os.remove(os.path.join(outdir, item))
def get_table(self, title, dict):
ret = f'''
<div class="col-xs-4">
<table id="key-characteristics" class="table qc-table compact">
<thead><tr>
<th colspan="2">{title}</th>
</tr></thead>
<tbody>'''
for title, value in dict.items():
if isinstance(value, list):
value = ", ".join(value)
if isinstance(value, bool):
value = '✔' if value else '✖'
ret += f'''<tr><td>{title}</td><td>{value}</td></tr>'''
return ret + '''
</tbody>
</table>
</div>'''
def get_image_box(self, title, url, col = 4):
return "" if not url else '''
<div class="col-xs-''' + str(col) + '''">
<table class="table qc-table compact">
<thead>
<tr>
<th>''' + title + '''</th>
</tr>
</thead>
<tbody>
<tr>
<td>
''' + (( '''<img src="''' + url + '''">''' ) if url else "") + '''
</td>
</tr>
</tbody>
</table>
</div>'''
def get_image_from_dict(self, dict):
ret = '''<div class="content">'''
titles = list(dict.keys())
stop = min(15, len(titles))
for i in range(0, stop, 3):
ret += '''<div class="container-row">'''
for j in range(0, 3):
if i + j >= stop: continue
ret += dict.pop(titles[i + j])
ret += '''</div>'''
return ret + '''</div>'''
def get_page_from_dict(self, title, dict):
ret = ''''''
while(len(dict) > 0):
ret += '''
<div class="page">
<div class="header">
<div class="header-left">
<img src="https://cdn.quantconnect.com/web/i/logo.png">
</div>
<div class="header-title">''' + title + '''</div>
<div class="header-right">''' + self.user['projectName'] + '''</div>
</div> ''' + self.get_image_from_dict(dict) + '''
''' + self.get_footer() + '''
</div>'''
return ret