Skip to content

contango.trading.analysis.graphing.underwater_drawdown

contango.trading.analysis.graphing.underwater_drawdown

UnderwaterDrawdown

UnderwaterDrawdown(config: C)

Bases: GraphType[UnderwaterDrawdownConfig]

A graph type that shows drawdown-from-peak over time for a shortlist of experiments.

Source code in src\contango\trading\analysis\graph_type.py
26
27
28
29
30
31
32
33
def __init__(self, config: C) -> None:
    """
    Initializes `GraphType`.

    Args:
        config: The matching configuration for the graph type.
    """
    self._config = config

build

build(df: DataFrame) -> go.Figure

Builds an underwater plot (drawdown-from-peak over time) for a shortlist of experiments.

Parameters:

Name Type Description Default
df DataFrame

The experiment DataFrame.

required

Returns:

Type Description
Figure

A plotly Figure.

Source code in src\contango\trading\analysis\graphing\underwater_drawdown.py
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def build(self, df: pd.DataFrame) -> go.Figure:
    """
    Builds an underwater plot (drawdown-from-peak over time) for a shortlist
    of experiments.

    Args:
        df: The experiment DataFrame.

    Returns:
        A plotly Figure.
    """
    plot_df = df.dropna(subset=["equity_curve"]).copy()

    if self.config.experiment_ids is not None:
        plot_df = plot_df[plot_df["experiment_id"].isin(self.config.experiment_ids)]
    else:
        plot_df = (
            plot_df.sort_values(self.config.rank_by, ascending=False)
            .head(self.config.top_n)
            .reset_index(drop=True)
        )

    fig = go.Figure()
    for _, row in plot_df.iterrows():
        timestamps, drawdowns = _compute_underwater_series(row["equity_curve"], self.config.max_points_per_curve)
        fig.add_trace(  # type: ignore[unknownMemberType]
            go.Scatter(
                x=timestamps,
                y=drawdowns,
                mode="lines",
                name=row["name"],
                fill="tozeroy",
            )
        )

    fig.update_layout(  # type: ignore[unknownMemberType]
        title="Underwater plot: drawdown from peak over time",
        xaxis_title="Time",
        yaxis_title="Drawdown From Peak",
        yaxis_tickformat=".1%",
    )
    return fig

UnderwaterDrawdownConfig dataclass

UnderwaterDrawdownConfig(experiment_ids: list[str] | None = None, top_n: int = 8, rank_by: str = 'calmar_ratio', max_points_per_curve: int = 1500)

Bases: GraphConfig

A graph configuration specific to the underwater drawdown graph.

Attributes:

Name Type Description
experiment_ids list[str] | None

The list of experiment_id values to plot.

top_n int

The top amount of experiments to plot when auto-ranking.

rank_by str

The name for the targeted metric to rank by when auto-selecting the shortlist.

max_points_per_curve int

The maximum amount of points kept per curve after downsampling.