plot_pnl
function backtide.analysis.pnl.plot_pnl(runs, normalize=False, drawdown=True, title=None, legend="upper left", figsize=(900, 600), filename=None, display=True)[source]
Create a PnL-over-time chart for one or more strategy runs.
Each line tracks a strategy's running profit & loss (current equity minus
the starting equity) in the base currency. When normalize=True, PnL is
normalized to a percentage of the starting equity instead, which makes
strategies with different initial cash visually comparable. When
drawdown=True (the default), a second panel is rendered below the
PnL curve showing each strategy's running drawdown on a shared x-axis.
The benchmark run is shown as a dashed gray line, if provided.
| Parameters |
runs : RunResult | list[RunResult]
The per-strategy results to plot, typically obtained from
normalize : bool, default=Falsequery_strategy_runs or directly from ExperimentResult.
Whether to render a drawdown panel underneath the PnL curve. When
True, the figure has two stacked rows (PnL on top, drawdown below)
sharing the same x-axis. Set to False for a single-panel chart.
title : str | dict | None, default=None
Title for the plot.
legend : str | dict | None, default="upper left"
Legend for the plot. See the user guide for an extended
description of the choices.
figsize : tuple[int, int], default=(900, 600)
Figure's size in pixels, format as (x, y).
filename : str | Path | None, default=None
Save the plot using this name. The type of the file depends on the
provided name (
display : bool | None, default=True.html, .png, .pdf, etc...). If filename has no
file type, the plot is saved as .html. If None, the plot isn't saved.
Whether to render the plot. If
None, it returns the figure.
|
| Returns |
Figure | None
The Plotly figure object. Only returned if
display=None.
|
See Also
Create a histogram of per-trade PnL for one or more strategy runs.
Create a rolling-return chart for one or more strategy runs.
Create a per-trade PnL over time plot for one or more strategy runs.
Example
>>> from backtide.analysis import plot_pnl
>>> from backtide.storage import query_strategy_runs, query_experiments
>>> exp = query_experiments().iloc[0]
>>> runs = query_strategy_runs(exp.id)
>>> # Absolute PnL
>>> plot_pnl(runs)
>>> # Normalized values
>>> plot_pnl(runs, normalize=True)