Óscar Romero Quincoces

Projects / Research and master's

RL for execution

An agent that learns to spread an order like a TWAP

2026MIAX master's, Instituto BME
A real Repsol order spread along the plan: average price paid and volume bought against the market
Figure 1. A real Repsol order spread along the plan: average price paid and volume bought against the market

Summary

The third reinforcement learning assignment. Every 5 seconds a Double DQN agent decides whether to buy one Repsol share or wait, on real order-book data. With the workshop's original reward it buys everything at the start; with a reward for following the plan it spreads the order just like a TWAP: an error of 0.90 shares and 27 of 27 orders completed. Reading the market saves 0.15 bp per order: real, but small. Team of 3.

The problem

A fixed number of Repsol shares has to be bought within a set time: a CARE buy order. Every 5 seconds the agent decides whether to buy one share or wait, and all it sees is how much time and how much volume are left. The benchmark is a TWAP, which buys at the same pace throughout. The data is real: the Repsol order book provided by the workshop.

This is the third assignment of the course; the first two are in RL for trading.

How it's built

A Double DQN agent in PyTorch. Every 25 episodes it sits an exam with no exploration on the same 27 orders, and that exam is used to pick the model, to regulate exploration and to stop in time. The error is the largest number of shares by which the agent strays from the plan it should follow.

The workshop's reward pays for buying below the average price; ours penalises, at every step, the distance between what has been bought and what should have been bought by then. The hyperparameters were tuned with sweeps (a discount of 0.9, a memory of 10,000 and learning every 8 steps), and exploration falls according to how close the agent is to the error of a TWAP, not according to the number of episodes. The code has tests.

Results

With the workshop's reward the agent completes every order but buys far too early: it finishes between 20% and 35% of the time window and strays 39.4 shares from the plan. With ours it copies the TWAP: an error of 0.90 shares, the same as the TWAP, and 27 of 27 orders completed. Exploring according to improvement helps when training is unstable (a final error of 0.96 against 5.65) and adds nothing when it was already stable.

With a plan that ends at 90%, the agent finishes at 89% of the time window, the same across 5 repetitions and on test data. Stopping once the target is met cuts training to 100–150 episodes instead of 300 with almost the same error (0.92 against 0.89). Reading the market saves 0.15 bp per order against the TWAP, and a placebo with shuffled data saves nothing; a simple rule saves more (0.34 bp) but is almost three times as irregular. Limits: between 3 and 5 repetitions per experiment, a single stock, and a simulator with no price impact or latency.

Figures

Linear exploration against improvement-based exploration, in a stable and in an unstable training run
Figure 2. Linear exploration against improvement-based exploration, in a stable and in an unstable training run
Four exploration schedules on the same seeds, episodes and orders
Figure 3. Four exploration schedules on the same seeds, episodes and orders
A 75-share order that finishes at 90% of the time window
Figure 4. A 75-share order that finishes at 90% of the time window
Saving per order against a TWAP at 90%, on test data: the agent that reads the market, a placebo and a simple rule
Figure 5. Saving per order against a TWAP at 90%, on test data: the agent that reads the market, a placebo and a simple rule

Built with

PythonPyTorchNumPypandasJupyter