Skip to content
Finance Lessons

Reinforcement Learning for Trading

Markets as a Markov Decision Process

The formal language of reinforcement learning — state, action, reward, transition, policy, return and discounting — why trading fits it and forecasting does not, and the one feature that separates a market from a video game: your action moves the environment.

15 min Updated Jun 19, 2026

Supervised machine learning is a fortune-teller. You hand it a feature vector, it hands back a number — tomorrow’s return, the probability of a fill, next hour’s volatility — and then it shrugs and goes home. It never tells you how much to buy, when to stop, or what to do once the order you just placed has moved the very price you were trying to predict. Forecasting is a one-shot answer to a question nobody actually asked. Trading is a sequence of decisions unfolding in time, each one changing the board you play the next one on.

Reinforcement learning (RL) is the discipline built for exactly that shape: an agent takes actions, the world answers with a reward and a new situation, and the agent learns a rule — a policy — that maximizes reward over the long run, not just the next tick. That long-run framing is also where our recurring villain returns. Across the machine-learning course the enemy was overfitting: a model that memorized noise and called it signal. It is still the enemy here — only now it wears a craftier disguise, because an RL agent gets to choose its own training experience, and a backtest that looks like free money is usually an agent that has overfit a simulator nobody should have trusted.

Before you read — take a guess

Your model nails next-day return with an out-of-sample R² that makes the desk jealous. Why is that, on its own, still not enough to trade?

The vocabulary of sequential decisions

RL has a small, precise vocabulary, and almost every confusion in the field comes from blurring two of these terms together. Define them once, carefully, and the rest of the course is just engineering.

  • State sts_t — everything the agent observes at time tt that is relevant to its decision. In trading: current price, your inventory (position), recent returns, order-book imbalance, time-of-day, remaining order to execute.
  • Action ata_t — what the agent does. Buy/hold/sell, a target position, a quoted bid–ask pair, or how many child orders to release this minute.
  • Reward rtr_t — the scalar feedback after acting. Often a risk-adjusted, cost-inclusive P&L increment — and designing it correctly is so dangerous it gets its own lesson next.
  • Transition / dynamics P(st+1st,at)P(s_{t+1} \mid s_t, a_t) — the rule of the world: the probability of landing in the next state given the current state and action. In markets this is the price process plus the impact of your own trade. You almost never know it; you sample from it.
  • Policy π(as)\pi(a \mid s) — the agent’s strategy: a (possibly randomized) mapping from states to actions. This is the object you are actually learning.
  • Trajectory / episode — one full play-through: s0,a0,r0,s1,a1,r1,s_0, a_0, r_0, s_1, a_1, r_1, \dots A trading episode might be one day, or the lifetime of unwinding one parent order.
  • Return GtG_t — the cumulative (discounted) reward from time tt onward. The thing the agent maximizes. Not a price forecast — an ugly coincidence of vocabulary we will be very careful about.
RL termSymbolTrading interpretation
Statests_tPrice, inventory, recent returns, book imbalance, time left
Actionata_tBuy / hold / sell, target position, quotes, child-order size
Rewardrtr_tRisk-adjusted P&L this step, net of costs and impact
TransitionPPPrice dynamics plus your trade’s market impact
Policyπ(as)\pi(a\mid s)The trading strategy you’re learning
EpisodeOne day, or one full order unwind
ReturnGtG_tCumulative discounted future reward (the objective)
Tip:

The two 'returns' trap

In finance, “return” means a percentage price change. In RL, “return” GtG_t means cumulative future reward. They are unrelated. When this course writes GtG_t it always means the RL one. When it means a price change, it says so in words.

Slot each idea into its RL term.

Pick the right option for each blank, then check.

The strategy you are learning — a mapping from situations to moves — is the . The rule of the world that says where you land next is the . The scalar feedback you get for acting is the .

A market is a Markov decision process

Stack those pieces together and you have a Markov decision process (MDP) — the formal object RL studies. Mechanically it’s a loop: the agent observes the state, its policy picks an action, the environment returns a reward and the next state, and around it goes, forever.

The agent–environment loop
action aₜreward rₜ, state sₜ₊₁Agent (policy π)HoldEnvironment (market)$100.0
Step
0
State (price, inv)
100.0, 0
Last reward
+0.00
Episode return
+0.00

One loop = one decision. The agent reads the state, its policy picks an action, the market returns a reward and a new state — forever. Flip on Market impact and the agent’s own trades push the price against it, shrinking every reward: in trading, unlike a video game, the action moves the environment. That feedback is exactly what supervised learning cannot capture.

The “Markov” in MDP is a promise about the state. A process has the Markov property when the next state depends only on the current state and action — not on the entire history of how you got here:

P(st+1st,at,st1,at1,)=P(st+1st,at).P(s_{t+1} \mid s_t, a_t, s_{t-1}, a_{t-1}, \dots) = P(s_{t+1} \mid s_t, a_t).

This is not a law of nature; it is a design constraint on what you put in your state. If “last traded price” doesn’t summarize everything relevant — and in markets it never does — then the price alone is not Markov, and an agent that conditions only on it is flying blind. The fix is not to bolt on memory of the past; it is to enrich the state until it is sufficient: add your inventory (so the agent knows its own exposure), recent returns (momentum/mean-reversion context), and order-book features (imbalance, depth, spread). A well-engineered state makes the Markov property true enough to be useful.

A 3-state Markov regime chain converging to its stationary distribution
BullBearFlat
0.050.100.200.250.250.150.800.700.50BullBearFlat
Current stateBullStep0
Visit frequencyStationary (long-run)
Bull100%
Bear0%
Flat0%

The token only ever looks at where it is now — never where it has been — to decide its next hop. That is the Markov property. Yet keep stepping and the share of time spent in each regime settles toward a fixed long-run mix: the stationary distribution.

The chain above shows the same idea on regimes: the token decides its next hop using only where it is now, yet the long-run share of time in each regime settles to a fixed mix. Memoryless transitions, stable statistics — that is the Markov property earning its keep.

Worked example — a tiny 2-state trading MDP

Picture a toy market with two regimes as the relevant state: Calm and Volatile. Each step the agent picks a position size, but first the world transitions. From Calm, the dynamics are:

P(CalmCalm)=0.8,P(VolatileCalm)=0.2.P(\text{Calm}\mid \text{Calm}) = 0.8,\qquad P(\text{Volatile}\mid \text{Calm}) = 0.2.

Suppose your policy in Calm goes long, and the reward (P&L increment) is r=+2r=+2 if you stay in Calm and r=5r=-5 if you tip into Volatile while still long. The expected one-step reward of being long in Calm is

0.8×(+2)+0.2×(5)=1.61.0=+0.6.0.8 \times (+2) + 0.2 \times (-5) = 1.6 - 1.0 = +0.6.

Notice what made that number: the transition probabilities, not a forecast of price. The agent isn’t predicting the next return — it’s weighing actions against the dynamics of the world, which is exactly what an MDP forces you to do.

In the toy MDP above, you decide the agent should also know its current inventory before choosing a size. Why?

The return and the discount factor γ

The agent doesn’t maximize the next reward — it maximizes the return, the cumulative discounted reward from now on:

Gt=rt+γrt+1+γ2rt+2+=k=0γkrt+k.G_t = r_t + \gamma\, r_{t+1} + \gamma^2 r_{t+2} + \dots = \sum_{k=0}^{\infty} \gamma^k\, r_{t+k}.

The discount factor γ[0,1)\gamma \in [0,1) decides how far the agent looks ahead. At γ=0\gamma = 0 the agent is purely myopic — it cares only about rtr_t and would happily blow up next step for a penny now. As γ1\gamma \to 1 it becomes far-sighted, treating a reward ten steps out almost like a reward today. Discounting also keeps the infinite sum finite and encodes a preference for sooner-rather-than-later profit.

Worked example — discounting a reward stream

Take a short episode with rewards r0=10,  r1=0,  r2=4,  r3=6r_0 = 10,\; r_1 = 0,\; r_2 = -4,\; r_3 = 6 and a discount of γ=0.9\gamma = 0.9. The return from t=0t=0 is:

G0=10+0.9(0)+0.92(4)+0.93(6).G_0 = 10 + 0.9(0) + 0.9^2(-4) + 0.9^3(6).

Compute the weights: 0.92=0.810.9^2 = 0.81 and 0.93=0.7290.9^3 = 0.729. So

G0=10+0+0.81(4)+0.729(6)=103.24+4.374=11.134.G_0 = 10 + 0 + 0.81(-4) + 0.729(6) = 10 - 3.24 + 4.374 = 11.134.

Now redo it myopically with γ=0\gamma = 0: G0=10G_0 = 10 flat — the agent never even sees the 4-4 shock or the +6+6 recovery. Same reward stream, completely different sense of “good,” set entirely by one knob.

If far-sightedness is better, why not just set γ = 1?

Answer. Two reasons. Mathematically, with an infinite horizon γ=1\gamma = 1 can make GtG_t diverge, so the objective stops being well-defined. Practically, markets are non-stationary — a reward 500 steps out is forecast through a world that will have changed — so weighting the far future as heavily as the present invites the agent to optimize a fantasy. A discount slightly below 1 (say 0.99) buys far-sightedness without betting the policy on a distant, unknowable future.

Info:

Reward design is a whole lesson

We’ve quietly assumed the reward rtr_t is handed to us. It isn’t — turning “make money carefully” into a single scalar is the hardest, most dangerous step in applied RL, and it’s the entire subject of lesson 2.

Prediction vs control — the deep difference

Here is the chasm between the previous course and this one. Supervised learning is prediction; RL is control.

In prediction, your data is (roughly) i.i.d., you estimate a fixed mapping to a label that exists independently of you, and — crucially — your prediction does not change the world. Guessing tomorrow’s rain doesn’t alter the weather. The label was going to be whatever it was going to be.

In control, you choose actions, and those actions change which states you visit next — they change the future data distribution your agent will learn from. In markets this is not a metaphor: a large buy moves the price (market impact), consumes liquidity, and shifts the order book the next decision is conditioned on. The environment is not a passive label generator; it’s a counterparty that reacts to you.

Supervised learning (prediction)Reinforcement learning (control)
Data(roughly) i.i.d., fixed distributionGenerated by your own actions; distribution shifts
OutputA prediction of a fixed labelA policy choosing actions over time
ObjectiveMatch a label (loss vs. truth)Maximize cumulative discounted return GtG_t
FeedbackThe label exists regardless of youYour action changes the next state
In marketsForecast a return; market ignores youYou trade; the market moves because of you
Failure modeOverfit static noiseOverfit a simulator you chose to live in

Flip on “Market impact” in the loop island above and you can watch this directly: the agent’s own trades push the price against it, shrinking every subsequent reward. That feedback — your action moves the environment — is the one feature a video game and a market share and a forecasting model can never represent.

Warning:

Pitfall — sizing is not classification

A tempting shortcut: train a classifier on {buy, hold, sell} labels and call the argmax your “policy.” It looks like RL; it isn’t. A classifier optimizes per-example accuracy against a fixed label, assuming your choice doesn’t change anything. But position size, timing, and the impact of your fill are sequential, self-affecting decisions with no ground-truth label to copy. Treating control as classification throws away the exact thing — the feedback loop — that made you reach for RL in the first place.

Match each statement to the paradigm it describes.

Pick a term, then click its definition.

The policy and the value function — a preview

The policy π(as)\pi(a \mid s) is the whole game: a (possibly stochastic) rule that, given a state, hands you an action — or a probability distribution over actions, so the agent can explore rather than always exploiting its current best guess. Everything in this course is, ultimately, a method for finding a good π\pi.

But how do you know a policy is good without playing infinitely many episodes? You estimate value. The state-value Vπ(s)V^\pi(s) is the expected return from state ss if you follow policy π\pi forever after; the action-value Qπ(s,a)Q^\pi(s,a) is the expected return from taking action aa in state ss and following π\pi thereafter. These two functions are the load-bearing beams of nearly every RL algorithm — they let you compare actions without enumerating the future — and lesson 3 builds them properly.

Checkpoint — the MDP frame

Question 1 of 40 correct

An agent uses γ = 0.95 and receives rewards r₀ = 4, r₁ = 10. What is the contribution of r₁ to the return G₀?

Check your answer to continue.

Recap

Big picture

Markets as an MDP

  • Markets as an MDP
    • The five pieces
      • State s — what you observe
      • Action a — what you do
      • Reward r — scalar feedback
      • Transition P — rule of the world
      • Policy π(a|s) — your strategy
    • Markov property
      • Next state depends only on now
      • Enrich state until sufficient
      • Add inventory, returns, book features
    • Return and γ
      • G = Σ γᵏ r
      • γ → 0 myopic
      • γ → 1 far-sighted
    • Prediction vs control
      • Prediction: output ignores world
      • Control: action changes future states
      • Markets: your trade moves the price
    • Coming next
      • V(s) and Q(s,a) — lesson 3
      • Reward design — lesson 2
State, action, reward, transition and policy compose into a Markov decision process; the agent maximizes discounted return; and the market's reaction to your trade is what makes this control, not prediction.

You now speak MDP: you can frame a trading problem as states, actions, rewards, transitions and a policy, you know why a price-only state is rarely Markov, and you can see the one feature — your action moves the environment — that makes this control rather than forecasting. But notice how much we leaned on the reward simply existing. Next, in Reward Design & Its Pitfalls, we put the whole weight of the agent on that single scalar — and watch what happens when “make money carefully” is even slightly mistranslated into math. It’s where most RL trading projects quietly die, and where our old villain, overfitting, finds its craftiest disguise yet: an agent that hacks the reward you wrote instead of the goal you meant.

Mark lesson as complete