plot_trade_duration
function backtide.analysis.trade_duration.plot_trade_duration(runs, bins=None, unit="auto", title=None, legend="upper right", figsize=(900, 600), filename=None, display=True)[source]
Create a histogram of trade durations for one or more strategy runs.
Each strategy gets its own translucent histogram overlaid on the same axes — useful to compare how long each strategy holds positions.
| Parameters |
runs : RunResult | list[RunResult]
The per-strategy results to plot. Runs without trades are skipped.
bins : int | None, default=None
Number of histogram bins. If
unit : "auto" | "minutes" | "hours" | "days", default="auto"None, Plotly's default binning algorithm
is used.
Time unit used on the x-axis. When
title : str | dict | None, default=Noneauto, the unit is picked from the
median trade duration across all runs.
Title for the plot.
legend : str | dict | None, default="upper right"
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 per-trade PnL over time plot for one or more strategy runs.
A single round-trip trade (open + close of a position).
Example
>>> from backtide.analysis import plot_trade_duration
>>> from backtide.storage import query_experiments, query_strategy_runs
>>> exp = query_experiments().iloc[0]
>>> runs = query_strategy_runs(exp.id)
>>> plot_trade_duration(runs)