# Time-Weighted Average Price (TWAP) in Financial Markets Contributor:
QuantInsti
Visit: QuantInsti

What is TWAP?

Time-weighted Average Price (TWAP) is a well-known trading algorithm which is based on the weighted average price and is defined by time criterion. TWAP is calculated for executing large trade orders. With the TWAP value, the trader can disperse a large order into a few small orders valued at the TWAP price since it is the most beneficial value.

This is basically done to not let a huge order suddenly increase the value of a particular financial asset in the financial market.

Let us now see an example of TWAP.

Example of TWAP

Let us assume that someone wants to make a strategy to buy 10,000 shares of an asset. In the process of making the strategy, one can choose from two possible strategies. One, a trader can issue orders to purchase 500 shares every 15 minutes for 5 hours.

Two, the trader can implement a strategy in which the order is issued to purchase 1,000 shares every 15 minutes for 2.5 hours.

But, with a strategy going on in the financial market, it is simpler to track the trading pattern by other traders and make a guess about the next strategy until and unless its orders are modified by adjusting the parameters. Further, this modification implies that the orders’ size can be randomized or the time between each order can be delayed.

Now, let us find out how the TWAP value can be calculated for a longer duration.

How is TWAP calculated?

In the case of Time-weighted Average Price, TWAP is calculated by averaging the entire day’s price bar, i.e., open, high, low, and close prices of the day. Then, on the basis of time decided to execute an order, every day’s averaged price is taken for calculating the average of the entire duration’s prices. This is known as the TWAP.

TWAP strategy is the best execution strategy for spreading out the trades over a specific time period and reduce the impact of trade on the market.

Like every trade, the TWAP also performs when all the conditions are met before which the price is calculated from the entry of the order and goes through the close of the market.

The calculation of TWAP goes as follows:

Average of each day’s price =

(Open + High + Low + Close)/4

Average of 28 days =

(Average of first day’s price + Average of second day’s price +……..+ Average of twenty-eighth day’s price)/28

In the excel sheet, you can calculate the average of each day. You can take the historical data from NASDAQ by simply typing the symbol of the stock you want the data of. Here, we have taken the data of Apple Inc. on which the calculation of the average of each day in the excel sheet is done in the following way:

Above, the calculation was done using the formula =AVERAGE(B2:E2) as shown in the cell. We had taken two months’ data from Apple Inc.

For calculating the average of each row, you simply need to copy and paste the formula for every row in the cell under TWAP.

Further, for averaging the entire period’s average, you simply put the formula =AVERAGE(G2:G23) as shown below:

The average comes out to be \$328.15 taken out under the heading Av_row.

After calculating TWAP, your order price will be considered undervalued when it is below TWAP and overvalued when it is above TWAP.

In Python also, we can calculate TWAP. First of all, we will fetch the data of the stock we wish to calculate TWAP of.

# Install package
!pip install yfinance

# Import libraries
import yfinance as yf
import pandas as pd

# Import price data for Apple

# Delete Volume column
del data[‘Close’]
del data[‘Volume’]
del data[‘Open’]
del data[‘High’]
del data[‘Low’]
data

Output:

Above we have got the entire data from 2020-05-18 to 2020-06-18 as we did above in the excel sheet.

Stay tuned for the next installment in this series – the author will find each row’s average under the column “av_row” since, in TWAP calculation, the first step is to find out the average of OHLC of each trading day.