Coverage for src / jquantstats / _reports / _portfolio.py: 100%
102 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-26 18:44 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-26 18:44 +0000
1"""HTML report generation for portfolio analytics.
3This module defines the Report facade which produces a self-contained HTML
4document containing all relevant performance numbers and interactive Plotly
5visualisations for a Portfolio.
7Examples:
8 >>> import dataclasses
9 >>> from jquantstats._reports import Report
10 >>> dataclasses.is_dataclass(Report)
11 True
12"""
14from __future__ import annotations
16import dataclasses
17import math
18from pathlib import Path
19from typing import TYPE_CHECKING, TypeGuard
21import plotly.graph_objects as go
22import plotly.io as pio
23import polars as pl
24from jinja2 import Environment, FileSystemLoader, select_autoescape
26if TYPE_CHECKING:
27 from ._protocol import PortfolioLike
29# templates/ lives one level above this subpackage (at src/jquantstats/templates/)
30_TEMPLATES_DIR = Path(__file__).parent.parent / "templates"
31_env = Environment(
32 loader=FileSystemLoader(_TEMPLATES_DIR),
33 autoescape=select_autoescape(["html"]),
34)
37# ── Formatting helpers ────────────────────────────────────────────────────────
40def _is_finite(v: object) -> TypeGuard[int | float]:
41 """Return True when *v* is a real, finite number."""
42 if not isinstance(v, (int, float)):
43 return False
44 return math.isfinite(float(v))
47def _fmt(value: object, fmt: str = ".4f", suffix: str = "") -> str:
48 """Format *value* for display in an HTML table cell.
50 Returns ``"N/A"`` for ``None``, ``NaN``, or non-finite values.
51 """
52 if not _is_finite(value):
53 return "N/A"
54 return f"{float(value):{fmt}}{suffix}"
57# ── Stats table ───────────────────────────────────────────────────────────────
59_METRIC_FORMATS: dict[str, tuple[str, str]] = {
60 "avg_return": (".6f", ""),
61 "avg_win": (".6f", ""),
62 "avg_loss": (".6f", ""),
63 "best": (".6f", ""),
64 "worst": (".6f", ""),
65 "sharpe": (".2f", ""),
66 "calmar": (".2f", ""),
67 "recovery_factor": (".2f", ""),
68 "max_drawdown": (".2%", ""),
69 "avg_drawdown": (".2%", ""),
70 "max_drawdown_duration": (".0f", " days"),
71 "win_rate": (".1%", ""),
72 "monthly_win_rate": (".1%", ""),
73 "profit_factor": (".2f", ""),
74 "payoff_ratio": (".2f", ""),
75 "volatility": (".2%", ""),
76 "skew": (".2f", ""),
77 "kurtosis": (".2f", ""),
78 "value_at_risk": (".6f", ""),
79 "conditional_value_at_risk": (".6f", ""),
80}
82_METRIC_LABELS: dict[str, str] = {
83 "avg_return": "Avg Return",
84 "avg_win": "Avg Win",
85 "avg_loss": "Avg Loss",
86 "best": "Best Period",
87 "worst": "Worst Period",
88 "sharpe": "Sharpe Ratio",
89 "calmar": "Calmar Ratio",
90 "recovery_factor": "Recovery Factor",
91 "max_drawdown": "Max Drawdown",
92 "avg_drawdown": "Avg Drawdown",
93 "max_drawdown_duration": "Max DD Duration",
94 "win_rate": "Win Rate",
95 "monthly_win_rate": "Monthly Win Rate",
96 "profit_factor": "Profit Factor",
97 "payoff_ratio": "Payoff Ratio",
98 "volatility": "Volatility (ann.)",
99 "skew": "Skewness",
100 "kurtosis": "Kurtosis",
101 "value_at_risk": "VaR (95 %)",
102 "conditional_value_at_risk": "CVaR (95 %)",
103}
105# Metrics where the *highest* value across assets is highlighted.
106_HIGHER_IS_BETTER: frozenset[str] = frozenset(
107 {"sharpe", "calmar", "recovery_factor", "win_rate", "monthly_win_rate", "profit_factor", "payoff_ratio"}
108)
110_CATEGORIES: list[tuple[str, list[str]]] = [
111 ("Returns", ["avg_return", "avg_win", "avg_loss", "best", "worst"]),
112 ("Risk-Adjusted Performance", ["sharpe", "calmar", "recovery_factor"]),
113 ("Drawdown", ["max_drawdown", "avg_drawdown", "max_drawdown_duration"]),
114 ("Win / Loss", ["win_rate", "monthly_win_rate", "profit_factor", "payoff_ratio"]),
115 ("Distribution & Risk", ["volatility", "skew", "kurtosis", "value_at_risk", "conditional_value_at_risk"]),
116]
119def _stats_table_html(summary: pl.DataFrame) -> str:
120 """Render a stats summary DataFrame as a styled HTML table.
122 Args:
123 summary: Output of :py:meth:`Stats.summary` — one row per metric,
124 one column per asset plus a ``metric`` column.
126 Returns:
127 An HTML ``<table>`` string ready to embed in a page.
128 """
129 assets = [c for c in summary.columns if c != "metric"]
131 # Build a fast lookup: metric_name → {asset: value}
132 metric_data: dict[str, dict[str, object]] = {}
133 for row in summary.iter_rows(named=True):
134 name = str(row["metric"])
135 metric_data[name] = {a: row.get(a) for a in assets}
137 header_cells = "".join(f'<th class="asset-header">{a}</th>' for a in assets)
138 rows_html_parts: list[str] = []
140 for category_label, metrics in _CATEGORIES:
141 rows_html_parts.append(
142 f'<tr class="table-section-header">'
143 f'<td colspan="{len(assets) + 1}"><strong>{category_label}</strong></td>'
144 f"</tr>\n"
145 )
146 for metric in metrics:
147 if metric not in metric_data:
148 continue
149 fmt, suffix = _METRIC_FORMATS.get(metric, (".4f", ""))
150 label = _METRIC_LABELS.get(metric, metric.replace("_", " ").title())
151 values = metric_data[metric]
153 # Find the best asset to highlight (only for higher-is-better metrics)
154 best_asset: str | None = None
155 if metric in _HIGHER_IS_BETTER:
156 finite_pairs = [(a, float(v)) for a, v in values.items() if _is_finite(v)]
157 if finite_pairs:
158 best_asset = max(finite_pairs, key=lambda x: x[1])[0]
160 cells = "".join(
161 f'<td class="metric-value{" best-value" if a == best_asset else ""}">'
162 f"{_fmt(values.get(a), fmt, suffix)}</td>"
163 for a in assets
164 )
165 rows_html_parts.append(f'<tr><td class="metric-name">{label}</td>{cells}</tr>\n')
167 rows_html = "".join(rows_html_parts)
168 return (
169 '<table class="stats-table">'
170 "<thead><tr>"
171 f'<th class="metric-header">Metric</th>{header_cells}'
172 "</tr></thead>"
173 f"<tbody>{rows_html}</tbody>"
174 "</table>"
175 )
178# ── Report dataclass ──────────────────────────────────────────────────────────
181def _figure_div(fig: go.Figure, include_plotlyjs: bool | str) -> str:
182 """Return an HTML div string for *fig*.
184 Args:
185 fig: Plotly figure to serialise.
186 include_plotlyjs: Passed directly to :func:`plotly.io.to_html`.
187 Pass ``"cdn"`` for the first figure so the CDN script tag is
188 injected; pass ``False`` for all subsequent figures.
190 Returns:
191 HTML string (not a full page).
192 """
193 return pio.to_html(
194 fig,
195 full_html=False,
196 include_plotlyjs=include_plotlyjs,
197 )
200@dataclasses.dataclass(frozen=True)
201class Report:
202 """Facade for generating HTML reports from a Portfolio.
204 Provides a :py:meth:`to_html` method that assembles a self-contained,
205 dark-themed HTML document with a performance-statistics table and
206 multiple interactive Plotly charts.
208 Usage::
210 report = portfolio.report
211 html_str = report.to_html()
212 report.save("output/report.html")
213 """
215 portfolio: PortfolioLike
217 def to_html(self, title: str = "JQuantStats Portfolio Report") -> str:
218 """Render a full HTML report as a string.
220 The document is self-contained: Plotly.js is loaded once from the
221 CDN and all charts are embedded as ``<div>`` elements. No external
222 CSS framework is required.
224 Args:
225 title: HTML ``<title>`` text and visible page heading.
227 Returns:
228 A complete HTML document as a :class:`str`.
229 """
230 pf = self.portfolio
232 # ── Metadata ──────────────────────────────────────────────────────────
233 has_date = "date" in pf.prices.columns
234 if has_date:
235 dates = pf.prices["date"]
236 start_date = str(dates.min())
237 end_date = str(dates.max())
238 n_periods = pf.prices.height
239 period_info = f"{start_date} → {end_date} | {n_periods:,} periods"
240 else:
241 start_date = ""
242 end_date = ""
243 period_info = f"{pf.prices.height:,} periods"
245 assets_list = ", ".join(pf.assets)
247 # ── Figures ───────────────────────────────────────────────────────────
248 # The first chart includes Plotly.js from CDN; subsequent ones reuse it.
249 _first = True
251 def _div(fig: go.Figure) -> str:
252 """Serialise *fig* to an HTML div, embedding Plotly.js only on the first call."""
253 nonlocal _first
254 include = "cdn" if _first else False
255 _first = False
256 return _figure_div(fig, include)
258 def _try_div(build_fig: object) -> str:
259 """Call *build_fig()* and return the chart div; on error return a notice."""
260 try:
261 fig = build_fig() # type: ignore[operator]
262 return _div(fig)
263 except Exception as exc:
264 return f'<p class="chart-unavailable">Chart unavailable: {exc}</p>'
266 snapshot_div = _try_div(pf.plots.snapshot)
267 rolling_sharpe_div = _try_div(pf.plots.rolling_sharpe_plot)
268 rolling_vol_div = _try_div(pf.plots.rolling_volatility_plot)
269 annual_sharpe_div = _try_div(pf.plots.annual_sharpe_plot)
270 monthly_heatmap_div = _try_div(pf.plots.monthly_returns_heatmap)
271 corr_div = _try_div(pf.plots.correlation_heatmap)
272 lead_lag_div = _try_div(pf.plots.lead_lag_ir_plot)
273 trading_cost_div = _try_div(pf.plots.trading_cost_impact_plot)
275 # ── Stats table ───────────────────────────────────────────────────────
276 stats_table = _stats_table_html(pf.stats.summary())
278 # ── Turnover table ────────────────────────────────────────────────────
279 try:
280 turnover_df = pf.turnover_summary()
281 turnover_rows = "".join(
282 f'<tr><td class="metric-name">{row["metric"].replace("_", " ").title()}</td>'
283 f'<td class="metric-value">{row["value"]:.4f}</td></tr>'
284 for row in turnover_df.iter_rows(named=True)
285 )
286 turnover_html = (
287 '<table class="stats-table">'
288 "<thead><tr>"
289 '<th class="metric-header">Metric</th>'
290 '<th class="asset-header">Value</th>'
291 "</tr></thead>"
292 f"<tbody>{turnover_rows}</tbody>"
293 "</table>"
294 )
295 except Exception as exc:
296 turnover_html = f'<p class="chart-unavailable">Turnover data unavailable: {exc}</p>'
298 # ── Assemble HTML ─────────────────────────────────────────────────────
299 footer_date = end_date if has_date else ""
300 template = _env.get_template("portfolio_report.html")
301 return template.render(
302 title=title,
303 period_info=period_info,
304 assets_list=assets_list,
305 aum=f"{pf.aum:,.0f}",
306 footer_date=footer_date,
307 snapshot_div=snapshot_div,
308 rolling_sharpe_div=rolling_sharpe_div,
309 rolling_vol_div=rolling_vol_div,
310 annual_sharpe_div=annual_sharpe_div,
311 monthly_heatmap_div=monthly_heatmap_div,
312 corr_div=corr_div,
313 lead_lag_div=lead_lag_div,
314 trading_cost_div=trading_cost_div,
315 stats_table=stats_table,
316 turnover_html=turnover_html,
317 container_max_width="1400px",
318 )
320 def save(self, path: str | Path, title: str = "JQuantStats Portfolio Report") -> Path:
321 """Save the HTML report to a file.
323 A ``.html`` suffix is appended automatically when *path* has no
324 file extension.
326 Args:
327 path: Destination file path.
328 title: HTML ``<title>`` text and visible page heading.
330 Returns:
331 The resolved :class:`pathlib.Path` of the written file.
332 """
333 p = Path(path)
334 if not p.suffix:
335 p = p.with_suffix(".html")
336 p.parent.mkdir(parents=True, exist_ok=True)
337 p.write_text(self.to_html(title=title), encoding="utf-8")
338 return p