quantstats-pro 0.4.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- quantstats/__init__.py +163 -0
- quantstats/_compat.py +431 -0
- quantstats/_montecarlo.py +296 -0
- quantstats/_numpy_compat.py +289 -0
- quantstats/_plotting/__init__.py +0 -0
- quantstats/_plotting/core.py +2173 -0
- quantstats/_plotting/wrappers.py +2109 -0
- quantstats/_reporting/__init__.py +12 -0
- quantstats/_reporting/engine.py +37 -0
- quantstats/_reporting/sections.py +223 -0
- quantstats/alphadecay/__init__.py +25 -0
- quantstats/alphadecay/core.py +712 -0
- quantstats/alphadecay/plotting.py +206 -0
- quantstats/montecarlo/__init__.py +34 -0
- quantstats/montecarlo/analytics.py +291 -0
- quantstats/montecarlo/base.py +92 -0
- quantstats/montecarlo/core.py +175 -0
- quantstats/montecarlo/models/__init__.py +15 -0
- quantstats/montecarlo/models/bayesian.py +88 -0
- quantstats/montecarlo/models/block_bootstrap.py +108 -0
- quantstats/montecarlo/models/bootstrap.py +38 -0
- quantstats/montecarlo/models/garch.py +83 -0
- quantstats/montecarlo/models/gbm.py +44 -0
- quantstats/montecarlo/models/heston.py +103 -0
- quantstats/montecarlo/models/jump_diffusion.py +86 -0
- quantstats/montecarlo/models/shuffle.py +49 -0
- quantstats/montecarlo/models/trimmed.py +50 -0
- quantstats/montecarlo/plotting.py +324 -0
- quantstats/montecarlo/registry.py +34 -0
- quantstats/plots.py +27 -0
- quantstats/py.typed +0 -0
- quantstats/report.html +64 -0
- quantstats/report_alpha_decay.html +43 -0
- quantstats/report_montecarlo.html +62 -0
- quantstats/report_simple.html +58 -0
- quantstats/reports.py +3502 -0
- quantstats/stats.py +3321 -0
- quantstats/utils.py +1011 -0
- quantstats/version.py +1 -0
- quantstats_pro-0.4.1.dist-info/METADATA +509 -0
- quantstats_pro-0.4.1.dist-info/RECORD +43 -0
- quantstats_pro-0.4.1.dist-info/WHEEL +4 -0
- quantstats_pro-0.4.1.dist-info/licenses/LICENSE.txt +202 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Section-based HTML reporting engine for QuantStats Pro.
|
|
3
|
+
|
|
4
|
+
This package powers the newer tearsheets (``reports.html_simple``,
|
|
5
|
+
``reports.html_montecarlo``, and ``reports.html_alpha_decay``) without
|
|
6
|
+
modifying the legacy monolithic ``reports.html`` path. The legacy report
|
|
7
|
+
keeps its bespoke rendering for backwards compatibility.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from . import engine, sections
|
|
11
|
+
|
|
12
|
+
__all__ = ["engine", "sections"]
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Template rendering engine for section-based tearsheets.
|
|
3
|
+
|
|
4
|
+
The engine fills ``{{key}}`` placeholders from a fragments mapping and strips
|
|
5
|
+
any placeholder left unfilled, generalising the string-replace + regex cleanup
|
|
6
|
+
pattern that the legacy ``reports.html`` performs inline.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import re as _regex
|
|
10
|
+
|
|
11
|
+
_PLACEHOLDER = _regex.compile(r"\{\{\s*(\w+)\s*\}\}")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def render_report(template_html: str, fragments: dict[str, str]) -> str:
|
|
15
|
+
"""
|
|
16
|
+
Render a template by substituting ``{{key}}`` placeholders.
|
|
17
|
+
|
|
18
|
+
Parameters
|
|
19
|
+
----------
|
|
20
|
+
template_html : str
|
|
21
|
+
Raw HTML template containing ``{{key}}`` placeholders.
|
|
22
|
+
fragments : dict[str, str]
|
|
23
|
+
Mapping of placeholder name to HTML/text fragment. Values are inserted
|
|
24
|
+
verbatim. Keys not present in the template are ignored.
|
|
25
|
+
|
|
26
|
+
Returns
|
|
27
|
+
-------
|
|
28
|
+
str
|
|
29
|
+
The rendered HTML. Any placeholder without a matching key is removed.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def _sub(match: _regex.Match) -> str:
|
|
33
|
+
key = match.group(1)
|
|
34
|
+
value = fragments.get(key)
|
|
35
|
+
return "" if value is None else str(value)
|
|
36
|
+
|
|
37
|
+
return _PLACEHOLDER.sub(_sub, template_html)
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Reusable report sections.
|
|
3
|
+
|
|
4
|
+
Each function returns an HTML fragment (string) ready to be injected into a
|
|
5
|
+
template by :func:`quantstats._reporting.engine.render_report`. The sections
|
|
6
|
+
reuse the low-level helpers already living in :mod:`quantstats.reports`
|
|
7
|
+
(``_html_table``, ``_embed_figure``, ``metrics``) so that the new tearsheets
|
|
8
|
+
stay visually consistent with the legacy one without duplicating logic.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import re as _re
|
|
14
|
+
|
|
15
|
+
import pandas as _pd
|
|
16
|
+
|
|
17
|
+
# Inline-styled cell used to turn a blank spacer row into a subtle gray rule.
|
|
18
|
+
_SEP_CELL = '<td style="border-bottom:1px solid #cfcfcf;padding:0;height:1px"></td>'
|
|
19
|
+
_ROW_RE = _re.compile(r"<tr>.*?</tr>", _re.S)
|
|
20
|
+
_CELL_RE = _re.compile(r"<td[^>]*>(.*?)</td>", _re.S)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _style_separator_rows(html: str) -> str:
|
|
24
|
+
"""Render blank spacer rows (all-empty cells) as a subtle gray separator."""
|
|
25
|
+
|
|
26
|
+
def _repl(match: _re.Match) -> str:
|
|
27
|
+
row = match.group(0)
|
|
28
|
+
cells = _CELL_RE.findall(row)
|
|
29
|
+
if cells and all(c.strip() == "" for c in cells):
|
|
30
|
+
return "<tr>" + _SEP_CELL * len(cells) + "</tr>"
|
|
31
|
+
return row
|
|
32
|
+
|
|
33
|
+
return _ROW_RE.sub(_repl, html)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _reports():
|
|
37
|
+
from .. import reports
|
|
38
|
+
|
|
39
|
+
return reports
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _utils():
|
|
43
|
+
from .. import utils
|
|
44
|
+
|
|
45
|
+
return utils
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _stats():
|
|
49
|
+
from .. import stats
|
|
50
|
+
|
|
51
|
+
return stats
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _plots():
|
|
55
|
+
from .. import plots
|
|
56
|
+
|
|
57
|
+
return plots
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _normalize_label(label: str) -> str:
|
|
61
|
+
"""Normalize a metric label for robust matching (drop ``%`` and spaces)."""
|
|
62
|
+
return str(label).replace("%", "").strip().lower()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def metrics_table(
|
|
66
|
+
returns,
|
|
67
|
+
benchmark=None,
|
|
68
|
+
rf: float = 0.0,
|
|
69
|
+
compounded: bool = True,
|
|
70
|
+
periods_per_year: int = 252,
|
|
71
|
+
drop_rows: list[str] | None = None,
|
|
72
|
+
collapse_separators: bool = False,
|
|
73
|
+
style_separators: bool = False,
|
|
74
|
+
strategy_title="Strategy",
|
|
75
|
+
benchmark_title=None,
|
|
76
|
+
prepare_returns: bool = True,
|
|
77
|
+
) -> str:
|
|
78
|
+
"""
|
|
79
|
+
Render the key-performance-metrics table as HTML.
|
|
80
|
+
|
|
81
|
+
Mirrors the legacy ``reports.html`` metrics table, with the ability to drop
|
|
82
|
+
specific rows by (normalized) label. When ``collapse_separators`` is True,
|
|
83
|
+
runs of consecutive blank spacer rows (empty index) are collapsed to a
|
|
84
|
+
single one and leading/trailing blanks are removed -- this keeps the visual
|
|
85
|
+
grouping while avoiding the large gaps left after whole metric groups are
|
|
86
|
+
dropped. When ``style_separators`` is True, the remaining blank spacer rows
|
|
87
|
+
are rendered as a subtle gray horizontal rule instead of empty whitespace.
|
|
88
|
+
"""
|
|
89
|
+
reports = _reports()
|
|
90
|
+
mtrx = reports.metrics(
|
|
91
|
+
returns=returns,
|
|
92
|
+
benchmark=benchmark,
|
|
93
|
+
rf=rf,
|
|
94
|
+
display=False,
|
|
95
|
+
mode="full",
|
|
96
|
+
sep=True,
|
|
97
|
+
internal="True",
|
|
98
|
+
compounded=compounded,
|
|
99
|
+
periods_per_year=periods_per_year,
|
|
100
|
+
prepare_returns=prepare_returns,
|
|
101
|
+
benchmark_title=benchmark_title,
|
|
102
|
+
strategy_title=strategy_title,
|
|
103
|
+
)[2:]
|
|
104
|
+
|
|
105
|
+
if drop_rows:
|
|
106
|
+
drop_norm = {_normalize_label(d) for d in drop_rows}
|
|
107
|
+
keep = [idx for idx in mtrx.index if _normalize_label(idx) not in drop_norm]
|
|
108
|
+
mtrx = mtrx.loc[keep]
|
|
109
|
+
|
|
110
|
+
if collapse_separators:
|
|
111
|
+
is_blank = [str(idx).strip() == "" for idx in mtrx.index]
|
|
112
|
+
keep_mask = []
|
|
113
|
+
prev_blank = True # drop leading blanks
|
|
114
|
+
for blank in is_blank:
|
|
115
|
+
keep_mask.append(not (blank and prev_blank))
|
|
116
|
+
prev_blank = blank
|
|
117
|
+
# Drop trailing blanks too.
|
|
118
|
+
for i in range(len(keep_mask) - 1, -1, -1):
|
|
119
|
+
if is_blank[i] and keep_mask[i]:
|
|
120
|
+
keep_mask[i] = False
|
|
121
|
+
elif keep_mask[i]:
|
|
122
|
+
break
|
|
123
|
+
mtrx = mtrx[keep_mask]
|
|
124
|
+
|
|
125
|
+
mtrx.index.name = "Metric"
|
|
126
|
+
html = reports._html_table(mtrx)
|
|
127
|
+
if style_separators:
|
|
128
|
+
html = _style_separator_rows(html)
|
|
129
|
+
return html
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def eoy_table(
|
|
133
|
+
returns,
|
|
134
|
+
benchmark=None,
|
|
135
|
+
benchmark_original=None,
|
|
136
|
+
compounded: bool = True,
|
|
137
|
+
strategy_title="Strategy",
|
|
138
|
+
benchmark_title=None,
|
|
139
|
+
) -> str:
|
|
140
|
+
"""Render the end-of-year returns table as HTML."""
|
|
141
|
+
reports = _reports()
|
|
142
|
+
utils = _utils()
|
|
143
|
+
stats = _stats()
|
|
144
|
+
|
|
145
|
+
if benchmark is not None:
|
|
146
|
+
benchmark_for_eoy = (
|
|
147
|
+
benchmark_original if benchmark_original is not None else benchmark
|
|
148
|
+
)
|
|
149
|
+
yoy = stats.compare(
|
|
150
|
+
returns, benchmark_for_eoy, "YE", compounded=compounded, prepare_returns=False
|
|
151
|
+
)
|
|
152
|
+
if isinstance(returns, _pd.Series):
|
|
153
|
+
yoy.columns = [benchmark_title, strategy_title, "Multiplier", "Won"]
|
|
154
|
+
yoy.index.name = "Year"
|
|
155
|
+
return reports._html_table(yoy)
|
|
156
|
+
|
|
157
|
+
yoy = _pd.DataFrame(utils.group_returns(returns, returns.index.year) * 100)
|
|
158
|
+
if isinstance(returns, _pd.Series):
|
|
159
|
+
yoy.columns = ["Return"]
|
|
160
|
+
yoy["Cumulative"] = utils.group_returns(returns, returns.index.year, True) * 100
|
|
161
|
+
yoy.index.name = "Year"
|
|
162
|
+
return reports._html_table(yoy)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def dd_table(returns, top: int = 5) -> str:
|
|
166
|
+
"""Render the worst-N drawdowns table as HTML (single Series)."""
|
|
167
|
+
reports = _reports()
|
|
168
|
+
stats = _stats()
|
|
169
|
+
|
|
170
|
+
dd = stats.to_drawdown_series(returns)
|
|
171
|
+
dd_info = stats.drawdown_details(dd).sort_values(by="max drawdown", ascending=True)[
|
|
172
|
+
:top
|
|
173
|
+
]
|
|
174
|
+
dd_info = dd_info[["start", "end", "max drawdown", "days"]]
|
|
175
|
+
dd_info.columns = ["Started", "Recovered", "Drawdown", "Days"]
|
|
176
|
+
return reports._html_table(dd_info, False)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def plot_section(
|
|
180
|
+
plot_name: str,
|
|
181
|
+
returns,
|
|
182
|
+
benchmark=None,
|
|
183
|
+
figfmt: str = "svg",
|
|
184
|
+
**plot_kwargs,
|
|
185
|
+
) -> str:
|
|
186
|
+
"""
|
|
187
|
+
Generate a single plot and return it as an embedded HTML fragment.
|
|
188
|
+
|
|
189
|
+
Parameters
|
|
190
|
+
----------
|
|
191
|
+
plot_name : str
|
|
192
|
+
Name of the plotting function in :mod:`quantstats.plots`.
|
|
193
|
+
returns : pd.Series or pd.DataFrame
|
|
194
|
+
Returns data passed as the first positional argument.
|
|
195
|
+
benchmark : optional
|
|
196
|
+
Passed as second positional argument when not None; some plot
|
|
197
|
+
functions (e.g. rolling_sharpe) do not accept a benchmark, so pass
|
|
198
|
+
``benchmark=None`` and they will be called with returns only.
|
|
199
|
+
figfmt : str
|
|
200
|
+
Figure format for embedding (``svg`` by default).
|
|
201
|
+
**plot_kwargs
|
|
202
|
+
Extra keyword arguments forwarded to the plotting function.
|
|
203
|
+
"""
|
|
204
|
+
reports = _reports()
|
|
205
|
+
utils = _utils()
|
|
206
|
+
plots = _plots()
|
|
207
|
+
|
|
208
|
+
plot_fn = getattr(plots, plot_name)
|
|
209
|
+
figfile = utils._file_stream()
|
|
210
|
+
|
|
211
|
+
kwargs = dict(
|
|
212
|
+
grayscale=plot_kwargs.pop("grayscale", False),
|
|
213
|
+
savefig={"fname": figfile, "format": figfmt},
|
|
214
|
+
show=False,
|
|
215
|
+
)
|
|
216
|
+
kwargs.update(plot_kwargs)
|
|
217
|
+
|
|
218
|
+
if benchmark is not None:
|
|
219
|
+
plot_fn(returns, benchmark, **kwargs)
|
|
220
|
+
else:
|
|
221
|
+
plot_fn(returns, **kwargs)
|
|
222
|
+
|
|
223
|
+
return reports._embed_figure(figfile, figfmt)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Alpha decay monitoring for QuantStats Pro.
|
|
3
|
+
|
|
4
|
+
Rolling-window risk metrics, historical distributions on an analysis scale
|
|
5
|
+
(log / log-severity transforms), z-score traffic lights, CUSUM change
|
|
6
|
+
detection, and time-underwater analysis for equity curves.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from . import plotting
|
|
10
|
+
from .core import (
|
|
11
|
+
DecayResult,
|
|
12
|
+
MetricResult,
|
|
13
|
+
WindowResult,
|
|
14
|
+
analyze,
|
|
15
|
+
available_metrics,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"DecayResult",
|
|
20
|
+
"MetricResult",
|
|
21
|
+
"WindowResult",
|
|
22
|
+
"analyze",
|
|
23
|
+
"available_metrics",
|
|
24
|
+
"plotting",
|
|
25
|
+
]
|