DEX & Exchange

TWAP (Time-Weighted Average Price)

The average price of an asset over a specific time window; used as a manipulation-resistant price oracle in DeFi protocols.

TWAP (Time-Weighted Average Price) — TWAP, or Time-Weighted Average Price, is a pricing metric that calculates the average price of a cryptocurrency over a specific time period, giving equal weight to each time interval regardless of volume. TWAP is used in DeFi oracles to resist price manipulation and in trading algorithms to execute large orders gradually across a defined window.

How TWAP Works

TWAP is calculated by sampling the price at regular intervals (such as every minute or every block) over a defined period, then computing the arithmetic mean of all samples. If ETH is priced at $3,200, $3,210, $3,190, $3,220, and $3,205 over five consecutive intervals, the TWAP is $3,205. Unlike VWAP (volume-weighted average price), TWAP treats each time period equally, making it resistant to short-term volume spikes that could skew a weighted average.

In DeFi, on-chain TWAP oracles accumulate price data from AMM pools over time. Uniswap V3, for example, stores cumulative price tick data that allows any smart contract to compute a TWAP over any window from 1 second to the lifetime of the pool. To calculate the TWAP, a contract reads the cumulative price at the start and end of the desired window and divides the difference by the elapsed time.

TWAP trading algorithms work differently — they split a large order into smaller equal-sized chunks and execute one chunk per interval over the target period. A trader wanting to buy $1 million of ETH over 4 hours using a TWAP algorithm would execute approximately $4,167 every minute, regardless of market conditions at each interval.

Why TWAP Matters

TWAP serves two critical functions in crypto markets. First, as an oracle mechanism, it provides manipulation-resistant pricing for DeFi protocols. An attacker who briefly pushes a pool's spot price up by 50% using a flash loan would barely move a 30-minute TWAP, because the manipulation exists for only one block out of hundreds. This makes TWAP a safer pricing source for lending, derivatives, and governance applications.

Second, as a trading strategy, TWAP helps large traders execute positions with minimal market impact. Instead of placing a $5 million market order that would immediately move the price, a TWAP algorithm spreads the execution evenly over hours or days, blending into normal market activity. This reduces the average cost of entry and avoids signaling the trader's intentions to other market participants.

Real-World Example

A DeFi governance protocol needs to liquidate 500,000 USDC worth of treasury tokens over 24 hours without crashing the price. Instead of executing a single large sell order, the protocol deploys a TWAP contract that sells $347 worth of tokens every minute for 24 hours. Each individual sale is small enough to cause negligible price impact. The final average execution price is $1.02 per token, compared to the estimated $0.88 per token they would have received from a single-block market sell. The TWAP strategy saved the treasury approximately $70,000 (14%) by avoiding the concentrated price impact of a single large order.

Common questions about TWAP (Time-Weighted Average Price) in cryptocurrency and DeFi.

TWAP weights all time intervals equally, while VWAP weights each interval by its trading volume. TWAP is simpler and more manipulation-resistant since it cannot be skewed by a single high-volume transaction. VWAP better represents the true average cost of trading because it accounts for where most volume actually occurred.

For oracle pricing, longer windows (15-60 minutes) provide stronger manipulation resistance but introduce lag. For trade execution, the window depends on order size relative to market volume — larger orders need longer windows. A common guideline is to set the TWAP window so that each individual trade represents less than 1% of the average volume per interval.

TWAP oracles can be manipulated if an attacker sustains price distortion across many blocks rather than just one. However, this is extremely expensive because the attacker must maintain the manipulated price against arbitrageurs who will trade against the distortion. A 30-minute TWAP requires sustaining manipulation for hundreds of blocks, making the cost prohibitive for well-traded pairs.

Ready to put your knowledge into practice?

Start Boosting