← All projects
order flow Reinforcement learning in a market simulated by a transformer trained on Nasdaq order flow.
How it works A transformer reads the orders sent to a stock exchange the way a language model reads text. Each token is one event (an offer to buy or sell, a cancellation, or an order that trades at once) with its price, size and delay, plus the offers still waiting after it. The model predicts the next event's kind, then its price, then its size. It has 2.3 million parameters and learned from 13 days of Nasdaq orders. Training took 45 minutes on a $350 GPU.
Order flow architecture
Each order sent to the exchange is one token: the sum of embeddings of its kind, its price offset from the best quote, its size and its delay, plus a linear projection without bias of 39 features of the order book after it, the log of the spread and, for 10 levels per side, the log of their distance and size. Four causal decoder blocks of width 256 follow, each an RMSNorm attention with 4 heads over the last 128 events, RoPE and normalised queries and keys, and a SwiGLU MLP of hidden width 352. Four heads predict the next order: its delay, its kind, its offset given the kind, and its size given both. Training took 2 passes over 131 million events in 44 minutes. The trader concatenates the model's last hidden state, 256-d, with its 9-d account. A linear layer maps these 265 inputs to 16 scores, the score of each of its 30 actions is the sum of its parts bounded by 8 tanh(s/8), and a softmax over the actions follows, after adding each score to the action's log-probability under the model. It is trained with GRPO: four episodes from the same start are compared, each episode's advantage is its reward minus the group's mean, over the group's standard deviation, with no value head, and a PPO update with a clipped ratio follows.
Order flow architecture
Decoder-only transformer, 2,269,692 parameters, trained on 13 days of Nasdaq orders
A
Model
B
Trader
Loss: sum of the four cross-entropies
2 passes over 131 million events, 44 minutes
One token per order sent to the exchange
Kind
limit buy
Offset
0 ticks
Size
300
Delay
2 ms
Book
39-d
embeddings, summed
Linear, no bias
Book: log spread, then for 10 levels per side,
the log of their distance and size
Decoder block × 4
width 256
RMSNorm, attention, 4 heads
last 128 events, RoPE, QK-norm
RMSNorm, SwiGLU 256→352→256
Delay
Kind
Offset
Size
Next event
1
Policy
Model state h, 256-d
last hidden state
Account, 9-d
cash, shares, orders, profit
‖
concatenate [account, h], 265-d
Linear 265→16
16 scores: wait 1, side 2, cancel 1, type 4, size 8
Score of each of the 30 actions
sum of its parts, bounded: 8·tanh(s/8)
log-probability under the model
Softmax over the 30 actions
2
GRPO: 4 episodes from one start
episode
1
2
3
4
R
account gain
+$12
+$3
−$5
+$8
A
advantage
+1.2
−0.2
−1.5
+0.6
A = (R − group mean) / group standard deviation
No value head: the group's mean is the baseline.
Every action in an episode gets that episode's A.
PPO update, probability ratio clipped to [0.8, 1.28].
Episodes of 30 decisions differ only in the trader's sampling.
120 iterations of 384 episodes.
On days kept out of training: about $10 ahead
of keeping its shares.
Order flow architecture
Each order sent to the exchange is one token: the sum of embeddings of its kind, its price offset from the best quote, its size and its delay, plus a linear projection without bias of 39 features of the order book after it, the log of the spread and, for 10 levels per side, the log of their distance and size. Four causal decoder blocks of width 256 follow, each an RMSNorm attention with 4 heads over the last 128 events, RoPE and normalised queries and keys, and a SwiGLU MLP of hidden width 352. Four heads predict the next order: its delay, its kind, its offset given the kind, and its size given both. Training took 2 passes over 131 million events in 44 minutes. The trader concatenates the model's last hidden state, 256-d, with its 9-d account. A linear layer maps these 265 inputs to 16 scores, the score of each of its 30 actions is the sum of its parts bounded by 8 tanh(s/8), and a softmax over the actions follows, after adding each score to the action's log-probability under the model. It is trained with GRPO: four episodes from the same start are compared, each episode's advantage is its reward minus the group's mean, over the group's standard deviation, with no value head, and a PPO update with a clipped ratio follows.
Order flow architecture
Decoder-only transformer, 2,269,692 parameters,
trained on 13 days of Nasdaq orders
A
Model
Loss: sum of the four cross-entropies
2 passes over 131 million events, 44 minutes
One token per order sent to the exchange
Kind
limit buy
Offset
0 ticks
Size
300
Delay
2 ms
Book
39-d
embeddings, summed
Linear, no bias
Book: log spread, then for 10 levels per side,
the log of their distance and size
Decoder block × 4
width 256
RMSNorm, attention, 4 heads
last 128 events, RoPE, QK-norm
RMSNorm, SwiGLU 256→352→256
Delay
Kind
Offset
Size
Next event
B
Trader
1
Policy
Model state h, 256-d
last hidden state
Account, 9-d
cash, shares, orders, profit
‖
concatenate [account, h], 265-d
Linear 265→16
16 scores: wait 1, side 2, cancel 1, type 4, size 8
Score of each of the 30 actions
sum of its parts, bounded: 8·tanh(s/8)
log-probability under the model
Softmax over the 30 actions
2
GRPO: 4 episodes from one start
episode
1
2
3
4
R
account gain
+$12
+$3
−$5
+$8
A
advantage
+1.2
−0.2
−1.5
+0.6
A = (R − group mean) / group standard deviation
No value head: the group's mean is the baseline.
Every action in an episode gets that episode's A.
PPO update, probability ratio clipped to [0.8, 1.28].
Episodes of 30 decisions differ only in the trader's sampling.
120 iterations of 384 episodes.
On days kept out of training: about $10 ahead
of keeping its shares.
The model was trained only to predict orders, yet its market behaves like a real one in two ways. When more shares wait to be bought than sold at the best prices, the price usually rises next: the queues predict the direction of 66% of price moves, against 69% on Nasdaq. A large sale moves the price roughly with the square root of its size, as in real markets. The simulated market still runs too fast and keeps too many offers waiting.
An exchange matches the orders, and the price comes only from those trades. It is written in Rust and compiled to WebAssembly for this page.
The trader drawn in colour starts with 1,000 shares. It reads the model's internal state and its own account, and adds learned scores to the model's probabilities of its orders. It learned by reinforcement learning (GRPO) to grow its account. On days kept out of training, it ended about $10 ahead of keeping its shares.