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:
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?
Here, 100 times 4 divided by total (which is 6) and so on. Solving this:
Now, we will make a equation for this:
Lets use this. Here from T0 to T1 price is P0 and so on. The general formulla will be:
Now, if we want to do this from Tk to Tn:
In the equation just replace T0 with Tk and something like that: