Skip to content

contango.trading.analysis.graphing.risk_return_overview

contango.trading.analysis.graphing.risk_return_overview

RiskReturnOverview

RiskReturnOverview(config: C)

Bases: GraphType[RiskReturnOverviewConfig]

A graph type that shows the risk/return overview scatter plot.

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 the risk/return overview scatter plot.

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\risk_return_overview.py
30
31
32
33
34
35
36
37
38
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
def build(self, df: pd.DataFrame) -> go.Figure:
    """
    Builds the risk/return overview scatter plot.

    Args:
        df: The experiment DataFrame.

    Returns:
        A plotly Figure.
    """
    plot_df = df.dropna(subset=["annual_return", "sharpe_ratio", "max_drawdown", self.config.color_by]).copy()
    plot_df["drawdown_severity"] = plot_df["max_drawdown"].abs()

    is_numeric_color = pd.api.types.is_numeric_dtype(plot_df[self.config.color_by])

    fig = px.scatter(   # type: ignore[unknownMemberType]
        plot_df,
        x="annual_return",
        y="sharpe_ratio",
        size="drawdown_severity",
        color=self.config.color_by,
        hover_name="name",
        hover_data={
            "max_drawdown": ":.2%",
            "calmar_ratio": ":.2f",
            "trade_count": True,
            "drawdown_severity": False,
        },
        color_continuous_scale="Viridis" if is_numeric_color else None,
        title="Risk return overview: annual return vs. sharpe ratio, bubble size = drawdown severity",
        labels={"annual_return": "Annual Return (CAGR)", "sharpe_ratio": "Sharpe Ratio"},
    )
    fig.update_layout(xaxis_tickformat=".1%")  # type: ignore[unknownMemberType]
    return fig

RiskReturnOverviewConfig dataclass

RiskReturnOverviewConfig(color_by: str = 'calmar_ratio')

Bases: GraphConfig

A graph configuration specific to the risk/return overview graph.

Attributes:

Name Type Description
color_by str

The column to color points by.