1/5
## TWAP from tk to tn In the last video, we derived an equation for calculating the Time-Weighted Average Price (TWAP) from time tk to tn. This equation requires us to know the price at each time ti and the duration, Δti, during this period. To simplify the calculation, we can use a cumulative price. This is a state variable that represents the sum of price changes up to a certain time. We'll denote the cumulative price at time tj as Cj. The equation for calculating the cumulative price is: ``` Cj = cumulative price up to tj = Σ(i = 0 to j - 1) Δti * pi ``` We can rewrite the TWAP equation using cumulative price. The top part of the equation is the sum of price changes from time tk to tn. By expanding the sum and subtracting the cumulative price up to time tk, we get: ``` Σ(i = k to n - 1) Δti * pi = (Δt0 * p0 + ... + Δtk - 1 * pk - 1 + Δtk * pk + ... + Δtn - 1 * pn - 1) - (Δt0 * p0 + ... + Δtk - 1 * pk - 1) ``` This simplifies to Cn - Ck. Therefore, the TWAP equation using cumulative price is: ``` TWAP from tk to tn = (Cn - Ck) / (tn - tk) ``` This equation is much simpler to implement because we only need to store the cumulative price as a state variable. We can then calculate the TWAP by taking the difference of the cumulative prices at time tn and tk, and dividing it by the duration. [Diagram of TWAP calculation using cumulative price]
A simple guide to calculating TWAP (Time-weighted average price) using cumulative price. This lesson demonstrates how to calculate the TWAP from time tk to tn using cumulative prices. It is shown how to calculate the cumulative price and how to use it in the TWAP equation.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
How to use Uniswap v2 dex and contracts
Interacting with the Uniswap v2 router and factory
How to create Uniswap v2 liquidity pools
How to add liquidity to Uniswap v2 pools
Swaps, flash swaps, flash swap arbitrage, and time-weighted average price (TWAP)
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on October 9, 2024
Solidity Developer
Uniswap V2Duration: 14min
Duration: 1h 20min
Duration: 10min
Duration: 54min
Duration: 25min
Duration: 26min
Duration: 1h 03min
Duration: 59min
Course Overview
About the course
How to use Uniswap v2 dex and contracts
Interacting with the Uniswap v2 router and factory
How to create Uniswap v2 liquidity pools
How to add liquidity to Uniswap v2 pools
Swaps, flash swaps, flash swap arbitrage, and time-weighted average price (TWAP)
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on October 9, 2024
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar