How Do I Attribute PnL in a Trading Bot? Knowing Which Markets Make You Money
“Up 4% this month” is a vanity number. The question that manages money is which positions, signals, and behaviors produced it — and which ate it. Attribution is the observability layer (see the 8 components) that turns total PnL into a decision system: what to scale, what to fix, what to kill.
What is PnL attribution?
Decomposing realized and unrealized PnL into decision-sized units. The four cuts that matter: by market (your edge is rarely uniform — most bots make their money in a minority of markets and quietly donate in the rest), by signal (if you run multiple strategies, attribute per strategy, not blended), by timing (entry timing versus exit timing — did your entries have edge but your exits give it back?), and by cost (spread paid, fees, funding, slippage — the stack from fees explained, attributed per trade so you can see which costs actually bite).
The logging schema (build this first)
Per trade, log at minimum: market ID, strategy/signal ID, signal timestamp, order sent timestamp, fill timestamp, fill price, quoted price at send, size, side, fees, spread at send, funding (perps), exit timestamp, exit price, and outcome (resolution if held). That schema makes every cut above a group-by. Two fields beginners skip that pay for themselves: quoted-price-at-send (your slippage measurement — the gap between decision and execution, see the sanity routine) and signal-vs-fill latency (slow execution quietly converts edge into noise — see the rate-limit patterns).
The weekly attribution review
Twenty minutes, structured: 1. Market cut — top 3 and bottom 3 markets by PnL; kill or shrink chronic donors (markets where you’ve lost 4 weeks straight aren’t “variance,” they’re information — see the seven mistakes). 2. Signal cut — per-strategy PnL after costs; a signal that only works blended with others is a liability in disguise. 3. Cost cut — which cost line is biggest? Funding dominating means timing and side discipline (funding data reads); spread dominating means book quality (order book guide). 4. Timing cut — compare average entry-edge-at-signal versus realized: a shrinking gap is execution decay; a negative gap is the bot fighting itself.
Decision rules (so the review changes something)
- Scale rule: add size to signals with 6+ weeks of positive after-cost attribution — via fractional Kelly sizing, not emotion.
- Kill rule: a signal with 4 consecutive negative weeks gets demoted to paper (see paper trading) until it re-proves.
- Fix rule: execution decay (rising latency-slippage) gets infrastructure fixes, not strategy changes — you’re debugging, not re-thinking (see the kill switch drill for the same maintenance discipline).
FAQ
What is PnL attribution in trading? Decomposing total profit-and-loss into units you can act on: by market, by signal, by timing, and by cost line. It converts “how much” into “what to scale and what to kill” — the observability layer of a production bot.
Why is my bot profitable overall but losing in some markets? Edge is rarely uniform — most strategies earn in a minority of markets and donate in the rest. Attribution by market finds the donors; the kill rule (4 negative weeks → demote to paper) stops the bleed — see the architecture.
How do I measure slippage in a bot? Log the quoted price at order-send and compare to your actual fill — the gap is your execution cost per trade. Trend it weekly; rising slippage is infrastructure decay, not bad luck.
Should I attribute PnL per strategy or per market? Both — they answer different questions. Per-strategy tells you what to scale; per-market tells you where your edge lives. Costs get attributed per trade so both cuts are after-cost and honest.
How often should I review bot performance? Weekly, structured, with decision rules applied — the review that doesn’t change sizing is a diary, not management. See the four-cut review above.
Educational content, not financial advice. Not affiliated with Polymarket. Last updated October 2026.