Skip to content

trading.analyzer.graphing.trade_quality_scatter

trading.analyzer.graphing.trade_quality_scatter

build_trade_quality_scatter

build_trade_quality_scatter(df: DataFrame, y_metric: str = 'profit_factor', color_by: str = 'calmar_ratio') -> go.Figure

Builds the trade-quality scatter plot.

Parameters:

Name Type Description Default
df DataFrame

Experiment DataFrame.

required
y_metric str

Metric for the y-axis. "profit_factor" (default) or "expectancy" are best.

'profit_factor'
color_by str

Column to color points by. Defaults to "calmar_ratio".

'calmar_ratio'

Returns:

Type Description
Figure

A plotly Figure.

Source code in src\contango\trading\analyzer\graphing\trade_quality_scatter.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def build_trade_quality_scatter(
    df: pd.DataFrame,
    y_metric: str = "profit_factor",
    color_by: str = "calmar_ratio",
) -> go.Figure:
    """
    Builds the trade-quality scatter plot.

    Args:
        df: Experiment DataFrame.
        y_metric: Metric for the y-axis. "profit_factor" (default) or "expectancy" are best.
        color_by: Column to color points by. Defaults to "calmar_ratio".

    Returns:
        A plotly Figure.
    """
    plot_df = df.dropna(subset=["win_rate", y_metric, "trade_count", color_by]).copy()

    fig = px.scatter(   # type: ignore[unknownMemberType]
        plot_df,
        x="win_rate",
        y=y_metric,
        size="trade_count",
        color=color_by,
        hover_name="experiment_id",
        title=f"Trade Quality — Win Rate vs. {y_metric} (bubble size = trade count)",
        labels={"win_rate": "Win Rate"},
    )
    fig.update_layout(xaxis_tickformat=".0%")  # type: ignore[unknownMemberType]
    return fig