HFT: Basics of orderflow
If you donβt understand order flow, youβre trading blind. Hereβs what happens when smart money enters the game
Table of contents:
Introduction.
What is order flow?
Why flat lines are a myth?
The square-root law
The basics of reaction-diffusionβaka simulating the trading frenzy
Introduction
Picture this: youβre selling ice cream on a sunny beach. Lifeβs good. But suddenly, a bus full of tourists arrives. Your cart is swarmed. Do you:
A) Keep prices the same and run out of ice cream in 5 minutes?
B) Raise prices because heck yeah, supply and demand?
If you picked B, congratulations! Youβve just mastered the first rule of trading: big demand β higher prices. But hereβs the twist: in financial markets, itβs not quite that simple. Instead of tourists, imagine hedge funds with billion-dollar orders. Instead of ice cream, think Tesla shares. And instead of you, picture a grumpy market maker muttering, βWhy canβt these traders just stick to small orders?β
Before we dig in, a friendly reminder: while many sophisticated models might mention topics like overfitting or the Kelly criterion, today weβll leave those behind. Our focus is on clear, engaging insights and simple mathematics. Ready to trade some ice cream for knowledge? Letβs begin!
What is order flow?
Letβs start with a hypothesis: Big trades move prices more than small ones. Shocking, right? But how do we quantify this? Enter order flowβthe heartbeat of markets that refers to the sequence of transactions. Every buy/sell order tells a story:
Small orders are like casual conversations among friends.
Big orders are the equivalent of shouting across the room, grabbing everyoneβs attention.
In 1991, Joel Hasbrouck published a paper titled Measuring the Information Content of Stock Trades. His findings?
Price impact isnβt instant. Like bad news from your in-laws, it takes time to sink in.
Bigger trades = bigger impact. A 1M order hits harder than a 100 one.
Market makers hate surprises. They widen spreads after big trades.
Illiquid markets = drama queens. Thinly traded stocks overreact to everything.
Speaking of drama, letβs talk about order books β the marketβs version of a Rorschach test.
Letβs put on our math professor hat for a momentβdonβt worry I promise to keep it simple. Suppose we denote the order size by Q. In many trading scenarios, the impact of a trade isnβt directly proportional to Q. Instead, itβs more nuanced. Big orders tend to have a disproportionately large effect, but not as dramatically large as a one-for-one increase in size might suggest.
To illustrate, consider a simplistic relationship:
where:
I is the price impact.
k is a constant that encapsulates market conditions.
Ξ± is an exponent that is often less than 1, meaning the impact grows less than proportionally with order size.
For many markets, researchers have found that Ξ± is around 0.5. In plain English, if you quadruple your order size, the price impact roughly doubles rather than quadruples. Itβs like our ice cream cart: if you suddenly serve four times as many cones, you might run out faster, but the extra excitement in the queue only increases so much.
Now that weβve set the stage with order flow, letβs move on to something even more visual: the order book. Think of it as the marketβs ledgerβa detailed menu of who is buying and selling, at which prices, and in what quantities. Unlike the neatly drawn lines in textbooks, real order books are as quirky as they are informative. Ready to dive into this art form? Letβs go!
I must admit that this photo really looks like an avocado and strawberry ice cream π
Why flat lines are a myth?
Can a system truly remain in a state of perfect equilibrium, or is every flat line just an illusion hiding underlying fluctuations?
In some books, you might see an order book depicted as two perfectly horizontal linesβone for buy orders, one for sell orders. In the real world, however, order books are more like the zany scribbles of a mad artist: they form a V shape, reflecting the depth and liquidity of the market.
Letβs break down a real order book:
Buy orders or Bids: These represent the quantities buyers are willing to purchase at different prices. The closer you are to the current market price, the fewer shares you might get, but at a more favorable price.
Sell orders or Asks: These indicate the quantities sellers are ready to sell. As you move away from the current market price, you might find larger orders, but at a steeper cost.
Hereβs a simplified visual. Fake order books look like this:
| Buy Orders | Sell Orders |
|100 shares @ $10|100 shares @ $11|
|100 shares @ $10|100 shares @ $11|
|100 shares @ $10|100 shares @ $11|
But real order books? They look like this, just like in the picture above:
| Buy Orders | Sell Orders |
|50 shares @ $9.90|50 shares @ $10.10|
|200 shares @ $9.80|200 shares @ $10.20|
|500 shares @ $9.70|500 shares @ $10.30|
Notice the V shape? As you dig deeper into the order bookβfurther from the market price. The available quantity increases, but the prices become less attractive. Itβs like going to the back of an ice cream truck and finding a shelf full of flavors you wouldnβt normally chooseβeven though theyβre plentiful.
If order books were actually flat, trading would be as easy as ordering pizza. But no, theyβre more like a sushi conveyor beltβthe good stuff gets snatched fast, leaving you with seaweed salad.
Okay! Having visualized the order book, letβs move on to one of the most fascinating discoveries in market impact research: the square-root law. If order flow is the marketβs gossip and the order book its colorful ledger, then the square-root law is the neat mathematical formula that ties it all together. Letβs crack the code behind this intriguing relationship.
The square-root law
When we first looked at how orders affect prices, you might have expected a linear relationship: double the order, double the impact. But real markets are a bit more forgiving. Researchers discovered that the price impact I of an order of size Q scales with the square root of Q:
where:
I is the price impact.
Ο represents the marketβs volatility.
Q is the trade size.
V is the daily traded volume.
Letβs breaking down the formula:
Volatility (Ο) is like the marketβs emotional barometer. A volatile market is like a roller coasterβfast and unpredictable.
Trade size (Q): The bigger your trade, the more likely you are to nudge the market. But hereβs the kickerβthe impact grows with the square root of QQQ, not directly with QQQ itself.
Daily volume (V) is the total activity in the market. If youβre trading in a market where millions of shares change hands, your 10,000-share trade might be a mere ripple.
A tradeβs impact grows with the square root of its size. So, if you trade 4x as much, the impact is only 2x. Markets are kind of forgiving!
import math
def calculate_impact(Q, V, sigma):
"""
Calculate the price impact using the square-root law.
Parameters:
Q (float): The size of the trade.
V (float): The daily traded volume.
sigma (float): The market volatility (as a decimal, e.g., 0.02 for 2%).
Returns:
float: The estimated price impact.
"""
impact = sigma * math.sqrt(Q / V)
return impact
# Example parameters
sigma = 0.02 # 2% daily volatility
V = 1_000_000 # 1,000,000 shares traded daily
Q = 10_000 # Trading 10,000 shares
price_impact = calculate_impact(Q, V, sigma)
print(f"Price Impact: {price_impact:.4f}")
# Output: 0.0020 β 0.2% price move
Buying 10k shares with 1M daily volume moves the price by ~0.2%. Not bad! π¬
If youβre wondering why itβs a square root and not, say, a cube root, blame the V shape. Or thank it. Either way, send your complaints to the French mathematicians who discovered this.
Now that weβve tamed the beast of market impact with our trusty square-root law, itβs time to introduce a more dynamic model: Bouchaudβs reaction-diffusion model. If you thought our previous topics were interesting, buckle upβthis model takes us on a wild ride into the physics of trading. Letβs dive in!
The basics of reaction-diffusionβaka simulating the trading frenzy
Picture a bustling marketplace where buyers and sellers move around like particles in a fluid. Jean-Philippe Bouchaud, one of the rockstars of market impact research, likened traders to particles undergoing a reaction-diffusion process. In simple terms, imagine that every buy or sell order is a little particle moving around, occasionally colliding and reacting.
In physics, reaction-diffusion models describe how particles interact and spread out over time. When two different particles meet, they might react and change the state of the system. In our trading analogy:
Buyers (particle A): Exclaim, I need this stock now!
Sellers (particle B): Counter, sell, sell, sell!
When these particles collide, a trade happens. Over time, these interactions create ripplesβmuch like throwing a stone into a pond.
The reaction-diffusion model is governed by partial differential equations (PDEs), which, donβt worry, we wonβt fully derive here. The key takeaway is that the propagation of these βtrading wavesβ can be modeled, and the impact of a big trade is analogous to throwing a rock into still water: the ripples spread out and gradually dissipate.
A simplified version of the model might look something like thisβin continuous form:
where:
C(x,t) represents the concentration of βtrading pressureβ at price level x and time t.
D is the diffusion coefficientβhow fast the impact spreads.
R is a reaction term representing the decay of impact over time.
In case you want to play around hereβs a very simplified Python snippet that simulates the diffusion of trading impact over time:
import numpy as np
import matplotlib.pyplot as plt
# Simulation parameters
L = 100 # Number of price levels
T = 50 # Number of time steps
D = 0.1 # Diffusion coefficient
R = 0.05 # Reaction (decay) rate
# Initial condition: a big trade at the center price level
C = np.zeros((T, L))
C[0, L//2] = 1.0 # Impact concentrated in the middle
# Simulate diffusion over time
for t in range(1, T):
for x in range(1, L-1):
diffusion = D * (C[t-1, x-1] - 2 * C[t-1, x] + C[t-1, x+1])
reaction = -R * C[t-1, x]
C[t, x] = C[t-1, x] + diffusion + reaction
# Plot the impact over time for select time steps
plt.figure(figsize=(30, 7))
for t in [0, 10, 20, 30, 40, 49]:
plt.plot(C[t, :], label=f'Time {t}')
plt.xlabel('Price level')
plt.ylabel('Trading impact')
plt.title('Simplified reaction-diffusion model of trading impact')
plt.legend()
plt.grid(True)
plt.show()
We simulate a price space of 100 levels and let the impact evolve over 50 time steps. Then, the initial impact is simulated as an impulse in the center of our grid. For each time step, the impact diffuses spreads to neighboring price levels and decays, which is the reaction term.
Now that weβve explored how big orders create ripples in the market, letβs shift gears to the realm of high-frequency trading. These are the cheetahs of the trading worldβquick, agile, and always ready to pounce on any opportunity. How do they use our insights from order flow, order books, and diffusion models to make split-second decisions? Letβs find out.
They are like the espresso shots of the financial markets: fast, potent, and capable of waking up even the sleepiest market maker. Their algorithms constantly monitor order flow, track order book dynamics, and use mathematical modelsβlike our trusty square-root lawβto predict and capitalize on price movements.
HFT firms leverage several key strategies:
Real-time order flow analysis: Every trade, no matter how small, provides data. HFTs analyze this data in real time to predict where the market is headed.
Rapid quote adjustments: Based on the expected price impactβcalculated via our square-root law. HFTs adjust their bid and ask prices faster than you can say latency arbitrage.
Slicing big orders: Instead of executing a massive order all at onceβand alerting the entire marketβbig orders are sliced into smaller chunks. This reduces immediate market impact and helps the order blend in with normal trading noise.
HFT algorithms are like that friend who always finishes your sentencesβonly instead of finishing your thoughts, theyβre finishing off all the available liquidity. If HFTs were any faster, theyβd be ordering pizza before the delivery guy even picked up the call!
Despite our neat formulas and elegant models, the reality of market impact is as messy as a dropped ice cream cone on a hot sidewalk. Here are some of the hurdles and nuances that even the most sophisticated models must face:
The metaorder puzzle.
A metaorder is a large order that is split into many smaller trades to minimize market impact. Imagine trying to eat an entire tub of ice cream in one goβyouβd probably end up with brain freeze. Instead, you take it spoonful by spoonful. Similarly, big orders are sliced and diced to hide their true size.
Challenge: Detecting whether a series of trades is part of a metaorder can be like trying to figure out if your neighborβs multiple trips to the fridge are for a midnight snack or an all-out ice cream binge.
Implication: If you know a big order is being executed in pieces, market makers might widen spreads, anticipating the continued pressure on price.
Informed vs. uninformed trading.
Not all trades are created equal. Some traders have insider insights, while others are simply following the herd.
Informed trades: When traders have privileged information, their trades can have a larger impact on price.
Uninformed trades: These are like a casual impulse buy at the ice cream truckβthey might cause a minor stir but generally donβt move the market much.
Challenge: Distinguishing between these types of trades is difficult, and models must account for the fact that not every trade is equally influential.
Time decay and impact fading.
Market impact is not static. Just as the excitement around a new ice cream flavor fades after the initial rush, the impact of a large order decays over time.
Mathematical twist: Some models introduce a time decay factor to account for how the price impact dissipates. Though we wonβt dive into the complex mathematics here, the idea is that the market forgets large orders over time.
Practical implication: A trade that moved the market significantly a few minutes ago may have little impact now, allowing liquidity to recover.
Liquidity and market conditions.
Market liquidity can vary wildly. In highly liquid markets, even large orders might only cause a small ripple. In contrast, in a thinly traded market, the same order could create a tidal wave.
Implication: Trading in a low-liquidity market is like trying to swim in a kiddie poolβevery movement creates a big splash!
Consideration: Traders must adjust their strategies based on current liquidity levels, and models must be flexible enough to handle these variations.
Noise and randomness.
Markets are influenced by countless factorsβnews, economic data, investor sentiment, and even the weather. This randomnessβaka noiseβmakes it challenging to isolate the pure effect of a trade.
Implication: Even the best models sometimes get it wrong because the market is a living, breathing entity full of surprises.
While these challenges might seem daunting, they also highlight the beauty of the markets: a place where mathematics meets human behavior, and every trade tells a story.
P.S. You don't mind helping me filter topics for the newsletter, do you? Let me know which ones you prefer!
π Did you gain valuable insights and now want to empower others in the field?
Appendix
Don't forget to check out this Rust example for inspiration on how to identify toxic flow by creating the VPIN metric [GitHub-VPIN].