Visualization¶
fincore provides pluggable visualization backends via the VizBackend protocol.
Built-in Backends¶
Matplotlib¶
from fincore.viz import get_backend
viz = get_backend("matplotlib")
viz.plot_returns(cum_returns)
viz.plot_drawdown(drawdown)
viz.plot_rolling_sharpe(rolling_sharpe)
viz.plot_monthly_heatmap(returns)
HTML (no extra dependencies)¶
from fincore.viz.html_backend import HtmlReportBuilder
builder = HtmlReportBuilder()
builder.add_title("My Report")
builder.add_stats_table(stats)
builder.save("report.html")
Via AnalysisContext¶
ctx = fincore.analyze(returns, factor_returns=benchmark)
ctx.plot(backend="matplotlib")
ctx.to_html(path="report.html")
API Reference¶
fincore.viz.base.VizBackend
¶
Bases: Protocol
Protocol that every visualization backend must implement.
plot_returns(cum_returns, **kwargs)
¶
Plot cumulative returns.
plot_drawdown(drawdown, **kwargs)
¶
Plot drawdown underwater chart.
plot_rolling_sharpe(sharpe, benchmark_sharpe=None, window=252, **kwargs)
¶
Plot rolling Sharpe ratio.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
sharpe
|
Series
|
Rolling Sharpe ratio series. |
必需 |
benchmark_sharpe
|
Series
|
Optional benchmark rolling Sharpe series. |
None
|
window
|
int
|
Window size (used for title/annotation). |
252
|
plot_monthly_heatmap(returns, **kwargs)
¶
Plot monthly returns heatmap.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
returns
|
Series or DataFrame
|
Either a daily returns series (will be aggregated internally) or a year x month table of monthly returns. |
必需 |
fincore.viz.html_backend.HtmlReportBuilder()
¶
Build a standalone HTML performance report.
This class also satisfies the :class:~fincore.viz.base.VizBackend
protocol (the plot_* methods append HTML sections and return
self for chaining).
Usage::
from fincore.viz.html_backend import HtmlReportBuilder
builder = HtmlReportBuilder()
builder.add_title("Performance Report")
builder.add_stats_table(perf_stats_series)
html_str = builder.build()
add_title(title='Performance Report')
¶
Append an <h1> title section to the report.
add_heading(text, level=2)
¶
Append a heading (<h2> … <h6>) section to the report.
add_stats_table(stats)
¶
Render a two-column key/value table from a pandas Series.
add_metric_cards(stats, keys=None)
¶
Render inline metric cards for selected keys from stats.
add_html(raw_html)
¶
Append raw HTML directly to the report body.
plot_returns(cum_returns, **kwargs)
¶
Append a cumulative-returns table section (VizBackend protocol).
plot_drawdown(drawdown, **kwargs)
¶
Append a drawdown table section (VizBackend protocol).
plot_rolling_sharpe(sharpe, benchmark_sharpe=None, window=APPROX_BDAYS_PER_YEAR, **kwargs)
¶
Append a rolling Sharpe ratio table section (VizBackend protocol).
plot_monthly_heatmap(returns, **kwargs)
¶
Append a monthly-returns pivot table section (VizBackend protocol).
build()
¶
Assemble all sections into a complete HTML document string.
save(path)
¶
Write the report to a file at path.