Placing a large order all at once moves the market against you. TWAP — Time-Weighted Average Price — splits it into smaller pieces over a defined time window, smoothing your fill price and reducing market impact.
This article walks through what TWAP is, when to use it, how to implement it on LMEX in Python, and the variants worth considering.
Place a market buy for 10 BTC on LMEX. Even on a liquid pair, the order walks up the book consuming liquidity. The first 1 BTC fills near the best ask; the last 1 BTC fills 10-50 basis points higher. Average fill is meaningfully worse than the original best ask.
For institutional-size orders, this slippage can dwarf the strategy's edge. A signal that says "go long" might be worth 30 basis points of expected return. If you slip 50 basis points entering, the trade is unprofitable before it starts.
The solution: don't place the whole order at once. Split it into smaller pieces, executed over time. The smaller pieces each cause minimal impact; the cumulative average fill price tracks the time-weighted average market price closely.
Three common execution algorithms:
**TWAP (Time-Weighted Average Price)** — split the order into equal pieces, execute at regular intervals. Simplest. Works well in steady markets, struggles when volume is uneven.
**VWAP (Volume-Weighted Average Price)** — split proportional to expected volume. Trade more during high-volume periods (typically market open/close), less during quiet periods. More sophisticated but requires accurate volume forecasts.
**POV (Percent of Volume)** — execute as a fixed percentage of real-time volume. Adapts dynamically. Best for very large orders where you want minimal footprint.
For most retail and small institutional orders, TWAP is the right choice. It's simple, transparent, and effective.
A working TWAP execution algo for LMEX:
The bot places 10 market orders of 0.1 BTC each, spaced 3 minutes apart. Total execution time: 30 minutes. Average fill should be close to the 30-minute volume-weighted average price.
A naive TWAP using market orders still incurs some slippage per slice. To minimise this, use limit orders at slightly aggressive prices (e.g., the bid for buys, the ask for sells with a small offset):
This is more efficient — limit orders cross the spread less aggressively than market orders, capturing better fills. The fallback to market order on no-fill prevents stuck orders.
For very small orders (< 0.1% of order book depth), market orders are fine. For larger orders, limit-based TWAP saves meaningful basis points.
A pure TWAP at exact intervals is predictable. Sophisticated counterparties can detect it and front-run. To avoid this, add randomisation:
Random qty and timing make the order pattern much harder to detect or game.
TWAP isn't universal. A few situations where it's wrong:
**Small orders** — for orders under 0.1% of order book depth, TWAP adds latency without meaningful improvement. Just market order.
**Urgent execution** — if a strategy signal is time-sensitive (breakout entry, stop-out exit), the price drift during TWAP execution can exceed the slippage savings. Use direct market orders.
**Trending markets** — TWAP averages your fill across time, which is great in chop but bad in trends. A 30-minute buy TWAP during a sharp rally fills you at progressively worse prices. Consider VWAP (front-loaded execution) instead.
**Illiquid markets** — TWAP relies on continuous liquidity during the execution window. In illiquid markets, individual slices can themselves cause material impact. Spread the order over hours/days rather than minutes.
Q: How many slices should I use?
5-20 is a reasonable range. Fewer slices is faster but more impactful. More slices is smoother but takes longer and adds latency. For most retail orders, 10 slices over 10-30 minutes is the sweet spot.
Q: What's the optimal TWAP duration?
Depends on order size relative to typical volume. Rule of thumb: spread execution over a window where total volume is 50-100x your order size. For a 10 BTC order on BTC-PERP, that's typically 15-60 minutes.
Q: Can I cancel a TWAP partway through?
Yes — track the order IDs and cancel any unfilled ones. The bot above doesn't support this natively, but adding a kill-switch flag that breaks the loop is straightforward.
Q: Does LMEX have built-in TWAP support?
Not natively as a single API call. You implement it client-side using the standard order endpoints. The advantage: you have full control over slicing logic and can customize for specific strategies.