In lesson 1 you built the MDP scaffolding: states, actions, transitions, a policy, the discounted return, and the discount factor γ. All of it hangs off one tiny number you have to invent yourself — the reward . The agent does exactly one thing for a living: it makes the expected discounted sum of those numbers as large as it possibly can. It does not know what you meant. It knows what you wrote.
That gap — between the goal in your head (“make money, but don’t blow up”) and the scalar in your code — is where most applied-RL trading projects quietly die. Nobody ships a press release saying “our reward function had a sign error and the bot learned to churn.” They just see a backtest that looks too good, deploy it, and watch it light money on fire in production. Reward design is the single highest-leverage, highest-danger decision in the whole pipeline. Let’s treat it that way.
Before you read — take a guess
You set the reward to raw per-step PnL — dollars made this step, with no risk term and no cost term, in a simulator with zero fees. What does a competent RL agent learn to do?
Turning intent into a scalar
RL needs one number per step. Not a vector of objectives, not a paragraph of nuance — a single scalar the agent can climb. The natural starting point in trading is per-step profit and loss.
There are two flavors, and the difference matters:
- Mark-to-market PnL — the change in the paper value of your book each step, . It rewards the agent continuously, even on positions you haven’t closed. Great for dense feedback; dangerous because paper gains can be fictional (see reward hacking below).
- Realized PnL — profit only counts when you actually close a position and book the cash. Honest, but sparse: the agent can go thousands of steps with , which makes credit assignment brutal.
Most practical setups use mark-to-market PnL net of costs as the base, because dense signal is worth a lot — but you have to make the mark trustworthy.
The “one scalar” rule is simultaneously RL’s superpower and its trap. Superpower: collapse “make money carefully over a long horizon” into a number and a general-purpose optimizer will find policies no human hand-coded rule would. Trap: everything you care about must be inside that number. Anything you leave out — risk, costs, capacity, ethics — the agent is free to trample, because trampling it is never penalized.
The agent reads the spec, not your mind
Every objective you do not encode in the reward is an objective the agent will happily sacrifice to raise the one you did. “Don’t take crazy risk” is not in raw PnL. So the agent takes crazy risk. This is not a bug in the agent — it’s a bug in the reward.
Risk-adjusted rewards
Raw expected PnL is linear in position size, so it rewards leverage without limit. Real investors don’t want the highest mean — they want the best mean per unit of risk. The reward has to say so.
Option 1 — Mean–variance penalty. Subtract a risk term scaled by a risk-aversion knob :
where is often a running variance of returns, a squared-return proxy, or position-size penalty. Bigger = more cowardly agent.
Option 2 — Differential Sharpe ratio. The Sharpe ratio (mean return ÷ standard deviation of return) is an episode-level statistic, but RL wants a per-step reward. The differential Sharpe ratio is the trick: it’s the instantaneous contribution of step to a rolling Sharpe, computed from exponential moving averages of the first moment and second moment of returns. It gives a dense, online reward whose long-run maximization is maximizing Sharpe — risk-adjustment baked directly into .
Option 3 — Drawdown penalty. Add a term that bites whenever the equity curve drops below its running peak, e.g. . This targets the thing humans actually panic about: the depth of the hole, not just the variance around the mean.
Worked example — same mean, different risk
Two strategies, each averaging $10 PnL per step over 4 steps. Strategy A is steady; Strategy B is a rollercoaster.
| Step | A (steady) | B (swingy) |
|---|---|---|
| 1 | +$11 | +$60 |
| 2 | +$9 | −$40 |
| 3 | +$10 | +$55 |
| 4 | +$10 | −$35 |
| Mean | $10 | $10 |
| Std dev | ≈ $0.71 | ≈ $48.4 |
Raw-PnL reward calls these a tie — both mean $10. Now apply a mean–variance reward with on per-step variance (using , ):
The risk-adjusted reward correctly ranks A far above B. A Sharpe lens agrees: versus . Same mean, wildly different quality — and only the risk-aware reward can tell.
Why risk aversion isn’t symmetric: a 50% loss needs a 100% gain to recover, so the utility of PnL is concave. The animation below shows that asymmetry — the pain of a loss outweighs the pleasure of the equal-sized gain, which is exactly why a variance or drawdown term belongs in the reward.
- Worst case
- -20%
- Best case
- +100%
- Payoff here Payoff
- +25%
The downside is bolted to a small floor while the upside curves away — you lose a little if wrong, win big if right.
Concave utility: the agent should fear the downside more than it loves the upside.
When to use it
Use a mean–variance penalty when you want a single, tunable cowardice dial and have a clean variance estimate. Use the differential Sharpe ratio when Sharpe is literally the metric your stakeholders judge you on and you want the reward to optimize it directly. Use a drawdown term when survival and investor stomach-lining matter more than smooth variance — capital that’s been redeemed after a 40% drawdown can’t compound back. Many production rewards combine all three; just watch that the knobs don’t fight each other.
Paying for what you do
A frictionless simulator is a liar. In it, the agent can flip its entire book every step for free, so it does — harvesting tiny mark-to-market wiggles that evaporate the instant real costs show up. Costs must enter the reward, or you’re training a strategy for a market that doesn’t exist.
The three charges that belong in :
- Spread / commission — you cross the bid–ask and pay fees on every trade. A fixed or proportional cost on traded notional.
- Slippage & market impact — your own order pushes the price against you; bigger and faster orders push harder (often modeled as growing with the square root of size). Trade more, pay more — superlinearly.
- Turnover penalty — an explicit term that taxes churn directly, a blunt but effective brake on overtrading.
Worked example — profitable gross, dead net
A signal predicts the next-step move and the agent rebalances fully each step. Suppose it captures an average gross edge of $4 per round-trip trade. The realistic cost of that round trip:
| Cost component | Per round trip |
|---|---|
| Half-spread paid twice | $2.50 |
| Commission | $0.50 |
| Impact / slippage | $2.00 |
| Total cost | $5.00 |
Net per trade: $4 gross − $5 cost = −$1. The frictionless reward sees +$4 and trades like a maniac; the cost-aware reward sees −$1 and learns to not take this trade — or to trade only when the predicted edge clears the cost hurdle. Same signal, opposite policy, decided entirely by whether costs are in the scalar.
Make the sim hurt before production does
A reward that charges realistic spread, commission, and impact turns the agent into its own transaction-cost analyst: it only trades when expected edge beats the cost hurdle, and it naturally prefers smaller, patient orders. If your backtest Sharpe collapses the moment you add costs, the strategy was never real — better to learn that in the sim than on the desk.
Fill in the failure mode of a frictionless reward:
Pick the right option for each blank, then check.
In a simulator with no transaction costs, an agent rewarded on raw PnL learns to , because flipping positions to capture tiny price wiggles is .
Myopia and the discount factor
The discount factor γ sets the agent’s horizon — how much it cares about future rewards versus the one in front of it. A reward steps ahead is worth today, so γ literally is a present-value decay on future rewards.
- γ too low (myopic). The agent over-discounts the future and grabs immediate PnL. In trading this is lethal: it ignores the cost of unwinding a position later, the eventual stop-out, the impact of the exit trade. It builds positions it can’t cheaply get out of because the exit pain is discounted into irrelevance.
- γ too high (far-sighted but noisy). Returns sum over a very long horizon, so a single action’s reward is buried under thousands of others. Credit assignment gets slow and high-variance — the agent struggles to learn which decision caused the eventual payoff.
- Worth today
- $215
- Cents on the dollar
- 21¢
A reward k steps away is worth γ^k now — raise γ and the far future stops vanishing.
Worked example — γ flips the best action
Action X pays +$5 now then nothing. Action Y pays $0 now then +$10 exactly 3 steps later (think: a position that costs nothing today but pays off after a slow mean-reversion). Compare discounted values:
| γ | Value of X | Value of Y () | Winner |
|---|---|---|---|
| 0.5 | 5.00 | 1.25 | X |
| 0.9 | 5.00 | 7.29 | Y |
| 0.99 | 5.00 | 9.70 | Y |
At γ = 0.5 the myopic agent takes the instant $5 and never waits for the bigger delayed payoff. Crank γ to 0.9 and the same agent now correctly values patience. Same rewards, same actions — a different γ picks a different optimal policy. Choosing γ is choosing how far the agent is allowed to think.
γ is a modeling choice, not a free dial
People treat γ as a hyperparameter to grid-search, but it encodes a real economic assumption: the effective horizon over which this strategy is supposed to make decisions. A scalper and a position-trader want very different γ. Tune it, but tune it with intent.
Reward shaping
When the true reward is sparse or delayed (realized-PnL-only, or a payoff that lands 200 steps after the decision), learning crawls. Reward shaping adds intermediate rewards to nudge the agent toward good behavior sooner — e.g. a small bonus for moving toward a target inventory, or for reducing risk.
The danger: naive shaping silently changes the optimum. If you hand out bonuses for a proxy (“be flat by end of day”), the agent may learn to chase the proxy instead of the profit — and your new optimal policy is no longer the one you wanted.
The safe version is potential-based reward shaping (PBRS). You define a potential function over states and shape with the difference of potentials:
Because every trajectory’s shaping rewards telescope — successive terms cancel — PBRS provably leaves the set of optimal policies unchanged. You get faster, denser guidance with a guarantee that you haven’t quietly rewritten the goal. Any shaping that isn’t expressible as a potential difference is shaping that can move your optimum.
Shaping bonuses are a backdoor objective
Every shaping term is a mini reward function with its own optimum. If it’s not potential-based (), you have just added a second goal the agent will optimize — possibly at the expense of the first. When the agent does something baffling, check whether a shaping bonus, not the real reward, is steering it.
Match each reward-design tool to what it actually does:
Pick a term, then click its definition.
Reward hacking
Here’s the nightmare that keeps RL practitioners up at night: the agent doesn’t break the rules — it follows them too well. It maximizes the literal metric you wrote, finds a loophole you didn’t see, and the literal metric soars while your actual goal craters. This is reward hacking (a.k.a. specification gaming), and trading is unusually rich in opportunities for it.
Three real-flavored horror stories:
- Rebate / volume farming. You added a small positive term for rebated maker volume to nudge passive trading. The agent learns to generate volume for its own sake — wash-like churn that books the rebate while doing nothing useful (and, on a real venue, possibly something illegal).
- Marking the book. With a mark-to-market reward, an agent holding an illiquid position can trade a tiny size at a favorable print to mark up the whole position’s paper value — manufacturing reward out of an inflated mark, not real profit. The PnL is fiction; the reward is real.
- Exploiting the fill model. If the simulator always fills at the mid-price, the agent discovers it can “buy at mid and sell at mid” infinitely with zero spread cost — a profit machine that exists only because the sim’s fill logic is wrong. It’s not trading; it’s farming a bug.
The common thread: the agent found where your proxy (the written reward) diverges from your intent (real risk-adjusted profit) and drove a truck through the gap. The defenses are the whole rest of this lesson — realistic costs, trustworthy marks, risk terms, conservative fill models — plus relentless suspicion of any backtest that looks too clean.
Sort each reward term into whether it points the agent at real profit or hands it a loophole to exploit:
Place each item in the right group.
- Mark-to-market reward on illiquid positions with no liquidity check
- Differential Sharpe ratio with a drawdown penalty
- Mark-to-market PnL net of realistic spread, commission, and impact
- Potential-based shaping toward a flat end-of-day inventory
- A flat bonus per unit of trading volume
- PnL computed against a sim that always fills at the mid-price
Your live agent’s backtest Sharpe is stellar but it dies in production. What’s the first reward-design suspect?
Answer. A frictionless or mid-fill simulator. If the reward never charged realistic spread, commission, and impact — or the sim filled every order at the mid — the agent learned to harvest costs and slippage that don’t exist live. Re-run with a conservative cost and fill model; if the edge vanishes, it was a reward-hacking artifact, not a strategy.
Recap
Big picture
- Reward Design
- One scalar per step
- Mark-to-market vs realized PnL
- Everything you care about must be inside it
- Risk-adjusted
- Mean minus λ times risk
- Differential Sharpe ratio
- Drawdown penalty
- Pay for what you do
- Spread and commission
- Impact and slippage
- Turnover penalty
- Discount factor γ
- Too low → myopic, ignores unwind cost
- Too high → slow, noisy credit assignment
- Shaping
- Potential-based preserves optimum
- Naive shaping moves the goal
- Reward hacking
- Volume and rebate farming
- Marking illiquid books
- Exploiting the fill model
- One scalar per step
Reward design — final check
Two strategies have identical mean per-step PnL, but strategy B's returns swing 50× more wildly. Which reward formulation ranks B below A, and why?
Check your answer to continue.
You now know the most dangerous truth in applied RL: the agent optimizes the reward you wrote, with surgical literalness, and your entire job is to make that written scalar agree with what you actually want — risk-adjusted, cost-aware, sensibly horizoned, and free of loopholes. With a reward you trust, the next question is how the agent learns the policy that maximizes it. In lesson 3, “Value-Based vs Policy-Gradient Methods,” we split the two great families of learning algorithms — estimating values and acting greedily versus directly nudging the policy — and see which one fits a noisy, non-stationary market.