TradeLabs AILearn

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.

Advanced3 min readUpdated 3 Oct 2026
Markdown
Lesson 26 of 27

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#

StageWhat happens
Market to youThe venue publishes data; it travels over networks to your machine
Receive and decodeYour network stack and feed handler process the message
DecisionYour strategy updates state and decides
Order sendThe order is built, risk checked and sent
You to venueThe order travels to the venue
Venue processingThe matching engine processes and acknowledges. See Matching Engines

Key measurements#

MetricFromTo
Feed latencyVenue event timestampYour receive timestamp
Internal (tick to trade)Receiving the market data that triggered a decisionSending the resulting order
Order round tripSending an orderReceiving the acknowledgement
Fill latencySending an orderReceiving 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#

BottleneckFix
Blocking logging or disk writesWrite asynchronously
Heavy work in the receive loopMove analytics elsewhere. See Message Queues
Garbage collection pausesReduce allocations, tune or control collection
Network distanceHost closer to the venue. See Co-Location and VPS, Cloud and Bare-Metal Servers
Operating system schedulingPin threads to cores. See CPU Affinity, NUMA and Cache Optimization
Kernel network stackKernel bypass for extreme cases. See Kernel Bypass and Low-Latency Networking
JSON parsingFaster 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?#

StrategyMeaningful latency
Daily or swing tradingSeconds to minutes do not matter
Intraday on minute barsUnder a second is usually fine
Reacting to news or breakoutsTens of milliseconds can matter
Market making and arbitrageMicroseconds

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.

Check your understanding

3 quick questions on this lesson. Get them all right to finish it.

Turn on JavaScript to take the quiz.

Finished this lesson?Sign in to save your progress across devices.
Next lessonMarket Data ReplayMarket data replay feeds recorded live data back through a trading system to test, debug and benchmark it. Learn how to record, replay and stay deterministic.

Mentioned in