Exchange vs Receive Timestamps and Latency Measurement
How to measure latency in a trading system: where to timestamp, tick to trade and order round trip, percentiles instead of averages and how to find bottlenecks.
Latency is the time between something happening and your system responding to it. For a long term investor, seconds do not matter. For an intraday bot, a delay of a few hundred milliseconds can turn a good signal into a bad fill. For high frequency firms, microseconds decide who wins. Whatever your speed, you cannot improve latency without measuring it, and measuring it correctly takes more care than simply subtracting two times. This lesson covers how to measure; Latency in Trading explains why it matters.
Where latency comes from#
| Stage | What happens |
|---|---|
| Market to you | The venue publishes data; it travels over networks to your machine |
| Receive and decode | Your network stack and feed handler process the message |
| Decision | Your strategy updates state and decides |
| Order send | The order is built, risk checked and sent |
| You to venue | The order travels to the venue |
| Venue processing | The matching engine processes and acknowledges. See Matching Engines |
Key measurements#
| Metric | From | To |
|---|---|---|
| Feed latency | Venue event timestamp | Your receive timestamp |
| Internal (tick to trade) | Receiving the market data that triggered a decision | Sending the resulting order |
| Order round trip | Sending an order | Receiving the acknowledgement |
| Fill latency | Sending an order | Receiving the fill |
Timestamp at every boundary#
Record a timestamp when each message arrives, after decoding, when the strategy decides, when risk checks finish and when the order leaves. The difference between adjacent timestamps shows exactly where time is spent.
import time
t0 = time.perf_counter_ns() # message received
msg = decode(raw)
t1 = time.perf_counter_ns() # decoded
order = strategy.on_message(msg)
t2 = time.perf_counter_ns() # decided
send(order)
t3 = time.perf_counter_ns() # sent
record(decode=t1 - t0, decide=t2 - t1, send=t3 - t2)
Use a monotonic clock such as perf_counter_ns for durations inside one machine; it never jumps backwards when the system clock is adjusted. Comparing times across machines needs synchronised clocks. See Clock Synchronization and PTP.
Use percentiles, not averages#
Latency distributions have long tails. The average hides rare but costly delays. Report the median (50th percentile), 99th and 99.9th percentiles, and the maximum.
Common bottlenecks#
| Bottleneck | Fix |
|---|---|
| Blocking logging or disk writes | Write asynchronously |
| Heavy work in the receive loop | Move analytics elsewhere. See Message Queues |
| Garbage collection pauses | Reduce allocations, tune or control collection |
| Network distance | Host closer to the venue. See Co-Location and VPS, Cloud and Bare-Metal Servers |
| Operating system scheduling | Pin threads to cores. See CPU Affinity, NUMA and Cache Optimization |
| Kernel network stack | Kernel bypass for extreme cases. See Kernel Bypass and Low-Latency Networking |
| JSON parsing | Faster parsers or binary protocols. See Binary Protocols |
Measuring against the venue#
Some venues include their own receive and send timestamps in acknowledgements, letting you split round trip time into network and venue processing. Record these and compare over time; changes can reveal network problems or venue load.
How fast is fast enough?#
| Strategy | Meaningful latency |
|---|---|
| Daily or swing trading | Seconds to minutes do not matter |
| Intraday on minute bars | Under a second is usually fine |
| Reacting to news or breakouts | Tens of milliseconds can matter |
| Market making and arbitrage | Microseconds |
Spend effort where it changes results. See High-Frequency Trading.
Frequently asked questions#
What is tick to trade latency?#
The time from receiving the market data that triggers a decision to sending the resulting order.
Why use percentiles for latency?#
Latency has long tails; percentiles such as the 99th show the rare slow events that averages hide.
How do I measure latency in Python?#
Record time.perf_counter_ns() at each processing stage and analyse the differences, using synchronised clocks for comparisons across machines.
Next, learn how recorded data helps testing in Market Data Replay.
3 quick questions on this lesson. Get them all right to finish it.
Turn on JavaScript to take the quiz.
Mentioned in
- WebSocket Market Data StreamsProgramming and Data
- Timestamps, Time Zones and Daylight SavingProgramming and Data
- Order Book Feeds: Snapshots and Incremental UpdatesProgramming and Data
- Feed Handlers and NormalizationProgramming and Data
- Sequence Numbers, Dropped Packets and Out-of-Order MessagesProgramming and Data
- Networking for TradersTrading Infrastructure