# 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.

Source: https://learn.tradelabsai.com/programming/latency-measurement/  
Track: Programming and Data · Level: Advanced · Updated: 2026-10-03  
Publisher: TradeLabs AI (https://tradelabsai.com). Education, not financial advice.  
Cite as: TradeLabs Learn, "Exchange vs Receive Timestamps and Latency Measurement", https://learn.tradelabsai.com/programming/latency-measurement/

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](https://learn.tradelabsai.com/orders/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](https://learn.tradelabsai.com/orders/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.

```python
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](https://learn.tradelabsai.com/infrastructure/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.

**Example: Why the average misleads**
A bot's internal latency over 10,000 orders has a median of 2 milliseconds. The average is 3.1 milliseconds. But the 99th percentile is 45 milliseconds and the maximum is 800 milliseconds. Investigation shows the slow orders happen when Python's garbage collector runs, and during bursts when a log write blocks. The average suggested a uniformly fast system; the percentiles revealed that 1 order in 100 is more than 20 times slower than normal, often during the busiest, most important moments. Moving logging to a background thread cuts the 99th percentile to 6 milliseconds.

## Common bottlenecks

| Bottleneck | Fix |
|---|---|
| Blocking logging or disk writes | Write asynchronously |
| Heavy work in the receive loop | Move analytics elsewhere. See [Message Queues](https://learn.tradelabsai.com/infrastructure/message-queues/) |
| Garbage collection pauses | Reduce allocations, tune or control collection |
| Network distance | Host closer to the venue. See [Co-Location](https://learn.tradelabsai.com/infrastructure/co-location/) and [VPS, Cloud and Bare-Metal Servers](https://learn.tradelabsai.com/infrastructure/vps-cloud-and-bare-metal-servers/) |
| Operating system scheduling | Pin threads to cores. See [CPU Affinity, NUMA and Cache Optimization](https://learn.tradelabsai.com/infrastructure/cpu-affinity/) |
| Kernel network stack | Kernel bypass for extreme cases. See [Kernel Bypass and Low-Latency Networking](https://learn.tradelabsai.com/infrastructure/kernel-bypass/) |
| JSON parsing | Faster parsers or binary protocols. See [Binary Protocols](https://learn.tradelabsai.com/infrastructure/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](https://learn.tradelabsai.com/algo-trading/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](https://learn.tradelabsai.com/programming/market-data-replay/).

## Continue learning

- Next lesson: [Market Data Replay](https://learn.tradelabsai.com/programming/market-data-replay/)
- Previous lesson: [Sequence Numbers, Dropped Packets and Out-of-Order Messages](https://learn.tradelabsai.com/programming/sequence-numbers/)
- Related: [Sequence Numbers, Dropped Packets and Out-of-Order Messages](https://learn.tradelabsai.com/programming/sequence-numbers/): Sequence numbers let trading systems detect lost, duplicated or out of order messages. Learn how gap detection, recovery and duplicate handling work in practice.
- Related: [Latency in Trading](https://learn.tradelabsai.com/orders/latency-in-trading/): Latency is the delay between a market event and your reaction to it. Learn the sources of trading latency, how it is measured and when it matters for your trades.
- Related: [Clock Synchronization and PTP](https://learn.tradelabsai.com/infrastructure/clock-synchronization-and-ptp/): Accurate clocks are vital for timestamps, latency measurement and regulation. Learn how NTP and PTP work, MiFID II and CAT clock rules and how to check your clocks.
- Related: [Co-Location](https://learn.tradelabsai.com/infrastructure/co-location/): Co location places trading servers inside or beside an exchange's data centre to cut latency. Learn how it works, what it costs, fairness rules and who needs it.
- Related: [Kernel Bypass and Low-Latency Networking](https://learn.tradelabsai.com/infrastructure/kernel-bypass/): Kernel bypass lets trading software read network packets straight from the network card, skipping the operating system. Learn how it works and the trade offs.
- Related: [Real-Time, Delayed and Historical Data](https://learn.tradelabsai.com/programming/real-time-data/): What real time market data really means, how it differs from delayed and snapshot data, where it comes from, what it costs and how to judge its quality.
