Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(shadow-account): exclude already explained trades from overtrading pnl

Lanre Shittud6a13530fb58
diff --git a/agent/src/shadow_account/backtester.py b/agent/src/shadow_account/backtester.py
index 3203c2402..f2b63ba01 100644
--- a/agent/src/shadow_account/backtester.py
+++ b/agent/src/shadow_account/backtester.py
@@ -653,6 +653,7 @@ def _compute_attribution(
noise = 0.0
early = 0.0
late = 0.0
+ already_explained: set[int] = set()
excluded_currencies: dict[str, int] = {}
pool_roundtrips = [
rt
@@ -695,6 +696,7 @@ def _compute_attribution(
impact += -pnl
reason = "rule_violation"
if impact != 0.0:
+ already_explained.add(id(rt))
counterfactuals.append({
"symbol": rt["symbol"],
"buy_dt": str(rt["buy_dt"]),
@@ -705,7 +707,9 @@ def _compute_attribution(
"reason": reason,
})
- overtrading = _overtrading_pnl(profile=profile, roundtrips=pool_roundtrips)
+ overtrading = _overtrading_pnl(
+ profile=profile, roundtrips=pool_roundtrips, already_explained=already_explained
+ )
explained = noise + early + late + overtrading
missed = round(shadow_pnl - real_pnl - explained, 2)
@@ -740,6 +744,7 @@ def _overtrading_pnl(
*,
profile: ShadowProfile,
roundtrips: list[dict[str, Any]],
+ already_explained: set[int] = frozenset(),
) -> float:
"""Excess-frequency PnL: trades beyond the shadow's expected budget.
@@ -747,6 +752,12 @@ def _overtrading_pnl(
compare against the user's actual roundtrip count over the same span.
Excess trades' PnL is totaled with a negative sign (shadow would've
skipped them, so real PnL — positive or negative — is "noise").
+
+ ``already_explained`` holds ``id()`` of roundtrips the caller already
+ booked into noise/early/late (#17's mutual-exclusivity invariant): those
+ trades' PnL already explains their gap from shadow behaviour, so they
+ are excluded from the "extra" candidates here to avoid double-counting
+ the same trade in two buckets.
"""
if not roundtrips:
return 0.0
@@ -761,7 +772,8 @@ def _overtrading_pnl(
if actual <= expected:
return 0.0
# Penalize the cheapest (lowest |pnl|) extras — those look like noise.
- extras = sorted(roundtrips, key=lambda rt: abs(float(rt["pnl"])))
+ candidates = [rt for rt in roundtrips if id(rt) not in already_explained]
+ extras = sorted(candidates, key=lambda rt: abs(float(rt["pnl"])))
extra_count = int(actual - expected)
extra_pnl = sum(float(rt["pnl"]) for rt in extras[:extra_count])
return -extra_pnl
diff --git a/agent/tests/test_shadow_attribution.py b/agent/tests/test_shadow_attribution.py
index a556dbac8..92d640d02 100644
--- a/agent/tests/test_shadow_attribution.py
+++ b/agent/tests/test_shadow_attribution.py
@@ -103,3 +103,26 @@ def test_short_loser_is_noise_only() -> None:
assert breakdown.noise_trades_pnl == pytest.approx(30.0)
assert breakdown.early_exit_pnl == 0.0
assert breakdown.late_exit_pnl == 0.0
+
+
+def test_early_exit_trade_not_also_counted_as_overtrading() -> None:
+ # trade 1 is an early-exit winner (hold=1d < the 3d rule): its pnl is
+ # already fully explained by early_exit_pnl. Trades 2-4 hold exactly the
+ # rule's 3 days, so with expected == 1.0 (low budget) and actual == 4,
+ # _overtrading_pnl must draw its "extra" candidates only from trades not
+ # already booked into early_exit_pnl.
+ roundtrips = [
+ _rt("AAPL.US", 30.0, 1.0),
+ _rt("AAPL.US", 40.0, 3.0),
+ _rt("AAPL.US", 50.0, 3.0),
+ _rt("AAPL.US", 60.0, 3.0),
+ ]
+ breakdown, _, _ = _compute_attribution(
+ profile=_profile(), roundtrips=roundtrips, shadow_pnl=0.0,
+ )
+ assert breakdown.early_exit_pnl == pytest.approx(30.0 * (3 - 1) / 3)
+ # Trade 1's pnl (30.0) must not also be folded into overtrading_pnl: the
+ # buggy version summed the three cheapest of ALL four trades (30+40+50 =
+ # 120), double-counting trade 1; the fix draws from the three trades not
+ # already explained (40+50+60 = 150).
+ assert breakdown.overtrading_pnl == pytest.approx(-150.0)