Implementation:HKUDS AI Trader Plot Market Metrics
| Knowledge Sources | |
|---|---|
| Domains | Data_Visualization, Performance_Analysis |
| Last Updated | 2026-02-09 14:00 GMT |
Overview
Concrete tool for generating multi-agent comparison charts with four key performance metrics using matplotlib.
Description
The plot_market_metrics function creates a figure with 4 horizontal subplots (CR, SR, Vol, MDD) at size 24x5 inches. It plots each agent's rolling metrics as a colored line and optionally overlays a market benchmark as a dashed line. The output is saved as a PDF file.
The companion calculate_rolling_metrics function computes the expanding-window metrics (CR, SR, Vol, MDD) from a portfolio values DataFrame, which are then passed to the plotting function.
Usage
Call calculate_rolling_metrics() on each agent's portfolio DataFrame, then pass the results to plot_market_metrics() for visualization. Used in the multi-agent comparison workflow.
Code Reference
Source Location
- Repository: AI-Trader
- File: tools/plot_metrics.py
- Lines: L258-306 (plot_market_metrics), L52-128 (calculate_rolling_metrics)
Signature
def plot_market_metrics(agent_data, baseline_data, market_name, output_file, is_hourly=True):
"""
Generate multi-agent comparison chart with 4 metric subplots.
Args:
agent_data: Dict mapping agent name to DataFrame with rolling metrics
baseline_data: Optional DataFrame with benchmark rolling metrics
market_name: Display name for the chart title
output_file: Path for PDF output
is_hourly: Whether data is hourly (affects period labeling)
Returns:
None. Saves PDF to output_file.
"""
def calculate_rolling_metrics(df, is_hourly=True):
"""
Compute expanding-window rolling metrics from portfolio values.
Args:
df: DataFrame with 'total_value' column
is_hourly: If True, uses 252*6.5 periods_per_year; else 252
Returns:
df with added columns: returns, CR, SR, Vol, MDD
"""
Import
from tools.plot_metrics import plot_market_metrics, calculate_rolling_metrics
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| agent_data | Dict[str, pd.DataFrame] | Yes | Agent name to rolling metrics DataFrame |
| baseline_data | pd.DataFrame or None | No | Benchmark rolling metrics |
| market_name | str | Yes | Display name for chart title |
| output_file | Path | Yes | PDF output file path |
| is_hourly | bool | No | Hourly vs daily data flag (default True) |
Outputs
| Name | Type | Description |
|---|---|---|
| PDF file | File | Multi-subplot comparison chart saved to output_file |
Usage Examples
Generate Comparison Chart
from tools.plot_metrics import plot_market_metrics, calculate_rolling_metrics
from pathlib import Path
# Prepare agent data
agent_data = {}
for agent_name in ["gpt-4o", "claude-3.5", "deepseek-v3"]:
df = load_portfolio_values(agent_name)
df = calculate_rolling_metrics(df, is_hourly=True)
agent_data[agent_name] = df
# Optional benchmark
baseline_df = load_benchmark_values("QQQ")
baseline_df = calculate_rolling_metrics(baseline_df, is_hourly=True)
# Plot
plot_market_metrics(
agent_data=agent_data,
baseline_data=baseline_df,
market_name="US Market (Hourly)",
output_file=Path("us_market_metrics.pdf"),
is_hourly=True
)