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
data = yf.download(‘AAPL’, start=”2020-05-18″, end=”2020-06-18″)
# Calculate adjustment factor
adjustment_factor = data[‘Adj Close’] / data[‘Close’]
# Calculate adjusted open price
data[‘Adj Open’] = adjustment_factor * data[‘Open’]
# Calculate adjusted high price
data[‘Adj High’] = adjustment_factor * data[‘High’]
# Calculate adjusted low price
data[‘Adj Low’] = adjustment_factor * data[‘Low’]
# Delete Volume column
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.
Visit QuantInsti website to download additional Python code: https://www.quantinsti.com/
Disclosure: Interactive Brokers
Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from QuantInsti and is being posted with permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.