Skip to content

contango.trading.analysis.graphing.equity_curve_overlay

contango.trading.analysis.graphing.equity_curve_overlay

EquityCurveConfig dataclass

EquityCurveConfig(target_metric: str, top_n: int, max_downsample_points: int = 1500)

Bases: GraphConfig

A graph configuration specific to the equity curve graph.

Attributes:

Name Type Description
target_metric str

The name for the targeted metric to graph.

top_n int

The top amount of values to keep for the graph.

max_downsample_points int

The maximum amount of downsample points to graph. Changing this value directly alters performance; however, it should usually not be changed for most uses.

EquityCurveOverlay

EquityCurveOverlay(config: EquityCurveConfig)

Bases: GraphType[EquityCurveConfig]

A graph type that shows the equity over time for a specified metric.

Source code in src\contango\trading\analysis\graphing\equity_curve_overlay.py
36
37
def __init__(self, config: EquityCurveConfig) -> None:
    super().__init__(config)

build

build(df: DataFrame) -> go.Figure

Builds an equity curve overlay for the top top_n 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\equity_curve_overlay.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def build(self, df: pd.DataFrame) -> go.Figure:
    """
    Builds an equity curve overlay for the top top_n experiments.

    Args:
        df: The experiment DataFrame.

    Returns:
        A plotly Figure.
    """
    plot_df = (
        df.dropna(subset=["equity_curve"])
        .sort_values(self.config.target_metric, ascending=False)
        .head(self.config.top_n)
        .reset_index(drop=True)
    )

    fig = go.Figure()
    for _, row in plot_df.iterrows():
        curve = downsample_curve(row["equity_curve"], self.config.max_downsample_points)
        timestamps: list[pd.Timestamp] = [pd.to_datetime(t, unit="ms") for t, _ in curve]
        values = [v for _, v in curve]
        fig.add_trace(  # type: ignore[unknownMemberType]
            go.Scatter(
                x=timestamps,
                y=values,
                mode="lines",
                name=f"{row['name']} ({self.config.target_metric}={row[self.config.target_metric]:.2f})",
            )
        )

    fig.update_layout(  # type: ignore[unknownMemberType]
        title=f"Equity Curve Overlay: top {len(plot_df)} by {self.config.target_metric}",
        xaxis_title="Time",
        yaxis_title="Account Value (USD)",
    )
    return fig