/
reports.py
636 lines (525 loc) · 24.8 KB
/
reports.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# QuantStats: Portfolio analytics for quants
# https://github.com/ranaroussi/quantstats
#
# Copyright 2019 Ran Aroussi
#
# 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 pandas as _pd
import numpy as _np
from datetime import (
datetime as _dt, timedelta as _td
)
import re as _regex
from tabulate import tabulate as _tabulate
from . import (
__version__, stats as _stats,
utils as _utils, plots as _plots
)
try:
from IPython.core.display import (
display as iDisplay, HTML as iHTML
)
except ImportError:
pass
def html(returns, benchmark=None, rf=0.,
grayscale=False, title='Strategy Tearsheet',
output=None, compounded=True):
if output is None and not _utils._in_notebook():
raise ValueError("`file` must be specified")
tpl = ""
with open(__file__[:-4] + '.html') as f:
tpl = f.read()
f.close()
date_range = returns.index.strftime('%e %b, %Y')
tpl = tpl.replace('{{date_range}}', date_range[0] + ' - ' + date_range[-1])
tpl = tpl.replace('{{title}}', title)
tpl = tpl.replace('{{v}}', __version__)
mtrx = metrics(returns=returns, benchmark=benchmark,
rf=rf, display=False, mode='full',
sep=True, internal="True",
compounded=compounded)[2:]
mtrx.index.name = 'Metric'
tpl = tpl.replace('{{metrics}}', _html_table(mtrx))
tpl = tpl.replace('<tr><td></td><td></td><td></td></tr>',
'<tr><td colspan="3"><hr></td></tr>')
tpl = tpl.replace('<tr><td></td><td></td></tr>',
'<tr><td colspan="2"><hr></td></tr>')
if benchmark is not None:
yoy = _stats.compare(returns, benchmark, "A", compounded=compounded)
yoy.columns = ['Benchmark', 'Strategy', 'Multiplier', 'Won']
yoy.index.name = 'Year'
tpl = tpl.replace('{{eoy_title}}', '<h3>EOY Returns vs Benchmark</h3>')
tpl = tpl.replace('{{eoy_table}}', _html_table(yoy))
else:
# pct multiplier
yoy = _pd.DataFrame(
_utils.group_returns(returns, returns.index.year) * 100)
yoy.columns = ['Return']
yoy['Cumulative'] = _utils.group_returns(
returns, returns.index.year, True)
yoy['Return'] = yoy['Return'].round(2).astype(str) + '%'
yoy['Cumulative'] = (yoy['Cumulative'] *
100).round(2).astype(str) + '%'
yoy.index.name = 'Year'
tpl = tpl.replace('{{eoy_title}}', '<h3>EOY Returns</h3>')
tpl = tpl.replace('{{eoy_table}}', _html_table(yoy))
dd = _stats.to_drawdown_series(returns)
dd_info = _stats.drawdown_details(dd).sort_values(
by='max drawdown', ascending=True)[:10]
dd_info = dd_info[['start', 'end', 'max drawdown', 'days']]
dd_info.columns = ['Started', 'Recovered', 'Drawdown', 'Days']
tpl = tpl.replace('{{dd_info}}', _html_table(dd_info, False))
# plots
figfile = _utils._file_stream()
_plots.returns(returns, benchmark, grayscale=grayscale,
figsize=(8, 5), subtitle=False,
savefig={'fname': figfile, 'format': 'svg'},
show=False, ylabel=False, cumulative=compounded)
tpl = tpl.replace('{{returns}}', figfile.getvalue().decode())
figfile = _utils._file_stream()
_plots.log_returns(returns, benchmark, grayscale=grayscale,
figsize=(8, 4), subtitle=False,
savefig={'fname': figfile, 'format': 'svg'},
show=False, ylabel=False, cumulative=compounded)
tpl = tpl.replace('{{log_returns}}', figfile.getvalue().decode())
if benchmark is not None:
figfile = _utils._file_stream()
_plots.returns(returns, benchmark, match_volatility=True,
grayscale=grayscale, figsize=(8, 4), subtitle=False,
savefig={'fname': figfile, 'format': 'svg'},
show=False, ylabel=False, cumulative=compounded)
tpl = tpl.replace('{{vol_returns}}', figfile.getvalue().decode())
figfile = _utils._file_stream()
_plots.yearly_returns(returns, benchmark, grayscale=grayscale,
figsize=(8, 4), subtitle=False,
savefig={'fname': figfile, 'format': 'svg'},
show=False, ylabel=False, compounded=compounded)
tpl = tpl.replace('{{eoy_returns}}', figfile.getvalue().decode())
figfile = _utils._file_stream()
_plots.histogram(returns, grayscale=grayscale,
figsize=(8, 4), subtitle=False,
savefig={'fname': figfile, 'format': 'svg'},
show=False, ylabel=False, compounded=compounded)
tpl = tpl.replace('{{monthly_dist}}', figfile.getvalue().decode())
figfile = _utils._file_stream()
_plots.daily_returns(returns, grayscale=grayscale,
figsize=(8, 3), subtitle=False,
savefig={'fname': figfile, 'format': 'svg'},
show=False, ylabel=False)
tpl = tpl.replace('{{daily_returns}}', figfile.getvalue().decode())
if benchmark is not None:
figfile = _utils._file_stream()
_plots.rolling_beta(returns, benchmark, grayscale=grayscale,
figsize=(8, 3), subtitle=False,
savefig={'fname': figfile, 'format': 'svg'},
show=False, ylabel=False)
tpl = tpl.replace('{{rolling_beta}}', figfile.getvalue().decode())
figfile = _utils._file_stream()
_plots.rolling_volatility(returns, benchmark, grayscale=grayscale,
figsize=(8, 3), subtitle=False,
savefig={'fname': figfile, 'format': 'svg'},
show=False, ylabel=False)
tpl = tpl.replace('{{rolling_vol}}', figfile.getvalue().decode())
figfile = _utils._file_stream()
_plots.rolling_sharpe(returns, grayscale=grayscale,
figsize=(8, 3), subtitle=False,
savefig={'fname': figfile, 'format': 'svg'},
show=False, ylabel=False)
tpl = tpl.replace('{{rolling_sharpe}}', figfile.getvalue().decode())
figfile = _utils._file_stream()
_plots.rolling_sortino(returns, grayscale=grayscale,
figsize=(8, 3), subtitle=False,
savefig={'fname': figfile, 'format': 'svg'},
show=False, ylabel=False)
tpl = tpl.replace('{{rolling_sortino}}', figfile.getvalue().decode())
figfile = _utils._file_stream()
_plots.drawdowns_periods(returns, grayscale=grayscale,
figsize=(8, 4), subtitle=False,
savefig={'fname': figfile, 'format': 'svg'},
show=False, ylabel=False, compounded=compounded)
tpl = tpl.replace('{{dd_periods}}', figfile.getvalue().decode())
figfile = _utils._file_stream()
_plots.drawdown(returns, grayscale=grayscale,
figsize=(8, 3), subtitle=False,
savefig={'fname': figfile, 'format': 'svg'},
show=False, ylabel=False)
tpl = tpl.replace('{{dd_plot}}', figfile.getvalue().decode())
figfile = _utils._file_stream()
_plots.monthly_heatmap(returns, grayscale=grayscale,
figsize=(8, 4), cbar=False,
savefig={'fname': figfile, 'format': 'svg'},
show=False, ylabel=False, compounded=compounded)
tpl = tpl.replace('{{monthly_heatmap}}', figfile.getvalue().decode())
figfile = _utils._file_stream()
_plots.distribution(returns, grayscale=grayscale,
figsize=(8, 4), subtitle=False,
savefig={'fname': figfile, 'format': 'svg'},
show=False, ylabel=False, compounded=compounded)
tpl = tpl.replace('{{returns_dist}}', figfile.getvalue().decode())
tpl = _regex.sub(r'\{\{(.*?)\}\}', '', tpl)
tpl = tpl.replace('white-space:pre;', '')
if output is None:
# _open_html(tpl)
_download_html(tpl, 'quantstats-tearsheet.html')
return
with open(output, 'w', encoding='utf-8') as f:
f.write(tpl)
def full(returns, benchmark=None, rf=0., grayscale=False,
figsize=(8, 5), display=True, compounded=True):
dd = _stats.to_drawdown_series(returns)
dd_info = _stats.drawdown_details(dd).sort_values(
by='max drawdown', ascending=True)[:5]
if not dd_info.empty:
dd_info.index = range(1, min(6, len(dd_info)+1))
dd_info.columns = map(lambda x: str(x).title(), dd_info.columns)
if _utils._in_notebook():
iDisplay(iHTML('<h4>Performance Metrics</h4>'))
iDisplay(metrics(returns=returns, benchmark=benchmark,
rf=rf, display=display, mode='full',
compounded=compounded))
iDisplay(iHTML('<h4>5 Worst Drawdowns</h4>'))
if dd_info.empty:
iDisplay(iHTML("<p>(no drawdowns)</p>"))
else:
iDisplay(dd_info)
iDisplay(iHTML('<h4>Strategy Visualization</h4>'))
else:
print('[Performance Metrics]\n')
metrics(returns=returns, benchmark=benchmark,
rf=rf, display=display, mode='full',
compounded=compounded)
print('\n\n')
print('[5 Worst Drawdowns]\n')
if dd_info.empty:
print("(no drawdowns)")
else:
print(_tabulate(dd_info, headers="keys",
tablefmt='simple', floatfmt=".2f"))
print('\n\n')
print('[Strategy Visualization]\nvia Matplotlib')
plots(returns=returns, benchmark=benchmark,
grayscale=grayscale, figsize=figsize, mode='full')
def basic(returns, benchmark=None, rf=0., grayscale=False,
figsize=(8, 5), display=True, compounded=True):
if _utils._in_notebook():
iDisplay(iHTML('<h4>Performance Metrics</h4>'))
metrics(returns=returns, benchmark=benchmark,
rf=rf, display=display, mode='basic',
compounded=compounded)
iDisplay(iHTML('<h4>Strategy Visualization</h4>'))
else:
print('[Performance Metrics]\n')
metrics(returns=returns, benchmark=benchmark,
rf=rf, display=display, mode='basic',
compounded=compounded)
print('\n\n')
print('[Strategy Visualization]\nvia Matplotlib')
plots(returns=returns, benchmark=benchmark,
grayscale=grayscale, figsize=figsize, mode='basic')
def metrics(returns, benchmark=None, rf=0., display=True,
mode='basic', sep=False, compounded=True, **kwargs):
if isinstance(returns, _pd.DataFrame) and len(returns.columns) > 1:
raise ValueError("`returns` must be a pandas Series, "
"but a multi-column DataFrame was passed")
if benchmark is not None:
if isinstance(returns, _pd.DataFrame) and len(returns.columns) > 1:
raise ValueError("`benchmark` must be a pandas Series, "
"but a multi-column DataFrame was passed")
blank = ['']
df = _utils._prepare_returns(returns, rf)
df.columns = ["returns"]
if benchmark is not None:
blank = ['', '']
df["benchmark"] = _utils._prepare_benchmark(
benchmark, returns.index, rf)
df = df.fillna(0)
# pct multiplier
pct = 100 if display or "internal" in kwargs else 1
# return df
dd = _calc_dd(df, display=(display or "internal" in kwargs))
metrics = _pd.DataFrame()
s_start = {'returns': df['returns'].index.strftime('%Y-%m-%d')[0]}
s_end = {'returns': df['returns'].index.strftime('%Y-%m-%d')[-1]}
s_rf = {'returns': rf}
if "benchmark" in df:
s_start['benchmark'] = df['benchmark'].index.strftime('%Y-%m-%d')[0]
s_end['benchmark'] = df['benchmark'].index.strftime('%Y-%m-%d')[-1]
s_rf['benchmark'] = rf
metrics['Start Period'] = _pd.Series(s_start)
metrics['End Period'] = _pd.Series(s_end)
metrics['Risk-Free Rate %'] = _pd.Series(s_rf)
metrics['Time in Market %'] = _stats.exposure(df) * pct
metrics['~'] = blank
if compounded:
metrics['Cumulative Return %'] = (
_stats.comp(df) * pct).map('{:,.2f}'.format)
else:
metrics['Total Return %'] = (df.sum() * pct).map('{:,.2f}'.format)
metrics['CAGR%%'] = _stats.cagr(df, rf, compounded) * pct
metrics['Sharpe'] = _stats.sharpe(df, rf)
metrics['Sortino'] = _stats.sortino(df, rf)
metrics['Max Drawdown %'] = blank
metrics['Longest DD Days'] = blank
if mode.lower() == 'full':
ret_vol = _stats.volatility(df['returns']) * pct
if "benchmark" in df:
bench_vol = _stats.volatility(df['benchmark']) * pct
metrics['Volatility (ann.) %'] = [ret_vol, bench_vol]
metrics['R^2'] = _stats.r_squared(df['returns'], df['benchmark'])
else:
metrics['Volatility (ann.) %'] = [ret_vol]
metrics['Calmar'] = _stats.calmar(df)
metrics['Skew'] = _stats.skew(df)
metrics['Kurtosis'] = _stats.kurtosis(df)
metrics['~~~~~~~~~~'] = blank
metrics['Expected Daily %%'] = _stats.expected_return(df) * pct
metrics['Expected Monthly %%'] = _stats.expected_return(
df, aggregate='M') * pct
metrics['Expected Yearly %%'] = _stats.expected_return(
df, aggregate='A') * pct
metrics['Kelly Criterion %'] = _stats.kelly_criterion(df) * pct
metrics['Risk of Ruin %'] = _stats.risk_of_ruin(df)
metrics['Daily Value-at-Risk %'] = -abs(_stats.var(df) * pct)
metrics['Expected Shortfall (cVaR) %'] = -abs(_stats.cvar(df) * pct)
metrics['~~~~~~'] = blank
metrics['Payoff Ratio'] = _stats.payoff_ratio(df)
metrics['Profit Factor'] = _stats.profit_factor(df)
metrics['Common Sense Ratio'] = _stats.common_sense_ratio(df)
metrics['CPC Index'] = _stats.cpc_index(df)
metrics['Tail Ratio'] = _stats.tail_ratio(df)
metrics['Outlier Win Ratio'] = _stats.outlier_win_ratio(df)
metrics['Outlier Loss Ratio'] = _stats.outlier_loss_ratio(df)
# returns
metrics['~~'] = blank
comp_func = _stats.comp if compounded else _np.sum
today = df.index[-1] # _dt.today()
metrics['MTD %'] = comp_func(
df[df.index >= _dt(today.year, today.month, 1)]) * pct
d = today - _td(3*365/12)
metrics['3M %'] = comp_func(
df[df.index >= _dt(d.year, d.month, d.day)]) * pct
d = today - _td(6*365/12)
metrics['6M %'] = comp_func(
df[df.index >= _dt(d.year, d.month, d.day)]) * pct
metrics['YTD %'] = comp_func(df[df.index >= _dt(today.year, 1, 1)]) * pct
d = today - _td(12*365/12)
metrics['1Y %'] = comp_func(
df[df.index >= _dt(d.year, d.month, d.day)]) * pct
metrics['3Y (ann.) %'] = _stats.cagr(
df[df.index >= _dt(today.year-3, today.month, today.day)
], 0., compounded) * pct
metrics['5Y (ann.) %'] = _stats.cagr(
df[df.index >= _dt(today.year-5, today.month, today.day)
], 0., compounded) * pct
metrics['10Y (ann.) %'] = _stats.cagr(
df[df.index >= _dt(today.year-10, today.month, today.day)
], 0., compounded) * pct
metrics['All-time (ann.) %'] = _stats.cagr(df, 0., compounded) * pct
# best/worst
if mode.lower() == 'full':
metrics['~~~'] = blank
metrics['Best Day %'] = _stats.best(df) * pct
metrics['Worst Day %'] = _stats.worst(df) * pct
metrics['Best Month %'] = _stats.best(df, aggregate='M') * pct
metrics['Worst Month %'] = _stats.worst(df, aggregate='M') * pct
metrics['Best Year %'] = _stats.best(df, aggregate='A') * pct
metrics['Worst Year %'] = _stats.worst(df, aggregate='A') * pct
# dd
metrics['~~~~'] = blank
for ix, row in dd.iterrows():
metrics[ix] = row
metrics['Recovery Factor'] = _stats.recovery_factor(df)
metrics['Ulcer Index'] = _stats.ulcer_index(df, rf)
# win rate
if mode.lower() == 'full':
metrics['~~~~~'] = blank
metrics['Avg. Up Month %'] = _stats.avg_win(df, aggregate='M') * pct
metrics['Avg. Down Month %'] = _stats.avg_loss(df, aggregate='M') * pct
metrics['Win Days %%'] = _stats.win_rate(df) * pct
metrics['Win Month %%'] = _stats.win_rate(df, aggregate='M') * pct
metrics['Win Quarter %%'] = _stats.win_rate(df, aggregate='Q') * pct
metrics['Win Year %%'] = _stats.win_rate(df, aggregate='A') * pct
if "benchmark" in df:
metrics['~~~~~~~'] = blank
greeks = _stats.greeks(df['returns'], df['benchmark'])
metrics['Beta'] = [str(round(greeks['beta'], 2)), '-']
metrics['Alpha'] = [str(round(greeks['alpha'], 2)), '-']
# prepare for display
for col in metrics.columns:
try:
metrics[col] = metrics[col].astype(float).round(2)
if display or "internal" in kwargs:
metrics[col] = metrics[col].astype(str)
except Exception:
pass
if (display or "internal" in kwargs) and "%" in col:
metrics[col] = metrics[col] + '%'
try:
metrics['Longest DD Days'] = _pd.to_numeric(
metrics['Longest DD Days']).astype('int')
metrics['Avg. Drawdown Days'] = _pd.to_numeric(
metrics['Avg. Drawdown Days']).astype('int')
if display or "internal" in kwargs:
metrics['Longest DD Days'] = metrics['Longest DD Days'].astype(str)
metrics['Avg. Drawdown Days'] = metrics['Avg. Drawdown Days'
].astype(str)
except Exception:
metrics['Longest DD Days'] = '-'
metrics['Avg. Drawdown Days'] = '-'
if display or "internal" in kwargs:
metrics['Longest DD Days'] = '-'
metrics['Avg. Drawdown Days'] = '-'
metrics.columns = [
col if '~' not in col else '' for col in metrics.columns]
metrics.columns = [
col[:-1] if '%' in col else col for col in metrics.columns]
metrics = metrics.T
if "benchmark" in df:
metrics.columns = ['Strategy', 'Benchmark']
else:
metrics.columns = ['Strategy']
if display:
print(_tabulate(metrics, headers="keys", tablefmt='simple'))
return None
if not sep:
metrics = metrics[metrics.index != '']
return metrics
def plots(returns, benchmark=None, grayscale=False,
figsize=(8, 5), mode='basic', compounded=True):
if mode.lower() != 'full':
_plots.snapshot(returns, grayscale=grayscale,
figsize=(figsize[0], figsize[0]),
show=True, mode=("comp" if compounded else "sum"))
_plots.monthly_heatmap(returns, grayscale=grayscale,
figsize=(figsize[0], figsize[0]*.5),
show=True, ylabel=False,
compounded=compounded)
return
_plots.returns(returns, benchmark, grayscale=grayscale,
figsize=(figsize[0], figsize[0]*.6),
show=True, ylabel=False)
_plots.log_returns(returns, benchmark, grayscale=grayscale,
figsize=(figsize[0], figsize[0]*.5),
show=True, ylabel=False)
if benchmark is not None:
_plots.returns(returns, benchmark, match_volatility=True,
grayscale=grayscale,
figsize=(figsize[0], figsize[0]*.5),
show=True, ylabel=False)
_plots.yearly_returns(returns, benchmark,
grayscale=grayscale,
figsize=(figsize[0], figsize[0]*.5),
show=True, ylabel=False)
_plots.histogram(returns, grayscale=grayscale,
figsize=(figsize[0], figsize[0]*.5),
show=True, ylabel=False)
_plots.daily_returns(returns, grayscale=grayscale,
figsize=(figsize[0], figsize[0]*.3),
show=True, ylabel=False)
if benchmark is not None:
_plots.rolling_beta(returns, benchmark, grayscale=grayscale,
figsize=(figsize[0], figsize[0]*.3),
show=True, ylabel=False)
_plots.rolling_volatility(
returns, benchmark, grayscale=grayscale,
figsize=(figsize[0], figsize[0]*.3), show=True, ylabel=False)
_plots.rolling_sharpe(returns, grayscale=grayscale,
figsize=(figsize[0], figsize[0]*.3),
show=True, ylabel=False)
_plots.rolling_sortino(returns, grayscale=grayscale,
figsize=(figsize[0], figsize[0]*.3),
show=True, ylabel=False)
_plots.drawdowns_periods(returns, grayscale=grayscale,
figsize=(figsize[0], figsize[0]*.5),
show=True, ylabel=False)
_plots.drawdown(returns, grayscale=grayscale,
figsize=(figsize[0], figsize[0]*.4),
show=True, ylabel=False)
_plots.monthly_heatmap(returns, grayscale=grayscale,
figsize=(figsize[0], figsize[0]*.5),
show=True, ylabel=False)
_plots.distribution(returns, grayscale=grayscale,
figsize=(figsize[0], figsize[0]*.5),
show=True, ylabel=False)
def _calc_dd(df, display=True):
dd = _stats.to_drawdown_series(df)
dd_info = _stats.drawdown_details(dd)
if dd_info.empty:
return _pd.DataFrame()
if "returns" in dd_info:
ret_dd = dd_info['returns']
else:
ret_dd = dd_info
# pct multiplier
pct = 1 if display else 100
dd_stats = {
'returns': {
'Max Drawdown %': ret_dd.sort_values(
by='max drawdown', ascending=True
)['max drawdown'].values[0] / pct,
'Longest DD Days': str(round(ret_dd.sort_values(
by='days', ascending=False)['days'].values[0])),
'Avg. Drawdown %': ret_dd['max drawdown'].mean() / pct,
'Avg. Drawdown Days': str(round(ret_dd['days'].mean()))
}
}
if "benchmark" in df and (dd_info.columns, _pd.MultiIndex):
bench_dd = dd_info['benchmark'].sort_values(by='max drawdown')
dd_stats['benchmark'] = {
'Max Drawdown %': bench_dd.sort_values(
by='max drawdown', ascending=True
)['max drawdown'].values[0] / pct,
'Longest DD Days': str(round(bench_dd.sort_values(
by='days', ascending=False)['days'].values[0])),
'Avg. Drawdown %': bench_dd['max drawdown'].mean() / pct,
'Avg. Drawdown Days': str(round(bench_dd['days'].mean()))
}
dd_stats = _pd.DataFrame(dd_stats).T
dd_stats['Max Drawdown %'] = dd_stats['Max Drawdown %'].astype(float)
dd_stats['Avg. Drawdown %'] = dd_stats['Avg. Drawdown %'].astype(float)
return dd_stats.T
def _html_table(obj, showindex="default"):
obj = _tabulate(obj, headers="keys", tablefmt='html',
floatfmt=".2f", showindex=showindex)
obj = obj.replace(' style="text-align: right;"', '')
obj = obj.replace(' style="text-align: left;"', '')
obj = obj.replace(' style="text-align: center;"', '')
obj = _regex.sub('<td> +', '<td>', obj)
obj = _regex.sub(' +</td>', '</td>', obj)
obj = _regex.sub('<th> +', '<th>', obj)
obj = _regex.sub(' +</th>', '</th>', obj)
return obj
def _download_html(html, filename="quantstats-tearsheet.html"):
jscode = _regex.sub(' +', ' ', """<script>
var bl=new Blob(['{{html}}'],{type:"text/html"});
var a=document.createElement("a");
a.href=URL.createObjectURL(bl);
a.download="{{filename}}";
a.hidden=true;document.body.appendChild(a);
a.innerHTML="download report";
a.click();</script>""".replace('\n', ''))
jscode = jscode.replace('{{html}}', _regex.sub(
' +', ' ', html.replace('\n', '')))
if _utils._in_notebook():
iDisplay(iHTML(jscode.replace('{{filename}}', filename)))
def _open_html(html):
jscode = _regex.sub(' +', ' ', """<script>
var win=window.open();win.document.body.innerHTML='{{html}}';
</script>""".replace('\n', ''))
jscode = jscode.replace('{{html}}', _regex.sub(
' +', ' ', html.replace('\n', '')))
if _utils._in_notebook():
iDisplay(iHTML(jscode))