#289·AI-Trader

agent_metric_snapshots.return_pct is cumulative, not window relative

Author: LudwigJMarxCreated Sep 21, 2026Updated Sep 21, 2026

Summary

refresh_agent_metric_snapshots writes a row per agent with window_start_at and window_end_at, but return_pct is the agent's cumulative profit since account start divided by INITIAL_CAPITAL. max_drawdown in the same row is computed over the window. The experiment console averages return_pct per variant, so an A/B result carries profit earned before the experiment began.

Where

Line Code Scope
experiment_metrics.py:64 latest_profit = float(history[-1]["profit"] or 0) if history else 0.0 last row inside the window
experiment_metrics.py:65 return_pct = latest_profit / INITIAL_CAPITAL * 100.0 cumulative
experiment_metrics.py:66 max_drawdown = _max_drawdown_from_history(history) window relative
experiments.py:670 AVG(COALESCE(ls.return_pct, 0)) AS return_pct_avg consumer

profit is written by _record_profit_history_once as cash + position_value - (initial_capital + deposited) (tasks.py:800-803), so it is a level since account start, not a per period change.

Environment

Commit d03ff6c
Python 3.14.7
sqlite3 3.53.4
OS macOS 26.6.2, arm64
Backend SQLite (DB_PATH pointed at a scratch file)

Reproduction

Two agents, both up 40000 thirty days ago. Agent 1 does nothing in the last seven days. Agent 2 drops to 30000 inside those seven days.

python
rows = [
    (1, 40000.0, now - timedelta(days=30)),
    (1, 40000.0, now - timedelta(days=6)),
    (1, 40000.0, now - timedelta(days=1)),
    (2, 40000.0, now - timedelta(days=30)),
    (2, 40000.0, now - timedelta(days=6)),
    (2, 30000.0, now - timedelta(days=1)),
]
for agent_id, profit, when in rows:
    cursor.execute(
        "INSERT INTO profit_history (agent_id, total_value, cash, position_value, profit, recorded_at)"
        " VALUES (?, ?, ?, 0.0, ?, ?)",
        (agent_id, 100000.0 + profit, 100000.0 + profit, profit, iso(when)),
    )

refresh_agent_metric_snapshots(window_days=7)
agent  window  return_pct  max_drawdown
    1      7d     40.0000        0.0000
    2      7d     30.0000        7.1429

Inside the window agent 1 moved 0.0 and agent 2 moved -10000.0. The window relative return would be 0.00 % and -7.14 %. max_drawdown reflects the window. return_pct does not.

An agent with no profit_history row inside the window gets return_pct = 0.0 written (experiment_metrics.py:64), and experiments.py:670 folds a missing snapshot into the average as 0 through COALESCE. In both cases "not measured" and "flat" produce the same number.

Not verified

  • Measured on SQLite. The PostgreSQL branch of _record_profit_history_once uses the same raw_profit expression, but only SQLite was run.
  • The experiment console output was not rendered; the claim about the consumer rests on reading experiments.py:656-687.