Analyzing Lead–Lag Effects Between Exchanges Using Flat Files
Price discovery in crypto markets happens across multiple exchanges simultaneously. When prices move, they may appear on one exchange slightly earlier than others before propagating across the market.
This is known as the lead–lag effect.
However, there is an important caveat:
There is no permanently “leading” exchange. Leadership changes depending on market conditions such as liquidity, trading activity, and time of day.
In this tutorial, you will learn how to analyze lead–lag relationships using historical trade data from the CoinAPI Flat Files API.
What You Will Build
By the end of this tutorial, you will:
- Load real trade data from Flat Files
- Convert raw trades into time series
- Measure lead–lag using cross-correlation
- Interpret which exchange leads (for a given period)
- Understand why results change over time
Step 0 — Set Up Your Environment
0.1 Create Project Folder
Open your terminal:
0.2 Create Python File
Open this file in VS Code or your preferred editor.
0.3 Install Dependencies
0.4 Create Data Folder
Place your Flat Files inside:
Step 1 — Choose Comparable Trading Pairs
We use:
Why this matters:
- same quote currency (USDT)
- avoids distortion from USD vs USDT differences
- isolates true market behavior
Step 2 — Inspect Raw Data
Before writing logic, inspect the dataset.
Run Python:
Example — Binance Sample Rows
Example — OKX Sample Rows
👉 Notice:
- timestamps are irregular
- prices are similar but not identical
- trades occur at different times
Step 3 — Add Full Analysis Script
Open analysis.py and paste everything below:
Step 4 — Run the Script
Step 5 — Resampling Explanation
Example output:
Trades occur at irregular timestamps across exchanges. To compare price movements, we convert them into a regular time series using a fixed interval (RESAMPLE_FREQ).
In this tutorial, we start with a 1-second interval, which provides a stable and easy-to-interpret baseline.
🔍 Advanced Note: Choosing the Right Time Resolution
The RESAMPLE_FREQ parameter controls how granular your analysis is.
- "1s" (1 second): Stable and easy to interpret (recommended for this tutorial)
- "500ms" or "200ms": Captures finer market dynamics
- "100ms" or lower: Reveals microstructure effects but may introduce noise
If the resolution is too coarse: → You may miss lead–lag effects entirely
If the resolution is too fine: → Data may become sparse and correlations unstable
You can experiment by changing:
RESAMPLE_FREQ = "1s"
to:
RESAMPLE_FREQ = "200ms"
and rerunning the analysis.
Step 6 — Interpretation
At a 1-second resolution, no clear lead–lag relationship is observed between Binance and OKX.
This means that price movements on both exchanges appear highly synchronized within each 1-second interval.
However, this does not necessarily mean that no lead–lag exists. Instead, it suggests that any lead–lag effect likely occurs at a finer time resolution (e.g., milliseconds), which is not captured at the current aggregation level.
Step 7 — Run Across Multiple Days
Repeat the analysis with different datasets.
Example:
| Date | Leader | Lag |
| Day 1 | Binance | 1s |
| Day 2 | OKX | 0.5s |
| Day 3 | None | ~0s |
Step 8 — Optional Experiment
Try running the analysis with different resolutions:
- RESAMPLE_FREQ = "1s"
- RESAMPLE_FREQ = "500ms"
- RESAMPLE_FREQ = "200ms"
Compare the results across runs.
You may observe:
- No clear leader at 1-second resolution
- Small lead–lag effects emerging at sub-second resolution
This demonstrates how market microstructure becomes more visible at finer time scales.
Key Insight
- Lead–lag relationships are highly dependent on the chosen time resolution.
- At coarser resolutions (e.g., 1 second), markets may appear perfectly synchronized.
- At finer resolutions (e.g., milliseconds), small but meaningful delays between exchanges can become visible.
- This reinforces that there is no permanently "leading" exchange — observed leadership depends on both market conditions and the level of analysis.
Important Notes
- do NOT compare BTC/USD vs BTC/USDT
- always use same quote currency
- use high-liquidity exchanges
- results depend on time period
Conclusion
In this tutorial, you used Flat Files to:
- analyze trade-level data
- measure lead–lag between exchanges
- observe how price discovery propagates
The most important takeaway:
There is no single “fastest” exchange — the observed lead–lag relationship depends not only on market conditions, but also on the time resolution used in the analysis.