Machine learning for trading, part two, in ten ideas

On this page
  1. 1. Reinforcement learning: learning from reward, not from labels
  2. 2. MDPs: a state, an action, a reward and the value of what follows
  3. 3. Q-learning: one update rule, repeated
  4. 4. Exploration: when to try something worse on purpose
  5. 5. Dyna: learn a model of the world, then practise in it
  6. 6. States and rewards: what you tell the learner decides what it learns
  7. 7. RL for trading: the market as an environment, and where that breaks
  8. 8. Comparing learners: change one thing at a time, or the result says nothing
  9. 9. Neural networks: linear regression, stacked and bent
  10. 10. Sequence models and responsible AI: what changes when the data is a sequence, and who it affects
  11. References

The first series was about prediction: a table of examples, each with the right answer, and models from linear regressionA simple method that predicts a numeric answer by assuming a straight-line relationship between the inputs and the answer. to boostingCombining many trees grown one after another, each using what the earlier trees got wrong. that learn to predict the answer for a new row. This second series is about decisions. A trading system does not only predict tomorrow’s return; it holds a position, and today’s choice changes what tomorrow’s choice costs. The first eight ideas build the learner that can handle that, reinforcement learningLearning what to do in each situation to collect the most numerical reward, by trying actions rather than being told which are right., and ask where it breaks in a market. The last two widen the toolkit to neural networksA model that takes a set of input variables and builds a nonlinear function of them to predict the answer., models built for sequences, and language models, and to what it takes to trust any of them with money. The examples again use daily prices from 2020 to 2025 for SPY, NVDA, GS, XOM and GLD. Read this page for the shape, then go to the post for the idea you need.

Two habits do most of the work this time:

The learner optimises the rewardThe single number the environment sends the agent at each step; the agent aims to maximise the total over the long run. you wrote down, not the goal you had in mind. And a result that looks good in the training years, before costs, has not yet shown anything.

The ten ideas at a glance, grouped by the job each one does (the grouping is mine; the numbers are the sections below):

ML 2

Frame the problem

Learn the values

Apply it to trading

Beyond the table

1 Reward, not labels

2 MDPs

3 Q-learning

4 Exploration

5 Dyna

6 States and rewards

7 RL for trading

8 Comparing learners

9 Neural networks

10 Sequences and responsible AI

1. Reinforcement learning: learning from reward, not from labels

A supervised learner is handed the right answer for every example. A reinforcement learner is handed a situation (the stateA signal telling the agent how the environment is at a given moment; the input its policy and value estimates use.), makes a choice (the actionOne of the set of things the agent can do; at each step it senses the state, chooses an action and performs it.), and afterwards receives one number (the reward). Nobody tells it what the right choice was. Its goal is the sum of the rewards over the long run, and what it learns is a policyThe agent's way of behaving: a mapping from the states it sees to the actions it takes in them.: a rule from state to action. The same ten days of SPY can be either kind of problem. As rows of featuresAn input variable a model uses to make its prediction; also called a predictor or an independent variable. with next-day returns as labelsThe outputs a model learns to predict; also called the target or the response., they ask “what will tomorrow bring?”. As one episodeOne natural run of interaction, such as one play of a game, ending in a terminal state before a reset. of buy-then-hold, they ask “what should I do, given that today’s choice is tomorrow’s starting point?”. The second question is harder for three reasons: the agentThe learner and decision maker: the part that chooses actions.’s own actions choose its training dataThe observed examples a method learns from: they are used to teach it how to make its estimate., the reward for a choice arrives spread over the days that follow, and to find a better action it must sometimes try one it believes is worse. Full post: Reinforcement learning: learning from reward, not from labels.

2. MDPs: a state, an action, a reward and the value of what follows

A Markov decision processThe reinforcement learning setup of agent, states, actions and rewards, written down with well defined transition probabilities. is the formal shape of the loop. Its one strong assumption is on the state: it must hold everything about the past that matters for the future, so that knowing the state, you need not know how you got there. The learner is judged by the return, the sum of the rewards still to come, with each later reward shrunk by a discount factorA setting that decides how much rewards further in the future are worth now, compared with a reward received right away. so that a reward far away counts for less than one tomorrow. The value of a state is the return you can expect from it under a policy; the value of an action is the same, for taking that action first. Bellman’s idea is that these values satisfy a recipe: the value of now equals the reward now plus the discounted value of next. The best policy is the one that, in every state, picks the action of highest value. Full post: MDPs: a state, an action, a reward and the value of what follows.

3. Q-learning: one update rule, repeated

A Q-tableA table with one entry for each state-action pair, holding the learner's current estimate of that pair's Q value. is a grid with one row per state and one column per action, holding the learner’s current estimate of each action’s value. Q-learningA method that learns each action's value in each state, aiming straight at the best values whatever policy the agent is following. fills it from experience with one rule. After each step the learner looks at what it got (the reward) and what the best it can now hope for is (the largest entry in the next state’s row), combines them into a target, and moves the old estimate a fraction of the way towards it. That fraction is the learning rateThe number that sets how big each update step is.: how much one new experience changes an estimate. The discount factor plays its separate role inside the target: how much the future counts. Because the target uses the best next action rather than the one actually taken, the table learns the values of acting greedily even while the learner is still exploringChoosing a non-greedy action so you can improve your estimate of its value.; this is what off-policyA method that evaluates or improves a policy different from the one that produced the data. means. Entries that are never visited never improve, so convergence needs coverage plus a shrinking learning rate; running longer cannot fix entries that are never visited. Full post: Q-learning: one update rule, repeated.

4. Exploration: when to try something worse on purpose

A learner that always takes the action it currently rates best may never find out that another is better: its first few tries of the right action were disappointing and it never went back. Sutton and Barto’s ten-armed test shows the greedyAn action whose estimated value is currently the highest. method finding the best action in only about a third of the tasks. The simple fix is epsilon-greedyA near-greedy rule: usually take the best-looking action, but sometimes pick among all actions at random with equal chance.: with a small probabilityHow often an event happens if you could repeat a trial without end: the share of trials where it happens., pick an action at random; otherwise take the best known. Exploring more finds good actions sooner and then wastes a share of its steps on random picks; exploring less learns slowly and then exploitsChoosing a greedy action, using what you already know about action values. better, so the usual compromise is to shrink the random share as experience grows. In a market, exploring costs real money, which is why the learning happens in a simulation and why the behaviour used in training is not the behaviour used live. Variability under a stable process (a stochastic environmentEverything outside the agent that it interacts with.) and a process that changes over time (a nonstationaryDescribes a problem whose true action values change over time. one) are different problems, and the second is the one markets pose. Full post: Exploration: when to try something worse on purpose.

5. Dyna: learn a model of the world, then practise in it

Real experience is slow and, in trading, expensive. DynaAn agent design in which planning, acting, learning a model and learning directly from real experience all run together. uses each real step twice: once to update the Q-table directly, and once to update a model of the environment, a record of which next state and reward followed each state and action. Between real steps the learner then draws remembered state–action pairs from the model and runs the same Q-update on them, many times, as if it were replaying experience. The gain is sample efficiency: more learning per real step. The cost is that the model can be wrong, and planningAny computation that takes a model of the environment and produces or improves a policy. on a wrong model makes the learner plan towards a wrong answer. In a deterministic world remembering the one outcome is enough; in a stochastic one the model has to hold the spread of outcomes or sample from it. The lesson reaches past Dyna to every backtestTesting a trading strategy by simulating how it would have traded over some past period. and every simulator: practice data is only as good as the process that made it. Full post: Dyna: learn a model of the world, then practise in it.

6. States and rewards: what you tell the learner decides what it learns

A Q-table needs a finite list of states, and market variables are continuous, so each indicatorA calculation on a security's prices or volume whose result is used to anticipate future price changes. is cut into bins and the bin numbers are combined into one state label. Every choice there is a choice about the problem. Too few bins merge situations that need different actions; too many leave most states visited once or never. If two situations that call for different trades share a state, the learner cannot tell them apart, whatever it does next. Holdings belong in the state for that reason: the same signal means “buy” to an agent in cash and “do nothing” to one already long. Reward is the other dial. Today’s price change, today’s change in account value, realised gain and gain after costs all teach different habits, and a bonus or penalty added to speed learning up can teach the learner to collect the bonus instead of making money. Full post: States and rewards: what you tell the learner decides what it learns.

7. RL for trading: the market as an environment, and where that breaks

Turning the learner into a trading system means answering, in order: what the state is built from (only what was known that morning), what an action means (a target position, not a repeated order), how a held position carries from day to day, and what the reward charges for. CommissionsThe fee a broker charges for a trade; one part of a trade's transaction cost. and the price you move against yourself can turn a strategy that is profitable before costs into one that loses after them, and high turnoverHow much of a portfolio is bought or sold over a period, as a share of its value; more turnover means more trading cost. is usually a sign the reward or the action mapping is wrong rather than a sign of skill. A policy trained on one period is tested on a later one that it never saw, once, without going back to tune on the result. Even then a market changes in ways a maze does not: the rules that produced the training years need not hold in the next. Ritter’s paper gives a worked example of a reward that charges for risk as well as for costs, and shows a Q-learner finding a known edge in a simulated market with trading costs. Full post: RL for trading: the market as an environment, and where that breaks.

8. Comparing learners: change one thing at a time, or the result says nothing

A hand-written rule, a supervised learner that predicts the next return and trades on the prediction, and a reinforcement learner that outputs the position directly are solving three different problems with the same prices. The supervised one needs labels and a separate rule to turn a forecast into a trade; the reinforcement one needs a reward and a simulator, and learns the trade directly. Replaying a fixed price history is still interactive for the agent’s own holdings, costs and rewards, even though its trades cannot move the recorded prices. A fair comparison gives all three the same features at the same times, the same test period, the same starting capital, the same costs and the same benchmark, and reports the same measures for each. When they disagree, the reason is usually in the objective each was given, not in which algorithm is cleverer. Full post: Comparing learners: change one thing at a time, or the result says nothing.

9. Neural networks: linear regression, stacked and bent

A single-layer network takes the features, forms several weighted sums of them (one per hidden unit), passes each sum through a fixed nonlinear function such as ReLUAn activation function that keeps a positive number and turns a negative one into zero., and fits a linear regressionA prediction problem whose answer is a number; when the answer is a category it is classification instead. on those outputs. Without the bend, stacking linear layers would still be one linear model; the activation functionA nonlinear function inside a neural network that is chosen in advance, not learned. is what lets the network represent relationships a straight line cannot. Training means choosing the weights that make a loss small: forward propagation computes the prediction, backpropagationThe way a neural network's fitting works out, by the chain rule, how much each parameter contributed to the error. computes how the loss changes with each weight, and gradient descent steps the weights downhill, usually on a small batch of examples at a time, with one pass through all the data called an epochOne complete pass through the whole training set during fitting.. More layers and more units mean more capacity and more ways to fit noise, so regularisationAdding a penalty to what fitting minimises, the main cure for overfitting. (a penalty on the weights, dropoutA form of regularisation for neural networks: while fitting, randomly remove a fraction of the units in a layer., early stoppingStop training at the point where the error on validation data is lowest, before the model starts to overfit.) is part of the recipe rather than an extra. The loss the network minimises is still prediction error, which is not the same as trading profit. Full post: Neural networks: linear regression, stacked and bent.

10. Sequence models and responsible AI: what changes when the data is a sequence, and who it affects

Market data has an order, so three families of model are built to respect it. A convolutional model slides the same small filter along the series and picks up short local patterns. A recurrent model carries a hidden state from one step to the next, so earlier inputs can influence later outputs. A transformerA neural network built on attention, which maps a set of input vectors to a new set of the same size. uses attentionWeighting every input by how relevant it is to the current position, with the weights computed from the inputs themselves., which lets each position weigh every other position with weights that depend on the data, so it can relate distant points and compute in parallel, but it has to be told the order explicitly. Zeng and colleagues combine the first and third to model short- and long-range dependence in intraday prices. Language models extend the toolkit to text, and bring a failure mode numbers never had: fluent output with nothing behind it. A fabricated source or figure in a research note is a financial risk, so checking sources, keeping a person in the loop, being able to explain a decision and monitoring the system in use are part of the design, not a review added at the end. Full post: Sequence models and responsible AI: what changes when the data is a sequence, and who it affects.

References

  • Sutton and Barto, Reinforcement Learning: An Introduction, 2nd edition (MIT Press, 2018). Chapter 1 for what reinforcement learning is and how it differs from supervised learningLearning from examples where every input comes with a known answer, so a model can learn to predict the answer for new cases.; Chapter 2 for the ten-armed testbed and epsilon-greedy action selection; Chapter 3 for Markov decision processes, the return, discounting, policies and value functionsWhat is good in the long run: for each state, the total reward the agent can expect to collect from there on.; section 6.5 for the Q-learning update; Chapter 8 for models, planning and Dyna-QA simple Dyna agent: one-step tabular Q-learning on real experience plus Q-learning updates on transitions replayed from a learned model.; the introduction to Part II for why large state spaces force generalisation.
  • Tom M. Mitchell, Machine Learning (McGraw-Hill, 1997). Chapter 13 for the learning task, delayed reward and credit assignmentWorking out which of the actions in a sequence deserve credit for the rewards that came later., and Q-learning.
  • Kevin P. Murphy, Probabilistic Machine Learning: Advanced Topics (MIT Press, 2023). Section 35.1 for the map of value-based, policy-search and model-basedDescribes methods that use a model of the environment and planning. methods.
  • James, Witten, Hastie, Tibshirani and Taylor, An Introduction to Statistical Learning with Applications in Python (Springer, 2023). Chapter 10 for single-layer networks, activation functions, fitting by gradient descentFitting by repeated small steps in the direction that lowers the error fastest, recomputing that direction after every step., epochs and minibatches, and ridgeLeast squares with a penalty that shrinks the coefficients towards zero; a tuning parameter sets how strong the shrinking is. and dropout regularisation.
  • Bishop and Bishop, Deep Learning: Foundations and Concepts (Springer, 2024). Chapter 12 for attention and transformers.
  • Gordon Ritter, Machine Learning for Trading (2017). A Q-learner with a reward that charges for risk, in a simulated market with trading costs.
  • Zeng, Kaur, Siddagangappa, Rahimi, Balch and Veloso, Financial Time Series Forecasting using CNN and Transformer (2023). Convolutions for short-range and attention for long-range dependence in intraday prices.
  • CFA Institute Research Foundation, Handbook of Artificial Intelligence and Big Data Applications in Investments (2023). Chapter 1 for explainability and the black-box problem in investment models; Chapter 11 for human oversight in an automated research system.
Tagged machine-learning, reinforcement-learning, trading, neural-networks← Home