# XIII. Time-weighted Average Price (TWAP) OKX

TWAP is temporarily not supported under portfolio accounts.

1. What is the Time-weighted Average Price?

Time-weighted average price (TWAP) strategy splits your order and places smaller orders at regular time intervals.

It is a strategy that will attempt to execute an order which trades in slices of order quantity at regular intervals of time as specified by users. The purpose of TWAP is to minimize the market impact on basket orders. This bot triggers the order based on the predefined time intervals. It calculates the order price based on the current best bid/offer price，then splits the order into smaller ones and places them on the market(If the order is not fully filled, it will be canceled directly according to the IOC order rules).

2. Sample Case (BTC/USDT)

Assume that user A wants to buy BTC contracts below 10,500 USDT as soon as possible without increasing the purchase cost on the current market, so user A sets up a TWAP bot as follows:

Bot Parameters

Price variance: 1%

Price limit: 10,500 USDT

Interval: 20s

Average amount: 500 contracts

Total amount: 10,000 contracts

Run the bot

After the order is placed, the system will place orders at regular time intervals. Assuming the order book is as below:

According to the predefined price range, the highest buy price equals the Best Bid Offer (BBO), which is 10029.99*(1+price range 1.0%) = 10130.29 USDT. The total amount of sell orders below 10130.29 USDT is 570+1+200+1+1+1+1=775，and multiply the random number between 0.5 to 1 (e.g., 0.63) to get the order amount=775*63%=488.25 contracts. The order amount is less than the predefined average amount of 500 contracts, so the bot buys 488 contracts of sliced order at 10130.29 USDT. If the order is not fully filled, it will be canceled directly, which means the sliced order is an IOC order.

The bot will place orders constantly based on the predefined time intervals*random ratio (0.5~1) until the total filled amount reaches the predefined total order amount.

The bot will place the order at the limit price if the order price is higher than the predefined limit price.

If the order amount is more than the predefined average amount, the bot will place the order based on the average amount* random ratio (0.5~1).

The order will be suspended when the last price (20,500 USDT) is higher than the limit price and resumed when the last price is below 10,500 USDT again.

The bot will stop when the total filled amount equals the total order amount.