An asset's time-weighted average price (TWAP) is the measure of an asset's average price over a predetermined period of time.

TWAP can be calculated for any specified time duration. TWAP trading algorithms seek to optimize a trade's average price while executing over a specified time period.

Here, we will discuss how to calculate TWAP:

Screenshot 2022-10-17 at 3.18.38 PM.png

Here, you can see for 0-4 seconds the price of ETH is 100 and from 4-5 seconds the price is 200 and then 100 again for the rest. Now, how to calculate the TWAP here?

Screenshot 2022-10-17 at 3.20.06 PM.png

Here, 100 times 4 divided by total (which is 6) and so on. Solving this:

Screenshot 2022-10-17 at 3.21.05 PM.png

Now, we will make a equation for this:

Screenshot 2022-10-17 at 3.21.31 PM.png

Lets use this. Here from T0 to T1 price is P0 and so on. The general formulla will be:

Screenshot 2022-10-17 at 3.23.53 PM.png

Screenshot 2022-10-17 at 3.24.50 PM.png

Now, if we want to do this from Tk to Tn:

In the equation just replace T0 with Tk and something like that:

Screenshot 2022-10-17 at 3.27.59 PM.png

Screenshot 2022-10-17 at 3.28.13 PM.png